首頁 > Java > java教程 > 主體

SpringMVC學習系列(3) 之 URL請求到Action的映射規則

黄舟
發布: 2017-03-03 10:49:58
原創
1200 人瀏覽過

在系列(SpringMVC學習系列(SpringMVC學習系列(3) 之 URL請求到Action的映射規則) 之 URL請求到Action的映射規則)中我們展示了一個簡單的get請求,並回傳了一個簡單的helloworld頁面。本篇我們來學習如何來設定一個action的url映射規則。

在系列(SpringMVC學習系列(SpringMVC學習系列(3) 之 URL請求到Action的映射規則) 之 URL請求到Action的映射規則)中我們在HelloWorldController上配置了一個@RequestMapping(value = "/helloworld")這表示對該controller的所有action請求必須是以"/helloworld」開始。

SpringMVC學習系列(SpringMVC學習系列(3) 之 URL請求到Action的映射規則) 之 URL請求到Action的映射規則.URL路徑映射

SpringMVC學習系列(SpringMVC學習系列(3) 之 URL請求到Action的映射規則) 之 URL請求到Action的映射規則.SpringMVC學習系列(SpringMVC學習系列(3) 之 URL請求到Action的映射規則) 之 URL請求到Action的映射規則.對一個action配置多個URL映射:

我們把上一篇中的HelloWorldController的index() action方法的@ RequestMapping更改為@RequestMapping(value={"/index", "/hello"}, method = {RequestMethod.GET}),這表示對該action配置了/index和/hello兩個對應。執行測試,如下:

SpringMVC學習系列(SpringMVC學習系列(3) 之 URL請求到Action的映射規則) 之 URL請求到Action的映射規則

可以看到/helloworld/hello要求也成功匹配。

SpringMVC學習系列(SpringMVC學習系列(3) 之 URL請求到Action的映射規則) 之 URL請求到Action的映射規則.SpringMVC學習系列(SpringMVC學習系列(3) 之 URL請求到Action的映射規則) 之 URL請求到Action的映射規則.URL請求參數映射:

這在查詢的時候常用到,例如我們根據id或編號來取得某筆記錄。

在HelloWorldController新增getDetail的action,程式碼如下:

@RequestMapping(value="/detail/{id}", method = {RequestMethod.GET})public ModelAndView getDetail(@PathVariable(value="id") Integer id){
    
    ModelAndView modelAndView = new ModelAndView();  
    modelAndView.addObject("id", id);  
    modelAndView.setViewName("detail");  
    return modelAndView;
}
登入後複製


其中value="/detail/{id}",中的{id}為佔位符表示可以對應請求為/detail/xxxx 的URL如:/detail/SpringMVC學習系列(SpringMVC學習系列(3) 之 URL請求到Action的映射規則) 之 URL請求到Action的映射規則SpringMVC學習系列(SpringMVC學習系列(3) 之 URL請求到Action的映射規則) 之 URL請求到Action的映射規則SpringMVC學習系列(3) 之 URL請求到Action的映射規則等。

方法的參數@PathVariable(value="id") Integer id 用於將URL中佔位符所對應變數對應到參數id上,@PathVariable(value="id") 中value的值要和占位符/{id}大括號中的值一致。

在views中加入detail.jsp視圖,用於將取得到的id值展示出來。檢視內容如下:

<%@ page language="java" contentType="text/html; charset=UTF-SpringMVC學習系列(3) 之 URL請求到Action的映射規則"
    pageEncoding="UTF-SpringMVC學習系列(3) 之 URL請求到Action的映射規則"%>Insert title here
${id}
登入後複製


執行測試,請求URL位址http://localhost:SpringMVC學習系列(3) 之 URL請求到Action的映射規則0SpringMVC學習系列(3) 之 URL請求到Action的映射規則0/SpringMVCLesson/helloworld/detail/SpringMVC學習系列(SpringMVC學習系列(3) 之 URL請求到Action的映射規則) 之 URL請求到Action的映射規則SpringMVC學習系列(SpringMVC學習系列(3) 之 URL請求到Action的映射規則) 之 URL請求到Action的映射規則SpringMVC學習系列(3) 之 URL請求到Action的映射規則  ,結果如下:

