1 @Controller
在SpringMVC中,@Controller用於標記一個類,將其註冊到Spring上下文, 負責處理由DispatcherServlet分發的請求,並將處理結果封裝成一個Model返回給View進行展示。
@Controller註解需要被Spring所認識,還需要在配置文件中增加以下配置:
// 通過該配置,Spring會掃描com.spring.controller包下的所有註解
<context:component-scan base-package="com.spring.controller"/>
2 @RequestMapping
@RequestMapping是將請求url映射到處理邏輯上,可用於類或方法上,該註解有六個屬性:
- value:指定請求的url;
- method:指定請求的method類型, GET、POST等;
- consumes:指定處理請求的提交內容類型(Content-Type),如application/json;
- produces: 指定返回的內容類型,僅當request請求頭中的(Accept)類型中包含該指定類型才返回;
- params: 指定request中必須包含某些參數值是,才讓該方法處理;
- headers: 指定request中必須包含某些指定的header值,才能讓該方法處理請求
@RequestMapping(“/mvc/?/getView”) 支持多種匹配符:
- ?:匹配文件名的一個字符,
- *:匹配文件名的所有字符
- **:匹配多層路徑
@Controller //控制器,類似於struts的Action
@RequestMapping("/mvc/*") // 父request請求url
public class FirstController {
@RequestMapping("/getView")
public String getView(){
return "home";
}
@RequestMapping 標記的處理器方法,可以直接在方法上給定HttpServlet 對象的參數申明(主要包括HttpServletRequest 、HttpServletResponse 和HttpSession),然後在方法體裏面直接使用。
3 @RequestParam
@RequestParam 主要功能是輔助控制層獲取到請求中的業務參數,其形式爲public String getName(@RequestParam("id") String userId)
,另外,@RequestParam能夠將請求中的參數轉換成函數定義中的形參形式。
@RequestParam 通過Request.getParameter() 獲取到的String可直接轉換爲簡單類型,所能處理的Content-Type 爲 application/x-www-form-urlencoded編碼的內容,提交方式GET、POST
;
4 @PathVariable
@PathVariable 將請求URL中的模板變量映射到方法參數上,即具備從URL中獲取動態參數的能力,參數名稱不一致時可參看如下方式:
@RequestMapping(value = "/getAddressByName/{name}", method = RequestMethod.POST)
@ResponseBody
public String getAddressByName(@PathVariable("name") String userName){
if (userName.equalsIgnoreCase("zhangsan")) {
return "shanghai";
} else {
return "anhui";
}
}
5 @RequestBody、@ResponseBody
@RequestBody 功能是讀取Request請求的body部分數據,使用系統默認配置的HttpMessageConverter進行解析,將數據轉換到形參指定的類型上,與@RequestParam不同,該註解處理的是 非application/x-www-form-urlencoded編碼的內容
。
@ResponseBody 將Controller的方法返回的對象,通過適當的HttpMessageConverter轉換爲指定格式後,寫入到Response對象的body數據區。
@RequestMapping("/setPerson")
@ResponseBody
public Person setPerson(@RequestBody Person person) {
System.out.println(JSON.toJSONString(person));
person.setName("zhangsan");
person.setSex("female");
person.setAddress("上海");
return person;
}
6 @RequestHeader、@CookieValue
@RequestHeader 可以將請求Request中的header部分綁定到方法形參上;
@CookieValue 可以把Request header中關於cookie的值綁定到方法的參數上;
@RequestMapping(value = "/getHeaderAndCookie", method = RequestMethod.POST)
@ResponseBody
public String getHeaderAndCookie(@RequestHeader("Host") String header, @CookieValue("name") String cookie) {
return "header:" + header + ", cookie:" + cookie;
}
7 @ModelAttribute
SpringMVC 使用@ModelAttribute 在不同方法之間通過Model來共享數據,主要有兩種使用方式,一種是標註在方法上,一種是標註在 Controller 方法參數上:
- @ModelAttribute作用在方法上時,表明該方法會向Model添加一個屬性,在當前Controller各方法之間共享,並且會在該Controller的所有@RequestMapping標註的方法執行前調用;
- @ModelAttribute作用在方法參數上時,表明該參數可以在Model中檢索到,並提取屬性值注入到自定義的方法形參上;
注:@ModelAttribute標記的方法會在每個@RequestMapping標記的方法執行前調用,因此需要謹慎使用。
@Controller
public class MyController {
@ModelAttribute("stringValue")
public String setName(){
System.out.println("執行 setName 方法");
return "zhangsan";
}
@ModelAttribute("userAge")
public int setAge(){
System.out.println("執行 setAge 方法");
return 10;
}
@RequestMapping(value = "/getAge", method = RequestMethod.GET)
public void getAge(@ModelAttribute("stringValue") String name, @ModelAttribute("userAge") int age){
System.out.println("name:" + name + ", age:" + age);
}
}
執行結果爲:
執行 setName 方法
執行 setAge 方法
name:zhangsan, age:10
8 @SessionAttributes
@SessionAttributes 只能標記在Controller類上,指明將Model中的哪些屬性是放到session中,可用於在多個請求之間傳遞參數。