Java Spring MVC @RequestMapping ...使用方法名称作为操作值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9718635/
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
Spring MVC @RequestMapping ... using method name as action value?
提问by mmm
Say I have this:
说我有这个:
@RequestMapping(value="/hello")
public ModelAndView hello(Model model){
System.out.println("HelloWorldAction.sayHello");
return null;
}
Is it possible to skip the value="hello" part, and just have the @RequestMapping annotation and have spring use the method name as the value, similar to this:
是否可以跳过 value="hello" 部分,只需要 @RequestMapping 注释并让 spring 使用方法名称作为值,类似于:
@RequestMapping
public ModelAndView hello(Model model){
System.out.println("HelloWorldAction.sayHello");
return null;
}
Thanks!
谢谢!
===================EDIT=====================
====================编辑======================
Tried this but not working:
试过这个但不起作用:
@Controller
@RequestMapping(value="admin", method=RequestMethod.GET)
public class AdminController {
@RequestMapping
public ResponseEntity<String> hello() {
System.out.println("hellooooooo");
}
}
回答by BasicCoder
Try to add "/*" on the request mapping value of the class
尝试在类的请求映射值上加上“/*”
@Controller
@RequestMapping(value="admin/*")
public class AdminController {
@RequestMapping
public ResponseEntity<String> hello() {
System.out.println("hellooooooo");
}
}
You can go the page http://localhost:8080/website/admin/hello
回答by bugske
It should work if you move the RequestMethod on your specific method:
如果您在特定方法上移动 RequestMethod ,它应该可以工作:
@Controller
@RequestMapping(value="admin")
public class AdminController {
@RequestMapping(method=RequestMethod.GET)
public ResponseEntity<String> hello() {
System.out.println("hellooooooo");
}
}
and access it through http://hostname:port/admin/hello
并通过http://hostname:port/admin/hello访问它
Have a look here: http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/mvc.html#mvc-ann-requestmapping
看看这里:http: //static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/mvc.html#mvc-ann-requestmapping
Good luck
祝你好运