博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring MVC 基于URL的映射规则(注解版)
阅读量:6888 次
发布时间:2019-06-27

本文共 3021 字,大约阅读时间需要 10 分钟。

好几天没有跟进Spring MVC的学习了,之前看了点源码都忘的差不多了。这次就跟着之前的问题,继续总结下Spring MVC中的小知识。

关于SpringMVC的小demo可以!

url-pattern

如果看过前一篇入门的帖子,应该了解到spring mvc在启动前必须要在web.xml中配置servlet,这样才能拦截到想要映射的url地址。

SpringMVC
org.springframework.web.servlet.DispatcherServlet
1
SpringMVC
*.html

其中servlet配置了servlet的实现类,而servlet-mapping则定义了spring mvc起作用的url模式,常见的配置有三种:

  • / 这个斜杠,表示拦截所有的url,如/test/test.html
  • /* 这个模式包含/,可以多拦截以*.jsp结尾的url
  • *.xxx 这个拦截固定结尾的url,常见的如*.do*.json等等

RequestMapping()

基于注解风格的Spring MVC就是通过这个方法来定义映射的url的,常使用的方式如下:

基于普通的url

这种是最简单的url映射,可以接收到localhost:8080/contextName/hello这样的请求

@RequestMapping("/hello")    public @ResponseBody String test() {        return "hello!";    }

基于多个普通的url路径

RequestMapping可以同时指定多个url,映射到同一个应答逻辑中:

//普通的url路径映射    @RequestMapping(value={
"/multi1","/multi2","/test/multi"}) public @ResponseBody String multiUrl() { return "test multi url"; }

基于路径变量的URL映射

这种URL映射可以直接在路径上指定变量,通过@PathVariable可以获得对象。

//基本的URL模板映射    @RequestMapping(value="/user1/{name}")    public @ResponseBody String basicUrl1(@PathVariable String name){        return "hello"+name;    }    @RequestMapping(value="/user2/{name}/test")    public @ResponseBody String basicUrl2(@PathVariable String name){        return "hello"+name+"test";    }    @RequestMapping(value="/user1/{name}/test/{age}")    public @ResponseBody String basicUrl3(@PathVariable String name,@PathVariable int age){        return "hello"+name+" age"+age;    }

基于通配风格的url映射

第一种:

@RequestMapping(value="/ant1?")    public @ResponseBody String ant1(){        return "ant1?";    }
  • 支持下面风格:
localhost:8080/context/ant12 或者localhost:8080/context/ant1a

第二种:

@RequestMapping(value="/ant2*")    public @ResponseBody String ant2(){        return "ant2*";    }
  • 支持下面风格:
localhost:8080/context/ant2aaaa 或者localhost:8080/context/ant2

第三种:

@RequestMapping(value="/ant3/*")    public @ResponseBody String ant3(){        return "ant3/*";    }
  • 支持下面风格:
localhost:8080/context/ant3/aaaa 或者localhost:8080/context/ant3/123

第四种:

@RequestMapping(value="/ant4/**")    public @ResponseBody String ant4(){        return "ant4/**";    }
  • 支持下面风格
localhost:8080/context/ant4/ 或者localhost:8080/context/ant4/aaa 或者localhost:8080/context/ant4/aaa/123

混用统配和路径变量

//混用    @RequestMapping(value="/ant5/**/{name}")    public @ResponseBody String ant5(@PathVariable String name){        return "ant+url  "+name;    }

它能匹配

localhost:8080/context/ant5/123 或者localhost:8080/context/ant5/aaa/123 或者localhost:8080/context/ant5/aaa/123/test

最后一个会被当做name值

基于正则的url映射

这个比较有意思,它支持{名称:正则表达式}的写法,以另一种风格限制url的映射。

//正则表达式    @RequestMapping(value="/student/{name:\\w+}-{age:\\d+}")    public @ResponseBody String regUrl(@PathVariable String name,@PathVariable int age){        return "name:"+name+" age:"+age;    }

例如上面的URL就只能匹配如:

localhost:8080/context/student/wangwu-33 或者localhost:8080/context/student/zhao4-22

参考

1  —— 不得不说,这个讲的很全

本文转自博客园xingoo的博客,原文链接:,如需转载请自行联系原博主。
你可能感兴趣的文章
在CentOS 6.5 环境下利用yum搭建LNMP环境
查看>>
Greenplum闰秒故障的分析解决
查看>>
iMatrix平台中组织结构树标签acsTags:tree用法
查看>>
WinForm多线程编程
查看>>
Hyperledger Fabric 客户端开发五
查看>>
spring的参数校验
查看>>
Nginx的URL Rewrite基本指令
查看>>
Properties属性文件操作工具类PropsUtil
查看>>
计算机系统要素 C4
查看>>
Mysql存储引擎
查看>>
每看一次自己写的代码都有一种重写的冲动
查看>>
androidManifest.xml问题
查看>>
升级ubuntu后nginx无法启动
查看>>
inux多线程顺序控制的示例
查看>>
SQLServer 2016安装时的错误:Polybase要求安装Oracle JRE 7更新51或更高版本
查看>>
wkhtmtopdf--高分辨率转HTML成PDF(二)
查看>>
如何优雅的编写Dockerfile
查看>>
调试时显示数据防止乱码
查看>>
logback 日志输出级别设置
查看>>
直接插入法
查看>>