敢说敢作敢为, 无怨无恨无悔。这篇文章主要讲述Spring mvc中@RequestMapping 基本用法相关的知识,希望能为你提供帮助。
java代码
文章图片
- @RequestMapping(value="https://www.songbingjia.com/departments")
- public String simplePattern(){
- System.out.println("simplePattern method was called");
- return "someResult";
- }
则访问http://localhost/xxxx/departments的时候,会调用 simplePattern方法了
2) 参数绑定
Java代码
文章图片
- @RequestMapping(value="https://www.songbingjia.com/departments")
- public String findDepatment(
- @RequestParam("departmentId") String departmentId){
- System.out.println("Find department with ID: " + departmentId);
- return "someResult";
- }
形如这样的访问形式:
/departments?departmentId=23就可以触发访问findDepatment方法了
3 REST风格的参数
Java代码
文章图片
- @RequestMapping(value="https://www.songbingjia.com/departments/{departmentId}")
- public String findDepatment(@PathVariable String departmentId){
- System.out.println("Find department with ID: " + departmentId);
- return "someResult";
- }
形如REST风格的地址访问,比如:
/departments/23,其中用(@PathVariable接收rest风格的参数
4 REST风格的参数绑定形式之2
先看例子,这个有点象之前的:
Java代码
文章图片
- @RequestMapping(value="https://www.songbingjia.com/departments/{departmentId}")
- public String findDepatmentAlternative(
- @PathVariable("departmentId") String someDepartmentId){
- System.out.println("Find department with ID: " + someDepartmentId);
- return "someResult";
- }
这个有点不同,就是接收形如/departments/23的URL访问,把23作为传入的departmetnId,,但是在实际的方法findDepatmentAlternative中,使用
@PathVariable("departmentId") String someDepartmentId,将其绑定为
someDepartmentId,所以这里someDepartmentId为23
5 url中同时绑定多个id
Java代码
文章图片
- @RequestMapping(value="https://www.songbingjia.com/departments/{departmentId}/employees/{employeeId}")
- public String findEmployee(
- @PathVariable String departmentId,
- @PathVariable String employeeId){
- System.out.println("Find employee with ID: " + employeeId +
- " from department: " + departmentId);
- return "someResult";
- }
这个其实也比较好理解了。
6 支持正则表达式
Java代码
文章图片
- @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.
推荐阅读
- Android 7.0 TextView点击事件无效修复方案
- word怎样画图?word画图图文详细教程攻略_Word专区
- excel筛选怎样用?excel2013筛选运用图文详细教程_Excel专区
- 福昕PDF浏览器无法复制文字的处理办法_其它办公
- 福昕PDF浏览器语言设置办法_其它办公
- 福昕PDF浏览器怎样关闭广告?_其它办公
- 福昕PDF浏览器浏览模式切换办法_其它办公
- office 2003精简版下载地址_其它办公
- Word文档怎样改变背景图片?_Word专区