Spring MVC之@RequestMapping基本用法

宁可枝头抱香死,何曾吹落北风中。这篇文章主要讲述Spring MVC之@RequestMapping基本用法相关的知识,希望能为你提供帮助。
 
1、方法级别上的应用:

1 @RequestMapping(value="https://www.songbingjia.com/department") 2 3public String simpleDapatment(){ 4 5system.out.println(""); 6return ("result"); 7 }

 
访问 http://localhost/XXXX/department 时,调用simpledepatment方法。
2、参数绑定
@RequestMapping(value="https://www.songbingjia.com/department")public String findDepartment(@RequestParaam(departmentId) String departmentId){System.out.println("asasadas"); return ("someResult"); }


【Spring MVC之@RequestMapping基本用法】访问http://localhost/XXXX/department?departmentId=22 就可以出发findDepartmentf方法。
3、REST 风格的参数
1 @RequestMapping(value="https://www.songbingjia.com/department/{departmentId}") 2 3 public String find(@PathVariable String departmentId){ 4 5system.out.println("asa"); 6 7return "someRequest"; 8 9 }

 
访问http://localhost/XXXX/department/22 ,@PathVariable 接收REST风格的参数
4、REST 风格的参数2
1 @RequestMapping(value="https://www.songbingjia.com/department/{depatmentId}") 2 3 public String findDepartment(@PathVariable(departmentId) String somedepartmentId) 4 5 { 6 7 syso; 8 9 retrrn "sds"; 10 11 }

 
访问http://localhost/XXXX/department/22 ,不同的是,22被传到departmentId,但在实际的方法findDepartment(@PathVariable(departmentId) String someDepartment)中,
将其绑定为someDepartment ,所以someDepartment 是22.
5、 URL绑定多个ID
1 @RequestMapping(value="https://www.songbingjia.com/user/{userId}/admin/{adminId}") 2 3 public String findPeople( 4 5@PathVariable String userId, 6 7@PathVariable String adminId){ 8 9 Syso("userId+adminId"); 10 11 return "sdfs"; 12 13 }

 
6、支持正则表达式
@RequestMapping(value="https://www.songbingjia.com/{textualPart:[a-z-]+}.{numericPart:[//d]+}") public String regularExpression( @PathVariable String textualPart, @PathVariable String numericPart){System.out.println("Textual part: " + textualPart + ", numeric part: " + numericPart); return "someResult"; }

 
比如如下的URL:/sometext.123,则输出: 
Textual part: sometext, numeric part: 123. 


    推荐阅读