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

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

	@RequestMapping(value="/{groupId}.do")
	public void detail(@PathVariable long groupId){
		groupRepository.selectOne(groupId);
	}

@RequestBody

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

	@RequestMapping(value = "/xxxxx.do")
	public void create(@RequestBody() String host){
		System.out.println("-----------" + host);
	}

@RequestHeader

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

	@RequestMapping(value = "/xxxxx.do")
	public void create(@RequestHeader() MultiValueMap<String, String> host){
		System.out.println("-----------" + host);
	}

@ResponseBody

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

	@RequestMapping(value = "/xxx.do")
	@ResponseBody
	public MultiValueMap<String, String> create(@RequestHeader() MultiValueMap<String, String> hosts) throws Exception {
		return hosts;
	}
@ResponseStatus
返回一個指定的http response狀態碼。

    
    @ResponseStatus(reason="no reason",value=HttpStatus.BAD_REQUEST)
    @RequestMapping("/responsestatus")
    public void responseStatusTest(){
      
    }


@SessionAttributes

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

@Controller
@SessionAttributes( { "user" })
@RequestMapping("/test")
public class ControllerTest {
  @RequestMapping("/session")
  @ResponseBody
  public String sessionIn(@ModelAttribute("user") User user) {
  return "index";
  } 
}

@CookieValue

@RequestMapping("/cookie")
@ResponseBody
public String cookie(@CookieValue("JSESSIONID") String sessionId) {
return sessionId;
}


@InitBinder

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

 
    @InitBinder
    public void initBinder(WebDataBinder binder) {
       SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
       dateFormat.setLenient(false);
       binder.registerCustomEditor(Date.class, new CustomDateEditor(
              dateFormat, false));
    }

   @RequestMapping("/databind1")
    public ModelAndView databind1(Date date) {
      …   
   }

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


@ExceptionHandler

  
   @RequestMapping("/exception")
    public void ExceptionTest() throws Exception{
       throw new Exception("i don't know");
    }  
    @ExceptionHandler
    public String handleException(Exception e,HttpServletRequest request){
       System.out.println(e.getMessage());
       return "helloworld";
    }


本文部分收集於其他博文。如有不全請大家補充,如有錯誤請指正。






發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章