SpringMVC學習系列(SpringMVC學習系列(3) 之 URL請求到Action的映射規則) 之 URL請求到Action的映射規則

可以看到已經正確的顯示了我們請求的id。

SpringMVC學習系列(SpringMVC學習系列(3) 之 URL請求到Action的映射規則) 之 URL請求到Action的映射規則.SpringMVC學習系列(3) 之 URL請求到Action的映射規則.URL通配符映射:

我們也可以透過通配符對URL映射進行配置,通配符有「?」和「*」兩個字元。其中“?”表示SpringMVC學習系列(SpringMVC學習系列(3) 之 URL請求到Action的映射規則) 之 URL請求到Action的映射規則個字符,“*”表示匹配多個字符,“**”表示匹配0個或多個路徑。

例如:

“/helloworld/index?”可以匹配“/helloworld/indexA”、“/helloworld/indexB”,但不能匹配“/helloworld/index”也不能匹配“ /helloworld/indexAA”;

“/helloworld/index*”可以匹配“/helloworld/index”、“/helloworld/indexA”、“/helloworld/indexAA”但不能匹配“/helloworld/index/ A」;

「/helloworld/index/*」可以搭配「/helloworld/index/」、「/helloworld/index/A」、「/helloworld/index/AA」、「/helloworld/index /AB”但不能匹配    “/helloworld/index”、“/helloworld/index/A/B”;

“/helloworld/index/**”可以匹配“/helloworld/index/”下的多有子路徑,例如:“/helloworld/index/A/B/C/D”;

如果現在有“/helloworld/index”和“/helloworld/*”,如果請求位址為“ /helloworld/index」那麼將如何匹配? Spring MVC會按照最長匹配優先原則(即和映射配置中哪個匹配的最多)來匹配,所以會匹配“/helloworld/index”,下面來做測試:

在HelloWorldController添加一個urlTest的action,內容如下:

@RequestMapping(value="/*", method = {RequestMethod.GET})public ModelAndView urlTest(){
    
    ModelAndView modelAndView = new ModelAndView();   
    modelAndView.setViewName("urltest");  
    return modelAndView;
}
登入後複製


在views資料夾中新增加一個視圖urltest.jsp,為了和index.jsp做區別urltest.jsp的內容如下:

<%@ page language="java" contentType="text/html; charset=UTF-SpringMVC學習系列(3) 之 URL請求到Action的映射規則"
    pageEncoding="UTF-SpringMVC學習系列(3) 之 URL請求到Action的映射規則"%>
    
    
    
    Insert title here
    
urlTest!
登入後複製


請求http://localhost:SpringMVC學習系列(3) 之 URL請求到Action的映射規則0SpringMVC學習系列(3) 之 URL請求到Action的映射規則0/SpringMVCLesson/helloworld/index檢視結果:

SpringMVC學習系列(3) 之 URL請求到Action的映射規則

#可以看出映射的是index對應的action。

請http://localhost:SpringMVC學習系列(3) 之 URL請求到Action的映射規則0SpringMVC學習系列(3) 之 URL請求到Action的映射規則0/SpringMVCLesson/helloworld/AAA查看結果:

SpringMVC學習系列(3) 之 URL請求到Action的映射規則

可以看出對應的是urlTest對應的action。

SpringMVC學習系列(SpringMVC學習系列(3) 之 URL請求到Action的映射規則) 之 URL請求到Action的映射規則.SpringMVC學習系列(3) 之 URL請求到Action的映射規則.URL正規表示式對應:

Spring MVC还支持正则表达式方式的映射配置,我们通过一个测试来展示:

在HelloWorldController添加一个regUrlTest的action,内容如下:

@RequestMapping(value="/reg/{name:\\w+}-{age:\\d+}", 
method = {RequestMethod.GET})public ModelAndView regUrlTest(@PathVariable(value="name") String name, 
@PathVariable(value="age") Integer age){
    
    ModelAndView modelAndView = new ModelAndView();   
    modelAndView.addObject("name", name); 
    modelAndView.addObject("age", age); 
    modelAndView.setViewName("regurltest");  
    return modelAndView;
}
登入後複製


在views文件夹中新加一个视图regurltest.jsp内容如下:

<%@ page language="java" contentType="text/html; charset=UTF-SpringMVC學習系列(3) 之 URL請求到Action的映射規則"
    pageEncoding="UTF-SpringMVC學習系列(3) 之 URL請求到Action的映射規則"%>
    
    Insert title here
${name}-${age}
登入後複製


请求//m.sbmmt.com/:SpringMVC學習系列(3) 之 URL請求到Action的映射規則0SpringMVC學習系列(3) 之 URL請求到Action的映射規則0/SpringMVCLesson/helloworld/reg/Hanmeimei-SpringMVC學習系列(SpringMVC學習系列(3) 之 URL請求到Action的映射規則) 之 URL請求到Action的映射規則SpringMVC學習系列(3) 之 URL請求到Action的映射規則查看结果:

SpringMVC學習系列(3) 之 URL請求到Action的映射規則

请求//m.sbmmt.com/:SpringMVC學習系列(3) 之 URL請求到Action的映射規則0SpringMVC學習系列(3) 之 URL請求到Action的映射規則0/SpringMVCLesson/helloworld/reg/Hanmeimei-Lilei查看结果(会发现找不到对应的action返回SpringMVC學習系列(3) 之 URL請求到Action的映射規則0SpringMVC學習系列(3) 之 URL請求到Action的映射規則):

SpringMVC學習系列(3) 之 URL請求到Action的映射規則

SpringMVC學習系列(SpringMVC學習系列(3) 之 URL請求到Action的映射規則) 之 URL請求到Action的映射規則.限制action所接受的请求方式(get或post):

之前我们在HelloWorldController的index() action方法上配置的为@RequestMapping(value="/*", method = {RequestMethod.GET})我们把它改为@RequestMapping(value="/*", method = {RequestMethod.POST})再次请求//m.sbmmt.com/:SpringMVC學習系列(3) 之 URL請求到Action的映射規則0SpringMVC學習系列(3) 之 URL請求到Action的映射規則0/SpringMVCLesson/helloworld/index试一下:

SpringMVC學習系列(3) 之 URL請求到Action的映射規則

这里可以看到结果映射到了urlTest这个action,这是因为我们在urlTest上配置的为@RequestMapping(value="/*", method = {RequestMethod.GET}),当index这个action映射不在符合时便映射到了urlTest。

我们也可以这样配置@RequestMapping(value="/*", method = {RequestMethod.GET, RequestMethod.POST})表示该action可以接受get或post请求,不过更简单的是不对method做配置则默认支持所有请求方式。

SpringMVC學習系列(3) 之 URL請求到Action的映射規則.限制action所接受请求的参数:

我们可以为某个action指定映射的请求中必须包含某参数,或必须不包含某参数,或者某参数必须等于某个值,或者某参数必须不等于某个值这些限制。

SpringMVC學習系列(3) 之 URL請求到Action的映射規則.SpringMVC學習系列(SpringMVC學習系列(3) 之 URL請求到Action的映射規則) 之 URL請求到Action的映射規則.指定映射请求必须包含某参数:

在HelloWorldController添加一个paramsTest的action,内容如下:

@RequestMapping(value="/paramstest", params="example", 
method = {RequestMethod.GET})public ModelAndView paramsTest(){
    
    ModelAndView modelAndView = new ModelAndView();   
    modelAndView.setViewName("paramstest");  
    return modelAndView;
}
登入後複製


在views文件夹中新加一个视图paramstest.jsp内容如下:

<%@ page language="java" contentType="text/html; charset=UTF-SpringMVC學習系列(3) 之 URL請求到Action的映射規則"
    pageEncoding="UTF-SpringMVC學習系列(3) 之 URL請求到Action的映射規則"%>Insert title here
paramstest!
登入後複製


请求//m.sbmmt.com/:SpringMVC學習系列(3) 之 URL請求到Action的映射規則0SpringMVC學習系列(3) 之 URL請求到Action的映射規則0/SpringMVCLesson/helloworld/paramstest查看结果:

SpringMVC學習系列(3) 之 URL請求到Action的映射規則

这里可以看到没有找到paramsTest这个action结果还是映射到了urlTest这个action。

请求//m.sbmmt.com/:SpringMVC學習系列(3) 之 URL請求到Action的映射規則0SpringMVC學習系列(3) 之 URL請求到Action的映射規則0/SpringMVCLesson/helloworld/paramstest?example查看结果:

SpringMVC學習系列(3) 之 URL請求到Action的映射規則

这次可以看到请求映射到了paramsTest这个action。

SpringMVC學習系列(3) 之 URL請求到Action的映射規則.SpringMVC學習系列(SpringMVC學習系列(3) 之 URL請求到Action的映射規則) 之 URL請求到Action的映射規則.指定映射请求必须包含某参数:

把刚才添加的paramsTest的@RequestMapping(value="/paramstest", params="example", method = {RequestMethod.GET}) 改为@RequestMapping(value="/paramstest", params="!example", method = {RequestMethod.GET})

重新请求//m.sbmmt.com/:SpringMVC學習系列(3) 之 URL請求到Action的映射規則0SpringMVC學習系列(3) 之 URL請求到Action的映射規則0/SpringMVCLesson/helloworld/paramstest?example查看结果:

SpringMVC學習系列(SpringMVC學習系列(3) 之 URL請求到Action的映射規則) 之 URL請求到Action的映射規則0

可以看到又没有找到paramsTest这个action而映射到了urlTest这个action。

SpringMVC學習系列(3) 之 URL請求到Action的映射規則.SpringMVC學習系列(3) 之 URL請求到Action的映射規則.指定映射请求中或者某参数必须等于某个值:

把刚才添加的paramsTest的@RequestMapping(value="/paramstest", params="example", method = {RequestMethod.GET}) 改为@RequestMapping(value="/paramstest", params="example=AAA", method = {RequestMethod.GET})

請求//m.sbmmt.com/:SpringMVC學習系列(3) 之 URL請求到Action的映射規則0SpringMVC學習系列(3) 之 URL請求到Action的映射規則0/SpringMVCLesson/helloworld/paramstest?example=BBB查看結果:

SpringMVC學習系列(SpringMVC學習系列(3) 之 URL請求到Action的映射規則) 之 URL請求到Action的映射規則SpringMVC學習系列(SpringMVC學習系列(3) 之 URL請求到Action的映射規則) 之 URL請求到Action的映射規則

可以看到沒有找到paramsTest這個action而映射到了urlTest這個action。

請求http://localhost:SpringMVC學習系列(3) 之 URL請求到Action的映射規則0SpringMVC學習系列(3) 之 URL請求到Action的映射規則0/SpringMVCLesson/helloworld/paramstest?example=BBB檢視結果:

SpringMVC學習系列(SpringMVC學習系列(3) 之 URL請求到Action的映射規則) 之 URL請求到Action的映射規則SpringMVC學習系列(SpringMVC學習系列(3) 之 URL請求到Action的映射規則) 之 URL請求到Action的映射規則

這次可以看到請求對應到了paramsTest這個action。

SpringMVC學習系列(3) 之 URL請求到Action的映射規則.SpringMVC學習系列(3) 之 URL請求到Action的映射規則.指定映射請求中或某參數必須等於某個值:

把剛才加的paramsTest的@RequestMapping(value="/paramstest", params="example", method = {RequestMethod.GET}) 改為@RequestMapping(value="/paramstest", params="example!=AAA", method = {RequestMethod.GET})

請http://localhost:SpringMVC學習系列(3) 之 URL請求到Action的映射規則0SpringMVC學習系列(3) 之 URL請求到Action的映射規則0/SpringMVCLesson/helloworld/paramstest?example=AAA查看結果:

SpringMVC學習系列(SpringMVC學習系列(3) 之 URL請求到Action的映射規則) 之 URL請求到Action的映射規則SpringMVC學習系列(3) 之 URL請求到Action的映射規則

可以看到請求映射到了paramsTest這個action。

請求http://localhost:SpringMVC學習系列(3) 之 URL請求到Action的映射規則0SpringMVC學習系列(3) 之 URL請求到Action的映射規則0/SpringMVCLesson/helloworld/paramstest?example=AAA查看結果:

SpringMVC學習系列(SpringMVC學習系列(3) 之 URL請求到Action的映射規則) 之 URL請求到Action的映射規則SpringMVC學習系列(3) 之 URL請求到Action的映射規則

可以看到沒有找到paramsTest這個action而映射到了urlTest這個action。

註:當我們為params指定多個參數時如:params={"exampleSpringMVC學習系列(SpringMVC學習系列(3) 之 URL請求到Action的映射規則) 之 URL請求到Action的映射規則", "exampleSpringMVC學習系列(SpringMVC學習系列(3) 之 URL請求到Action的映射規則) 之 URL請求到Action的映射規則"},表示的是and關係,即兩個參數限制必須同時滿足。

 

SpringMVC學習系列(3) 之 URL請求到Action的映射規則.限制action所接受請求頭參數:

同限制action所接受的請求參數一樣,我們也可以為某個action指定映射的請求頭中必須包含某一參數,或必須不包含某參數,或某參數必須等於某個值,或某參數必須不等於某個值這些限制。

SpringMVC學習系列(3) 之 URL請求到Action的映射規則.SpringMVC學習系列(SpringMVC學習系列(3) 之 URL請求到Action的映射規則) 之 URL請求到Action的映射規則.指定映射請求頭必須包含某個參數:

@RequestMapping(value="/headerTest", headers = "example")。與限制請求參數是一樣的,可以參考上面的範例進行測試。

SpringMVC學習系列(3) 之 URL請求到Action的映射規則.SpringMVC學習系列(SpringMVC學習系列(3) 之 URL請求到Action的映射規則) 之 URL請求到Action的映射規則.指定映射請求頭必須包含某參數:

#@RequestMapping(value="/headerTest", headers = "!example")。與限制請求參數是一樣的,可以參考上面的範例進行測試。

SpringMVC學習系列(3) 之 URL請求到Action的映射規則.SpringMVC學習系列(3) 之 URL請求到Action的映射規則.指定映射請求頭中或某參數必須等於某個值:

@RequestMapping(value="/headerTest", headers = "Accept=text/html")。與限制請求參數是一樣的,可以參考上面的範例進行測試。

SpringMVC學習系列(3) 之 URL請求到Action的映射規則.SpringMVC學習系列(3) 之 URL請求到Action的映射規則.指定映射請求頭中或某參數必須等於某個值:

@RequestMapping(value="/headerTest", headers = "Accept! =text/html")。與限制請求參數是一樣的,可以參考上面的範例進行測試。

註:當我們為headers指定多個參數時如:headers={"exampleSpringMVC學習系列(SpringMVC學習系列(3) 之 URL請求到Action的映射規則) 之 URL請求到Action的映射規則", "exampleSpringMVC學習系列(SpringMVC學習系列(3) 之 URL請求到Action的映射規則) 之 URL請求到Action的映射規則"},表示的是and關係,即兩個參數限制必須同時滿足。

 

 以上就是SpringMVC學習系列(SpringMVC學習系列(3) 之 URL請求到Action的映射規則) 之URL請求到Action的映射規則的內容,更多相關內容請關注PHP中文網(m.sbmmt.com)!


#
相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!