Request URI部分的注解:@PathVariable处理Request Header部分的注解:@RequestHeader,@CookieValue处理Request Body部分的注解:@RequestParam,@RequestBody处理Attribute类型的注解:@sessionAttribute,@ModelAttribute@RequestMapping URI template样式映射时,即url/{param},这时param可以通过@PathVariable注解绑定它传过来的值到方法的参数上 @Controller public class RelativePathUriTemplateController { @RequestMapping("/url/{param}") public void getParams(@PathVariable String param) { //.... } } Request请求的Header部分的值绑定到方法的参数上 Host localhost:8080 Accept text/html,application/xhtml+xml,application/xml;q=0.9 Accept-Language fr,en-gb;q=0.7,en;q=0.3 Accept-Encoding gzip,deflate Accept-Charset ISO-8859-1,utf-8;q=0.7,*;q=0.7 Keep-Alive 300 @RequestMapping("/url") public void displayHeaderInfo(@RequestHeader("Accept-Encoding") String encoding, @RequestHeader("Keep-Alive") long keepAlive) { //... } 把request header部分的 Accept-Encoding的值,绑定到参数encoding上了, Keep-Alive header的值绑定到参数keepAlive上。
RequestHeader中关于cookie的值绑定到方法的参数上@RequestMapping("/url") public void displayHeaderInfo(@CookieValue("JSESSIONID") String cookie) { //... } Content-Type不是application/x-www-form-urlencoded编码的内容,例如application/json,application/xml等通过使用HandlerAdapter配置的HttpMessageConverter来解析data body,然后绑定到相应的Bean上因为配置有FormHttpMessageConverter,所以也可以用来处理application/x-www-form-urlencoded的内容,处理完的结果放在一个MultiValueMap<String,Stirng>里 @RequestMapping(value = "/url", method = RequestMethod.POST) public void handle(@RequestBody String body) throws IOException { //... } Controller的方法返回的对象,通过适当的HttpMessageConverter转换为指定格式的数据写入到Response对象的body数据区 @ResponseBody @RequestMapping("/") public RedirectView root() { return new RedirectView("/index/index.html"); }HttpSession中的Attribute对象的值 @Controller @RequestMapping("/editPet.do") @SessionAttributes("pet") public class EditPetForm { // ... } @RequestMapping之前,为请求绑定需要从后台查询的Model @ModelAttribute public Account addAccount(@RequestParam String number) { return accountManager.findAccount(number); } 这种方式实际的效果就是在调用@RequestMapping的方法之前,为request对象的model里put("account",Account)
Bean上,要绑定的值来源于 @SessionAttributes启用的Attribute对象@ModelAttribute用于方法上时指定的Model对象以上两种情况都没有时,new一个需要绑定的Bean对象,然后把Request中按名称对应的方式把值绑定到Bean中 @RequestMapping(value="/owners/{ownerId}/pets/{petId}/edit", method = RequestMethod.POST) public String PRocessSubmit(@ModelAttribute Pet pet) { } 首先查询 @SessionAttributes有无绑定的Pet对象,若没有则查询@ModelAttribute方法层面上是否绑定了Pet对象,若没有则将URI template中的值按对应的名称绑定到Pet对象的各属性上
转自http://blog.csdn.net/walkerjong/article/details/7946109
新闻热点
疑难解答