java 如何使用注解在 SpringMVC 中创建默认方法?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3327682/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to create a default method in SpringMVC using annotations?
提问by chubbsondubs
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 响应几种方法。当没有指定更具体的内容时,我想将其中一种方法标记为默认值。例如:
@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 ) {}
}
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.
所以/user/login -> 登录方法,/user/logout -> logout 等。我想这样做,如果有人去/user 然后它路由到这些方法之一。但是,我在 @RequestMapping 上没有看到任何允许我将这些方法之一指定为默认处理程序的内容。我也没有看到任何其他可能用于该类的注释来执行此操作。我开始怀疑它不存在。
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 2.5.6。这在 3.0.0 中解决了吗?我可能只是破解 Spring 使其工作,因为它非常烦人,这并不简单。
Thanks in Advance.
提前致谢。
回答by Leniel Maccaferri
Take a look at this answer:
看看这个答案:
Spring MVC and annotated controllers issue
What if you annotate a method with:
如果您使用以下方法注释方法会怎样:
@RequestMapping(method = RequestMethod.GET)
You can see an example here:
你可以在这里看到一个例子:
Spring 3.0 MVC + Hibernate : Simplified with Annotations – Tutorial
Spring 3.0 MVC + Hibernate:使用注解简化——教程
The same behavior can be seen here:
可以在此处看到相同的行为:
Spring Framework 3.0 MVC by Aaron Schram(look at page 21)
回答by vegemite4me
Short answer: I do not know how to simply specify one method as default with a simple tag.
简短回答:我不知道如何使用简单的标签简单地将一种方法指定为默认方法。
But there is this ...
但是有这个...
I do not know in which version of Spring this was implemented, but you can provide multiple values to @RequestMappingin 3.1.2. So I do this:
我不知道这是在哪个版本的 Spring 中实现的,但是您可以在 3.1.2 中提供多个values @RequestMapping。所以我这样做:
@Controller
@RequestMapping("/user")
public class UserController {
@RequestMapping(value = {"", "/", "/list"}, method = RequestMethod.GET)
public String listUsers(ModelMap model) { }
@RequestMapping(value = "/add", method = RequestMethod.POST)
public ModelAndView add(HttpServletRequest request, ModelMap model) { }
}
The following URLs then map to listUsers():
以下 URL 然后映射到listUsers():
回答by Panini Luncher
I would create one default method without RequestMapping's value in there. Please see method defaultCall()below. You can then simply call them with URL: [yourhostname]/user
我会创建一个没有 RequestMapping 值的默认方法。请参阅下面的方法defaultCall()。然后您可以简单地使用 URL 调用它们:[yourhostname]/user
@Controller
@RequestMapping("/user")
public class UserController {
@RequestMapping(method = RequestMethod.GET)
public String defaultCall( MapModel model ) {
//Call the default method directly, or use the 'forward' String. Example:
return authenticate( model );
}
@RequestMapping("login")
public String login( MapModel model ) {}
@RequestMapping("logout")
public String logout( MapModel model ) {}
@RequestMapping("authenticate")
public String authenticate( MapModel model ) {}
}
回答by cmorris
Simply using @RequestMapping("**")on your default method should work. Any more specific mappings should still pick up their requests. I use this method for setting up default methods sometimes. Currently using Spring v4.3.8.RELEASE.
只需@RequestMapping("**")在默认方法上使用即可。任何更具体的映射仍应接受他们的请求。我有时使用这种方法来设置默认方法。目前使用 Spring v4.3.8.RELEASE。

