您的位置:首页 > 编程语言 > Java开发

关于Spring MVC的参数绑定

2016-02-12 00:07 639 查看
1、@PathVariable

@RequestMapping("/pets/{petId}") 
  public String test(@PathVariable String
string, Model model)
{     

        
return string;

  }

2、@RequestHeader

@RequestMapping("/pets/{petId}") 
  public String test(@@RequestHeader
String string, Model model)
{     

        
return string;

  }

获取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 
 

3、@CookieValue

        

把Request
header中关于cookie的值绑定到方法的参数上

        

如:

JSESSIONID=415A4AC178C59DACE0B2C9CA727CDD84
 
@RequestMapping("/displayHeaderInfo.do") 
public void
displayHeaderInfo(@CookieValue("JSESSIONID") String
cookie)  { 
}
 

4、@RequestParam

        

简单类型的绑定,通过Request.getParameter()
获取的String可直接转换为简单类型的情况

public String getPata(@RequestParam("test") String
test, ModelMap model) { 
       
return test; 


 
5、@RequestBody

常用来处理Content-Type:
不是application/x-www-form-urlencoded编码的内容,例如application/json,
application/xml等;
        

它是通过使用HandlerAdapter
配置的HttpMessageConverters来解析post data body,然后绑定到相应的bean上的。

因为配置有FormHttpMessageConverter,所以也可以用来处理application/x-www-form-urlencoded的内容,处理完的结果放在一个MultiValueMap里,这种情况在某些特殊需求下使用,详情查看FormHttpMessageConverter api;

@RequestMapping(value = "/something", method =
RequestMethod.PUT) 
public void handle(@RequestBody String body,
Writer writer) throws IOException { 
 
writer.write(body); 

6、@SessionAttributes

        

用来绑定HttpSession中的attribute对象的值

7、@ModelAttribute

 

将request级别的对象,变成session级别的,即,请求结束后会被保存起来,供其它方法使用

 

spring学习之@SessionAttributes详解

http://blog.csdn.net/li_xiao_ming/article/details/8349178

 

http://www.2cto.com/kf/201410/346482.html

hibernate之实体@onetomany和@manytoone双向注解
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: