Spring MVC @RequestMapping ...使用方法名称作为动作值?

时间:2012-03-15 11:20:54

标签: java spring-mvc

说我有这个:

@RequestMapping(value="/hello")
public ModelAndView hello(Model model){

    System.out.println("HelloWorldAction.sayHello");
    return null;      
}   

是否可以跳过value =“hello”部分,只需要@RequestMapping注释并让spring使用方法名作为值,类似于:

@RequestMapping
public ModelAndView hello(Model model){

    System.out.println("HelloWorldAction.sayHello");
    return null;      
}

谢谢!

=================== EDIT =====================

试过这个但没有工作:

@Controller
@RequestMapping(value="admin", method=RequestMethod.GET)
public class AdminController {

    @RequestMapping
    public ResponseEntity<String> hello() { 
      System.out.println("hellooooooo");
    }


}

2 个答案:

答案 0 :(得分:4)

尝试在类

的请求映射值上添加“/ *”
@Controller
@RequestMapping(value="admin/*")
public class AdminController {

    @RequestMapping
    public ResponseEntity<String> hello() { 
      System.out.println("hellooooooo");
    }
}

您可以转到页面http://localhost:8080/website/admin/hello

答案 1 :(得分:3)

如果您根据特定方法移动RequestMethod,它应该有效:

@Controller
@RequestMapping(value="admin")
public class AdminController {

    @RequestMapping(method=RequestMethod.GET)
    public ResponseEntity<String> hello() { 
      System.out.println("hellooooooo");
    }
}

并通过http://hostname:port/admin/hello

访问它

看看这里:http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/mvc.html#mvc-ann-requestmapping

祝你好运