如何使用注解在SpringMVC中创建默认方法? [英] How to create a default method in SpringMVC using annotations?

查看:112
本文介绍了如何使用注解在SpringMVC中创建默认方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我找不到解决方案,这让我发疯。我映射了@Controller,它使用@RequestMapping响应多种方法。如果未指定其他具体方法,我想将其中一种方法标记为默认方法。例如:

I can't find a solution to this, and it's driving me crazy. I have @Controller mapped that responds to several methods using @RequestMapping. I'd like to tag one of those methods as default when nothing more specific is specified. For example:

@Controller
@RequestMapping("/user/*")
public class UserController {

   @RequestMapping("login")
   public String login( MapModel model ) {}

   @RequestMapping("logout")
   public String logout( MapModel model ) {}

   @RequestMapping("authenticate")
   public String authenticate( MapModel model ) {}
}

因此/ user / login->登录方法,/ user / logout->注销等。我想这样做,以便转到/ user,然后路由到这些方法之一。但是,我在@RequestMapping上看不到任何允许我将这些方法之一指定为默认处理程序的内容。我也看不到任何其他可用于此目的的注释。我开始怀疑它不存在。

So /user/login -> login method, /user/logout -> logout, etc. I'd like to make it so that if someone goes to /user then it routes to one of these methods. However, I don't see anything on @RequestMapping that would allow me to specify one of these methods as a default handler. I also don't see any other annotations that might be used on the class either to do this. I'm beginning to suspect it doesn't exist.

我正在使用Spring 2.5.6。在3.0.0中解决了吗?我可能只是破解Spring使其工作,因为它非常烦人,这并不简单。

I'm using Spring 2.5.6. Is this solved in 3.0.0? I might just hack Spring to make it work because it's tremendously annoying this isn't more straightforward.

感谢前进。

推荐答案

看看这个答案:

Spring MVC和带注释的控制器问题

如果注释了方法:

@RequestMapping(method = RequestMethod.GET)

您可以在此处查看示例:

You can see an example here:

Spring 3.0 MVC + Hibernate:通过注释进行简化–教程

在这里可以看到相同的行为:

The same behavior can be seen here:

Aaron Schram撰写的Spring Framework 3.0 MVC (请参见第21页)

这篇关于如何使用注解在SpringMVC中创建默认方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