简介:

@RequestBody

作用: 

      i) 该注解用于读取Request请求的body部分数据,使用系统默认配置的HttpMessageConverter进行解析,然后把相应的数据绑定到要返回的对象上;

      ii) 再把HttpMessageConverter返回的对象数据绑定到 controller中方法的参数上。

使用时机:

A) GET、POST方式提时, 根据request header Content-Type的值来判断:

  •     application/x-www-form-urlencoded, 可选(即非必须,因为这种情况的数据@RequestParam, @ModelAttribute也可以处理,当然@RequestBody也能处理);
  •     multipart/form-data, 不能处理(即使用@RequestBody不能处理这种格式的数据);
  •     其他格式, 必须(其他格式包括application/json, application/xml等。这些格式的数据,必须使用@RequestBody来处理);

B) PUT方式提交时, 根据request header Content-Type的值来判断:

  •     application/x-www-form-urlencoded, 必须;
  •     multipart/form-data, 不能处理;
  •     其他格式, 必须;

说明:request的body部分的数据编码格式由header部分的Content-Type指定;

@ResponseBody

作用: 

      该注解用于将Controller的方法返回的对象,通过适当的HttpMessageConverter转换为指定格式后,写入到Response对象的body数据区。

使用时机:

      返回的数据不是html标签的页面,而是其他某种格式的数据时(如json、xml等)使用;

 

 

 

@ModelAttribute使用详解

1.@ModelAttribute注释方法 
    例子(1),(2),(3)类似,被@ModelAttribute注释的方法会在此controller每个方法执行前被执行,因此对于一个controller映射多个URL的用法来说,要谨慎使用。
    
    (1)@ModelAttribute注释void返回值的方法 
    

[java] view plain copy

 

 print?

    1. <span style="font-size:12px;">@Controller  
    2. public class HelloWorldController {  
    3.   
    4. @ModelAttribute  
    5. public void populateModel(@RequestParam String abc, Model model) {  
    6. "attributeName", abc);  
    7.         }  
    8.   
    9. @RequestMapping(value = "/helloWorld")  
    10. public String helloWorld() {  
    11. return "helloWorld";  
    12.         }  
    13.     }</span>  
    14. <span style="font-size:12px;">  
    15. </span>

        这个例子,在获得请求/helloWorld 后,populateModel方法在helloWorld方法之前先被调用,它把请求参数(/helloWorld?abc=text)加入到一个名为attributeName的model属性中,在它执行后helloWorld被调用,返回视图名helloWorld和model已由@ModelAttribute方法生产好了。
    这个例子中model属性名称和model属性对象有model.addAttribute()实现,不过前提是要在方法中加入一个Model类型的参数。
        当URL或者post中不包含次参数时,会报错,其实不需要这个方法,完全可以把请求的方法写成,这样缺少此参数也不会出错

    [java] view plain copy

     

     print?

      1. <span style="font-size:12px;">       @RequestMapping(value = "/helloWorld")  
      2. public String helloWorld(String abc) {  
      3. return "helloWorld";  
      4.         }</span>  
      5. <span style="font-size:12px;">  
      6. </span>


          (2)@ModelAttribute注释返回具体类的方法 
          

      [java] view plain copy

       

       print?

        1. <span style="font-size:12px;">@ModelAttribute  
        2. public Account addAccount(@RequestParam String number) {  
        3. return accountManager.findAccount(number);  
        4.     }  
        5. </span>  
        6. <span style="font-size:12px;">  
        7. </span>

         

            这种情况,model属性的名称没有指定,它由返回类型隐含表示,如这个方法返回Account类型,那么这个model属性的名称是account。
            这个例子中model属性名称有返回对象类型隐含表示,model属性对象就是方法的返回值。它无须要特定的参数。
            
            (3)@ModelAttribute(value="")注释返回具体类的方法 
            

        [java] view plain copy

         

         print?

        1. <span style="font-size:12px;">@Controller  
        2. public class HelloWorldController {  
        3.   
        4. @ModelAttribute("attributeName")  
        5. public String addAccount(@RequestParam String abc) {  
        6. return abc;  
        7.         }  
        8.   
        9. @RequestMapping(value = "/helloWorld")  
        10. public String helloWorld() {  
        11. return "helloWorld";  
        12.         }  
        13.     }</span>  
        14. <span style="font-size:12px;">  
        15. </span>

         

            这个例子中使用@ModelAttribute注释的value属性,来指定model属性的名称。model属性对象就是方法的返回值。它无须要特定的参数。
            
            (4)@ModelAttribute和@RequestMapping同时注释一个方法 
            

        [java] view plain copy

         

         print?

          1. <span style="font-size:12px;">@Controller  
          2. public class HelloWorldController {  
          3.   
          4. @RequestMapping(value = "/helloWorld.do")  
          5. @ModelAttribute("attributeName")  
          6. public String helloWorld() {  
          7. return "hi";  
          8.         }  
          9.     }</span>  
          10. <span style="font-size:12px;">  
          11. </span>

           

              这时这个方法的返回值并不是表示一个视图名称,而是model属性的值,视图名称由RequestToViewNameTranslator根据请求"/helloWorld.do"转换为逻辑视图helloWorld。
              Model属性名称有@ModelAttribute(value=””)指定,相当于在request中封装了key=attributeName,value=hi。
              
          2.@ModelAttribute注释一个方法的参数 

              (1)从model中获取     
              

          [java] view plain copy

           

           print?

          1. <span style="font-size:12px;">@Controller  
          2. public class HelloWorldController {  
          3.   
          4. @ModelAttribute("user")  
          5. public User addAccount() {  
          6. return new User("jz","123");  
          7.         }  
          8.   
          9. @RequestMapping(value = "/helloWorld")  
          10. public String helloWorld(@ModelAttribute("user") User user) {  
          11. "jizhou");  
          12. return "helloWorld";  
          13.         }  
          14.     }</span>  
          15. <span style="font-size:12px;">  
          16. </span>

           

              在这个例子里,@ModelAttribute("user") User user注释方法参数,参数user的值来源于addAccount()方法中的model属性。
              此时如果方法体没有标注@SessionAttributes("user"),那么scope为request,如果标注了,那么scope为session
              
              (2)从Form表单或URL参数中获取(实际上,不做此注释也能拿到user对象) 
              
              

          [java] view plain copy

           

           print?

            1. <span style="font-size:12px;">@Controller  
            2. public class HelloWorldController {  
            3.   
            4. @RequestMapping(value = "/helloWorld")  
            5. public String helloWorld(@ModelAttribute User user) {  
            6. return "helloWorld";  
            7.         }  
            8.     }</span>  
            9. <span style="font-size:12px;">  
            10. </span>