网站友情链接检测,百度开户渠道商哪里找,查看wordpress访问记录,wordpress用户名无效转载自 Spring MVC 基于URL的映射规则#xff08;注解版#xff09; url-pattern 如果看过前一篇入门的帖子#xff0c;应该了解到spring mvc在启动前必须要在web.xml中配置servlet#xff0c;这样才能拦截到想要映射的url地址。
servletservlet-nameSpr…转载自 Spring MVC 基于URL的映射规则注解版 url-pattern 如果看过前一篇入门的帖子应该了解到spring mvc在启动前必须要在web.xml中配置servlet这样才能拦截到想要映射的url地址。
servletservlet-nameSpringMVC/servlet-nameservlet-classorg.springframework.web.servlet.DispatcherServlet/servlet-classload-on-startup1/load-on-startup
/servletservlet-mappingservlet-nameSpringMVC/servlet-nameurl-pattern*.html/url-pattern
/servlet-mapping 其中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 helloname;}RequestMapping(value/user2/{name}/test)public ResponseBody String basicUrl2(PathVariable String name){return hellonametest;}RequestMapping(value/user1/{name}/test/{age})public ResponseBody String basicUrl3(PathVariable String name,PathVariable int age){return helloname ageage;} 基于通配风格的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 anturl 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 开勇学Spring mvc —— 不得不说这个讲的很全 2 URL到Action的映射规则