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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 06:39:18  来源:igfitidea点击:

Spring MVC @RequestMapping ... using method name as action value?

javaspring-mvc

提问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

你可以去页面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

祝你好运