標籤:requestmapping pathvariable requestparam requestheader cookievalue
1.1. @RequestMapping映射請求
SpringMVC 使用 @RequestMapping 註解為控制器指定可以處理那些URL 請求
@requestMapping 可以定義在 類 和 方法 上
package com.ibigsea.springmvc.helloworld;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;@Controllerpublic class HelloWorld {/** * 配置@RequestMapping 攔截 localhost:8080/springmvc/hello 請求 * @return */@RequestMapping("/hello")public String helloWorld() {System.out.println("hello world");return "helloworld";}}
package com.ibigsea.springmvc.helloworld;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;@Controller@RequestMapping("/hello")public class HelloWorld {/** * 配置@RequestMapping 攔截 localhost:8080/springmvc/hello/world 請求 * @return */@RequestMapping("/world")public String helloWorld(){System.out.println("hello world");return "helloworld";}}
@RequestMapping
– 類定義處:提供初步的請求映射資訊。相對於 WEB 應用的根目錄
– 方法處:提供進一步的細分映射資訊。相對於類定義處的 URL。若
類定義處未標註 @RequestMapping,則方法處標記的 URL 相對於
WEB 應用的根目錄
DispatcherServlet 截獲請求後,就通過控制器上
@RequestMapping 提供的映射資訊確定請求所對應的處理方法。
@RequestMapping 除了可以使用請求 URL 映射請求外,
還可以使用要求方法、請求參數及要求標頭映射請求
1.2. @RequestMapping限定要求方法、請求參數、要求標頭
/** * 接收GET請求 * @return */@RequestMapping(value="/get",method = RequestMethod.GET)public String get(){System.out.println("get");return "get";}/** * 接收POST請求 * @return */@RequestMapping(value="/post",method = RequestMethod.POST)public String post(){System.out.println("post");return "post";}/** * 只接收 name 參數 * @return */@RequestMapping(value="/params",params="name")public String params(String name){System.out.println("hello "+name);return "helloworld";}/** * 只接收要求標頭中 Content-Type 為 text/html;charset=UTF-8的請求 * @return */@RequestMapping(value="/headers",headers="Content-Type:text/html;charset=UTF-8")public String headers(){System.out.println("headers");return "helloworld";}
1.3. @RequestMapping匹配符
– ?:匹配檔案名稱中的一個字元
– *:匹配檔案名稱中的任一字元
– **:** 匹配多層路徑
執行個體:
URL : /user/*/create
-- /user/bigsea/create 、 /user/sea/create 等URL
URL : /user/**/create
-- /user/big/sea/create 、 /user/sea/big/create 等URL
URL : /user/create??
-- /user/createaa 、/user/createbb
1.4. @PathVariable 註解
帶預留位置的 URL 是 Spring3.0 新增的功能,該功能在SpringMVC 向 REST 目標挺進發展過程中具有裡程碑的意義
通過 @PathVariable 可以將 URL 中預留位置參數綁定到控制器處理方法的入參中:URL 中的 {xxx} 預留位置可以通過@PathVariable("xxx") 綁定到操作方法的入參中。
/** * localhost:8080/springmvc/hello/pathVariable/bigsea * localhost:8080/springmvc/hello/pathVariable/sea * 這些URL 都會 執行此方法 並且將 <b>bigsea</b>、<b>sea</b> 作為參數 傳遞到name欄位 * @param name * @return */@RequestMapping("/pathVariable/{name}")public String pathVariable(@PathVariable("name")String name){System.out.println("hello "+name);return "helloworld";}
JSP(這裡指定全路徑):
<h1>pathVariable</h1><a href="${pageContext.request.contextPath}/hello/pathVariable/bigsea" > name is bigsea </a><br/><a href="${pageContext.request.contextPath}/hello/pathVariable/sea" > name is sea</a><br/>
運行結果:
hello bigseahello sea
1.5. @RequestParam 綁定請求參數
在處理方法入參處使用 @RequestParam 可以把請求參數傳遞給要求方法
– value:參數名
– required:是否必須。預設為 true, 表示請求參數中必須包含對應的參數,若不存在,將拋出異常
/** * 如果 required = true 則表示請求參數對應的 欄位 必須存在.如果不存在則會拋出異常<br/> * @param firstName 可以為null * @param lastName 不能為null .為null報異常 * @param age age欄位表示如果沒有 age 參數 則預設值為 0 * @return */@RequestMapping("/requestParam")public String requestParam(@RequestParam(value="firstName",required=false)String firstName,@RequestParam( value="lastName" ,required = true) String lastName,@RequestParam(value="age",required = false ,defaultValue="0")int age) {System.out.println("hello my name is " + (firstName == null ? "" : firstName)+ lastName + "," + age +" years old this year");return "helloworld";}
Jsp:
<a href="requestParam?firstName=big&lastName=sea" > name is bigsea , age is 0 </a><br/><a href="requestParam?lastName=sea&age=23" > name is sea , age is 23 </a><br/><a href="requestParam" > throws exception </a>
運行結果:
hello my name is bigsea,0 years old this yearhello my name is sea,23 years old this year
1.6. @RequestHeader 擷取要求標頭
要求標頭包含了若干個屬性,伺服器可據此獲知用戶端的資訊,通過 @RequestHeader 即可將求頭中的屬性值綁定到處理方法的入參中
/** * 擷取要求標頭中的資訊 * @RequestHeader 也有 value ,required ,defaultValue 三個參數 * @param userAgent * @param cookie * @return */@RequestMapping("/requestHeader")public String requestHeader(@RequestHeader("User-Agent")String userAgent,@RequestHeader("Cookie")String cookie){System.out.println("userAgent:["+userAgent+"]");System.out.println("cookie:["+cookie+"]");return "helloworld";}JSP:
<a href="requestHeader" > requestHeader </a>
運行結果:
userAgent:[Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2383.0 Safari/537.36]cookie:[JSESSIONID=DA3B15F559349EA2C3F08BE772FCAFD8]
1.7. @CookieValue 擷取 cookie值
/** * 使用@CookieValue 綁定cookie值<br/> * 註解@CookieValue 也有 value ,required ,defaultValue 三個參數 * @param session * @return */public String cookieValue(@CookieValue(value = "JSESSIONID", required= false)String session){System.out.println("JESSIONID:["+session+"]");return "helloworld";}
JSP:
<a href="cookieValue" > cookieValue </a>
運行結果
JESSIONID:[A4196EEDFD829B40CC1975F029A61328]
1.8. 源碼分析
SpringMVC 學習筆記(二) @RequestMapping、@PathVariable等註解