spring之@ModelAttribute,@RequestBody, @ResponseBody 参数传递的运用详解

@RequestBody

做用: html

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

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

使用时机:app

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

  •     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指定;url

@ResponseBody

做用: spa

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

使用时机:orm

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

@ModelAttribute使用详解

(1)@ModelAttribute注释void返回值的方法 

@Controller  
    public class HelloWorldController {  
  
        @ModelAttribute  
        public void populateModel(@RequestParam String abc, Model model) {  
           model.addAttribute("attributeName", abc);  
        }  
  
        @RequestMapping(value = "/helloWorld")  
        public String helloWorld() {  
           return "helloWorld";  
        }  
    }

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

@RequestMapping(value = "/helloWorld")  
        public String helloWorld(String abc) {  
           return "helloWorld";  
        }

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

@ModelAttribute  
    public Account addAccount(@RequestParam String number) {  
       return accountManager.findAccount(number);  
    }

 这种状况,model属性的名称没有指定,它由返回类型隐含表示,如这个方法返回Account类型,那么这个model属性的名称是account。
    这个例子中model属性名称有返回对象类型隐含表示,model属性对象就是方法的返回值。它无需要特定的参数。

(3)@ModelAttribute(value="")注释返回具体类的方法 

@Controller  
    public class HelloWorldController {  
  
        @ModelAttribute("attributeName")  
        public String addAccount(@RequestParam String abc) {  
           return abc;  
        }  
  
        @RequestMapping(value = "/helloWorld")  
        public String helloWorld() {  
           return "helloWorld";  
        }  
    }

 这个例子中使用@ModelAttribute注释的value属性,来指定model属性的名称。model属性对象就是方法的返回值。它无需要特定的参数。

 

(4)@ModelAttribute和@RequestMapping同时注释一个方法

@Controller  
    public class HelloWorldController {  
  
        @RequestMapping(value = "/helloWorld.do")  
        @ModelAttribute("attributeName")  
        public String helloWorld() {  
           return "hi";  
        }  
    }

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

相关文章
相关标签/搜索