SpringMVC註解說明

@controller

通過@controller標註即可將class定義爲一個controller類。


@RequestMapping

value 表示需要匹配的url的格式。
method 表示所需處理請求的http 協議(如get,post,put,delete等),可選值爲RequestMethod這個enum的值。
params 格式爲”paramname=paramvalue” 或 “paramname!=paramvalue”。 表示參數必須等於某值,或者不等於才進入此映射方法。不填寫的時候表明不限制
headers 用來限定對應的reqeust請求的headers中必須包括的內容,例如headers={"Connection=keep-alive"}, 表示請求頭中的connection的值必須爲keep-alive。






@RequestParam

value 對應表單name空間的值
required 是否允許爲空
defaultValue 默認值

@PathVariable

獲得地址欄中傳的參數 例如:

[java] view plain copy
 print?
  1. @RequestMapping(value="/{groupId}.do")  
  2. public void detail(@PathVariable long groupId){  
  3.     groupRepository.selectOne(groupId);  
  4. }  

@RequestBody

在參數之前加入@RequestBody註解。用來將指定的客戶端發送過來的請求參數的數據格式轉換成Java實體

[java] view plain copy
 print?
  1. @RequestMapping(value = "/xxxxx.do")  
  2. public void create(@RequestBody() String host){  
  3.     System.out.println("-----------" + host);  
  4. }  

@RequestHeader

在參數之前加入@RequestHeader註解。用來將指定的請求頭信息影射爲方法的參數。

[java] view plain copy
 print?
  1. @RequestMapping(value = "/xxxxx.do")  
  2. public void create(@RequestHeader() MultiValueMap<String, String> host){  
  3.     System.out.println("-----------" + host);  
  4. }  

@ResponseBody

如果這個方法定義了@ResponseBody註解。那麼會把返回值轉換成這個數據格式,輸出給客戶端

[java] view plain copy
 print?
  1. @RequestMapping(value = "/xxx.do")  
  2. @ResponseBody  
  3. public MultiValueMap<String, String> create(@RequestHeader() MultiValueMap<String, String> hosts) throws Exception {  
  4.     return hosts;  
  5. }  
@ResponseStatus
返回一個指定的http response狀態碼。

[java] view plain copy
 print?
  1. @ResponseStatus(reason="no reason",value=HttpStatus.BAD_REQUEST)  
  2. @RequestMapping("/responsestatus")  
  3. public void responseStatusTest(){  
  4.     
  5. }  


@SessionAttributes

寫在類級別的註解,定義一個session attributes,屬性名字爲SessionAttributes指定。可以指定多個(數組),也同時可以指定類型。

[java] view plain copy
 print?
  1. @Controller  
  2. @SessionAttributes( { "user" })  
  3. @RequestMapping("/test")  
  4. public class ControllerTest {  
  5.   @RequestMapping("/session")  
  6.   @ResponseBody  
  7.   public String sessionIn(@ModelAttribute("user") User user) {  
  8.   return "index";  
  9.   }   
  10. }  

@CookieValue

[java] view plain copy
 print?
  1. @RequestMapping("/cookie")  
  2. @ResponseBody  
  3. public String cookie(@CookieValue("JSESSIONID") String sessionId) {  
  4. return sessionId;  
  5. }  


@InitBinder 

在controller中註冊一個customer protperty editor以解析request中的參數並通過date bind機制與handler method中的參數做綁定。

[java] view plain copy
 print?
  1. @InitBinder  
  2. public void initBinder(WebDataBinder binder) {  
  3.    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");  
  4.    dateFormat.setLenient(false);  
  5.    binder.registerCustomEditor(Date.classnew CustomDateEditor(  
  6.           dateFormat, false));  
  7. }  

[java] view plain copy
 print?
  1. @RequestMapping("/databind1")  
  2.  public ModelAndView databind1(Date date) {  
  3.    …     
  4. }  

訪問url http://localhost:8080/springmvc/databind1.action?date=2000-01-02
通過initbinder中註冊的customeDateEditor類型,自動將2000-01-02轉換爲日期類型


@ExceptionHandler

[java] view plain copy
 print?
  1. @RequestMapping("/exception")  
  2.  public void ExceptionTest() throws Exception{  
  3.     throw new Exception("i don't know");  
  4.  }    
  5.  @ExceptionHandler  
  6.  public String handleException(Exception e,HttpServletRequest request){  
  7.     System.out.println(e.getMessage());  
  8.     return "helloworld";  
  9.  }  
發佈了79 篇原創文章 · 獲贊 34 · 訪問量 34萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章