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 映射請求外,
還可以使用請求方法、請求參數(shù)及請求頭映射請求
1.2. @RequestMapping限定請求方法、請求參數(shù)、請求頭
/** * 接收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 參數(shù) * @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 目標挺進發(fā)展過程中具有里程碑的意義
通過 @PathVariable 可以將 URL 中占位符參數(shù)綁定到控制器處理方法的入參中:URL 中的 {xxx} 占位符可以通過@PathVariable('xxx') 綁定到操作方法的入參中。
/** * localhost:8080/springmvc/hello/pathVariable/bigsea * localhost:8080/springmvc/hello/pathVariable/sea * 這些URL 都會 執(zhí)行此方法 并且將 <b>bigsea</b>、<b>sea</b> 作為參數(shù) 傳遞到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 綁定請求參數(shù)
在處理方法入參處使用 @RequestParam 可以把請求參數(shù)傳遞給請求方法
– value:參數(shù)名
– required:是否必須。默認為 true, 表示請求參數(shù)中必須包含對應的參數(shù),若不存在,將拋出異常
/** * 如果 required = true 則表示請求參數(shù)對應的 字段 必須存在.如果不存在則會拋出異常<br/> * @param firstName 可以為null * @param lastName 不能為null .為null報異常 * @param age age字段表示如果沒有 age 參數(shù) 則默認值為 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 三個參數(shù) * @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 三個參數(shù) * @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. 源碼分析