1.1. @RequestMapping映射請(qǐng)求
SpringMVC 使用 @RequestMapping 注解為控制器指定可以處理那些URL 請(qǐng)求
@requestMapping 可以定義在 類 和 方法 上 package com.ibigsea.springmvc.helloworld;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class HelloWorld {
/**
* 配置@RequestMapping 攔截 localhost:8080/springmvc/hello 請(qǐng)求
* @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 請(qǐng)求
* @return
*/
@RequestMapping('/world')
public String helloWorld(){
System.out.println('hello world');
return 'helloworld';
}
}
@RequestMapping – 類定義處:提供初步的請(qǐng)求映射信息。相對(duì)于 WEB 應(yīng)用的根目錄 – 方法處:提供進(jìn)一步的細(xì)分映射信息。相對(duì)于類定義處的 URL。若 類定義處未標(biāo)注 @RequestMapping,則方法處標(biāo)記的 URL 相對(duì)于 WEB 應(yīng)用的根目錄 DispatcherServlet 截獲請(qǐng)求后,就通過(guò)控制器上 @RequestMapping 提供的映射信息確定請(qǐng)求所對(duì)應(yīng)的處理方法。
@RequestMapping 除了可以使用請(qǐng)求 URL 映射請(qǐng)求外, 還可以使用請(qǐng)求方法、請(qǐng)求參數(shù)及請(qǐng)求頭映射請(qǐng)求
1.2. @RequestMapping限定請(qǐng)求方法、請(qǐng)求參數(shù)、請(qǐng)求頭/**
* 接收GET請(qǐng)求
* @return
*/
@RequestMapping(value='/get',method = RequestMethod.GET)
public String get(){
System.out.println('get');
return 'get';
}
/**
* 接收POST請(qǐng)求
* @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';
}
/**
* 只接收請(qǐng)求頭中 Content-Type 為 text/html;charset=UTF-8的請(qǐng)求
* @return
*/
@RequestMapping(value='/headers',headers='Content-Type:text/html;charset=UTF-8')
public String headers(){
System.out.println('headers');
return 'helloworld';
} 1.3. @RequestMapping匹配符
– ?:匹配文件名中的一個(gè)字符 – *:匹配文件名中的任意字符 – **:** 匹配多層路徑 實(shí)例: 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 目標(biāo)挺進(jìn)發(fā)展過(guò)程中具有里程碑的意義 通過(guò) @PathVariable 可以將 URL 中占位符參數(shù)綁定到控制器處理方法的入?yún)⒅校?/span>URL 中的 {xxx} 占位符可以通過(guò)@PathVariable('xxx') 綁定到操作方法的入?yún)⒅小?/span> /**
* localhost:8080/springmvc/hello/pathVariable/bigsea
* localhost:8080/springmvc/hello/pathVariable/sea
* 這些URL 都會(huì) 執(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/>
運(yùn)行結(jié)果: hello bigsea
hello sea
1.5. @RequestParam 綁定請(qǐng)求參數(shù)
在處理方法入?yún)⑻幨褂?nbsp;@RequestParam 可以把請(qǐng)求參數(shù)傳遞給請(qǐng)求方法 – value:參數(shù)名 – required:是否必須。默認(rèn)為 true, 表示請(qǐng)求參數(shù)中必須包含對(duì)應(yīng)的參數(shù),若不存在,將拋出異常 /**
* 如果 required = true 則表示請(qǐng)求參數(shù)對(duì)應(yīng)的 字段 必須存在.如果不存在則會(huì)拋出異常<br/>
* @param firstName 可以為null
* @param lastName 不能為null .為null報(bào)異常
* @param age age字段表示如果沒(méi)有 age 參數(shù) 則默認(rèn)值為 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>
運(yùn)行結(jié)果: hello my name is bigsea,0 years old this year
hello my name is sea,23 years old this year 1.6. @RequestHeader 獲取請(qǐng)求頭請(qǐng)求頭包含了若干個(gè)屬性,服務(wù)器可據(jù)此獲知客戶端的信息,通過(guò) @RequestHeader 即可將求頭中的屬性值綁定到處理方法的入?yún)⒅?/p> /**
* 獲取請(qǐng)求頭中的信息
* @RequestHeader 也有 value ,required ,defaultValue 三個(gè)參數(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> 運(yùn)行結(jié)果:
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 三個(gè)參數(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> 運(yùn)行結(jié)果
JESSIONID:[A4196EEDFD829B40CC1975F029A61328]
1.8. 源碼分析
|
|
來(lái)自: Levy_X > 《JAVAWEB學(xué)習(xí)資料》