好幾天沒有跟進Spring MVC的學習了,之前看了點源碼都忘的差不多了。這次就跟著之前的問題,繼續總結下Spring MVC中的小知識。
url-pattern
如果看過前一篇入門的帖子,應該了解到spring mvc在啟動前必須要在web.xml中配置servlet,這樣才能攔截到想要映射的url地址。
1
2
3
4
5
6
7
8
9
10
|
< servlet > < servlet-name >SpringMVC</ servlet-name > < servlet-class >org.springframework.web.servlet.DispatcherServlet</ servlet-class > < load-on-startup >1</ load-on-startup > </ servlet > < servlet-mapping > < servlet-name >SpringMVC</ servlet-name > < url-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這樣的請求
1
2
3
4
|
@RequestMapping ( "/hello" ) public @ResponseBody String test() { return "hello!" ; } |
基于多個普通的url路徑
RequestMapping可以同時指定多個url,映射到同一個應答邏輯中:
1
2
3
4
5
|
//普通的url路徑映射 @RequestMapping (value={ "/multi1" , "/multi2" , "/test/multi" }) public @ResponseBody String multiUrl() { return "test multi url" ; } |
基于路徑變量的URL映射
這種URL映射可以直接在路徑上指定變量,通過@PathVariable可以獲得對象。
1
2
3
4
5
6
7
8
9
10
11
12
13
|
//基本的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映射
第一種:
1
2
3
4
|
@RequestMapping (value= "/ant1?" ) public @ResponseBody String ant1(){ return "ant1?" ; } |
支持下面風格:
localhost:8080/context/ant12 或者localhost:8080/context/ant1a
第二種:
1
2
3
4
|
@RequestMapping (value= "/ant2*" ) public @ResponseBody String ant2(){ return "ant2*" ; } |
支持下面風格:
localhost:8080/context/ant2aaaa 或者localhost:8080/context/ant2
第三種:
1
2
3
4
|
@RequestMapping (value= "/ant3/*" ) public @ResponseBody String ant3(){ return "ant3/*" ; } |
支持下面風格:
localhost:8080/context/ant3/aaaa 或者localhost:8080/context/ant3/123
第四種:
1
2
3
4
|
@RequestMapping (value= "/ant4/**" ) public @ResponseBody String ant4(){ return "ant4/**" ; } |
支持下面風格
localhost:8080/context/ant4/ 或者localhost:8080/context/ant4/aaa 或者localhost:8080/context/ant4/aaa/123
混用統配和路徑變量
1
2
3
4
5
|
//混用 @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的映射。
1
2
3
4
5
|
//正則表達式 @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
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:http://www.cnblogs.com/xing901022/p/5263702.html