spring mvc 中使用 @RequestMapping 映射請求

我們在HelloWorldController上配置了一個@RequestMapping(value = “/helloworld”)這表示對該controller的所有action請求必須是以”/helloworld”開始。

1.URL路徑映射

1.1.對一個action配置多個URL映射:

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

這裏寫圖片描述

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

1.2.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/123等。

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

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

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
    ${id}
</body>
</html>

運行測試,請求URL地址 http://localhost:8080/SpringMVCLesson/helloworld/detail/123 ,結果如下:

2

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

1.3.URL通配符映射:

我們還可以通過通配符對URL映射進行配置,通配符有“?”和“”兩個字符。其中“?”表示1個字符,“”表示匹配多個字符,“**”表示匹配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-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
    urlTest!
</body>
</html>

請求http://localhost:8080/SpringMVCLesson/helloworld/index查看結果:
這裏寫圖片描述

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

請求http://localhost:8080/SpringMVCLesson/helloworld/AAA查看結果:
這裏寫圖片描述

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

1.4.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-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
    ${name}-${age}
</body>
</html>

請求http://localhost:8080/SpringMVCLesson/helloworld/reg/Hanmeimei-18查看結果:

這裏寫圖片描述

請求http://localhost:8080/SpringMVCLesson/helloworld/reg/Hanmeimei-Lilei查看結果(會發現找不到對應的action返回404):
這裏寫圖片描述

2.限制action所接受的請求方式(get或post):

之前我們在HelloWorldController的index() action方法上配置的爲@RequestMapping(value=”/“, method = {RequestMethod.GET})我們把它改爲@RequestMapping(value=”/“, method = {RequestMethod.POST})再次請求http://localhost:8080/SpringMVCLesson/helloworld/index試一下:
這裏寫圖片描述

這裏可以看到結果映射到了urlTest這個action,這是因爲我們在urlTest上配置的爲@RequestMapping(value=”/*”, method = {RequestMethod.GET}),當index這個action映射不在符合時便映射到了urlTest。

我們也可以這樣配置@RequestMapping(value=”/*”, method = {RequestMethod.GET, RequestMethod.POST})表示該action可以接受get或post請求,不過更簡單的是不對method做配置則默認支持所有請求方式。

3.限制action所接受請求的參數:

我們可以爲某個action指定映射的請求中必須包含某參數,或必須不包含某參數,或者某參數必須等於某個值,或者某參數必須不等於某個值這些限制。

3.1.指定映射請求必須包含某參數:

在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-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
    paramstest!
</body>
</html>

請求http://localhost:8080/SpringMVCLesson/helloworld/paramstest查看結果:

這裏寫圖片描述

這裏可以看到沒有找到paramsTest這個action結果還是映射到了urlTest這個action。

請求http://localhost:8080/SpringMVCLesson/helloworld/paramstest?example查看結果:

這裏寫圖片描述

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

3.2.指定映射請求必須不包含某參數:

把剛纔添加的paramsTest的@RequestMapping(value=”/paramstest”, params=”example”, method = {RequestMethod.GET}) 改爲@RequestMapping(value=”/paramstest”, params=”!example”, method = {RequestMethod.GET})

重新請求http://localhost:8080/SpringMVCLesson/helloworld/paramstest?example查看結果:

這裏寫圖片描述

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

3.3.指定映射請求中或者某參數必須等於某個值:

把剛纔添加的paramsTest的@RequestMapping(value=”/paramstest”, params=”example”, method = {RequestMethod.GET}) 改爲@RequestMapping(value=”/paramstest”, params=”example=AAA”, method = {RequestMethod.GET})

請求http://localhost:8080/SpringMVCLesson/helloworld/paramstest?example=BBB查看結果:
這裏寫圖片描述

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

請求http://localhost:8080/SpringMVCLesson/helloworld/paramstest?example=AAA查看結果:

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

3.4.指定映射請求中或者某參數必須不等於某個值:

把剛纔添加的paramsTest的@RequestMapping(value=”/paramstest”, params=”example”, method = {RequestMethod.GET}) 改爲@RequestMapping(value=”/paramstest”, params=”example!=AAA”, method = {RequestMethod.GET})

請求http://localhost:8080/SpringMVCLesson/helloworld/paramstest?example=BBB查看結果:
這裏寫圖片描述

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

請求http://localhost:8080/SpringMVCLesson/helloworld/paramstest?example=AAA查看結果:

這裏寫圖片描述

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

注:當我們爲params指定多個參數時如:params={“example1”, “example2”},表示的是and關係,即兩個參數限制必須同時滿足。

4.限制action所接受請求頭參數:

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

4.1.指定映射請求頭必須包含某參數:

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

4.2.指定映射請求頭必須不包含某參數:

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

4.3.指定映射請求頭中或者某參數必須等於某個值:

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

4.4.指定映射請求頭中或者某參數必須不等於某個值:

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

注:當我們爲headers指定多個參數時如:headers={“example1”, “example2”},表示的是and關係,即兩個參數限制必須同時滿足。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章