Loading

SpringMVC的数据响应

SpringMVC的数据响应方式

1)页面跳转
1.1 直接返回字符串
1.2 通过ModelAndView对象返回
2) 回写数据
2.1 直接返回字符串
2.2 返回对象或集合

页面跳转返回字符串形式

直接返回字符串:此种方式会将返回的字符串与视图解析器的前后缀拼接后跳转。

转发地址为 /html/index.html

页面跳转-返回ModelAndView对象

@RequestMapping("/quick2")
public ModelAndView quickMethod2(){
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("redirect:index.html");  //重定向属于客户端重新发请求,无法访问 WEB-INF 文件夹
return modelAndView;}

@RequestMapping("/quick3")
public ModelAndView quickMethod3(){
ModelAndView modelAndView = new ModelAndView();modelAndView.setViewName("forward:/WEB-INF/views/index.html");
return modelAndView;}

页面跳转 -向request域存储数据

在进行转发时,往往要向request域中存储数据通过SpringMVC框架注入的request对象setAttribute()方法设置

@RequestMapping("/quick")
public String quickMethod(HttpServletRequest request){
request.setAttribute("name","zhangsan");
return "index";}

通过ModelAndView的addObject()方法设置

@RequestMapping("/quick3")
public ModelAndView quickMethod3(){
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("forward:/WEB-INF/views/index.html");
modelAndView.addObject("name","lisi");
return modelAndView;
}

回写数据-直接返回字符串

将需要回写的字符串直接返回,但此时需要通过@ResponseBody注解告知SpringMVC框架,方法返回的字符串不是跳转是直接在http响应体中返回。

@RequestMapping("/quick5")
@ResponseBodypublic String quickMethod5() throws IOException {
return "hello springMVC!!!";
}

开发中往往要将复杂的java对象转换成json格式的字符串,我们可以使用web阶段学习过的json转换工具jackson进行转换,导入jackson坐标。

 <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-core</artifactId>
      <version>2.13.0</version>
    </dependency>
    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-annotations</artifactId>
      <version>2.13.0</version>
    </dependency>
    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-databind</artifactId>
      <version>2.13.0</version>
    </dependency>

通过jackson转换json格式字符串,回写字符串。

@RequestMapping("/)
@ResponseBody
public String quickMethod7() throws IOException {
User user = new User();user.setUsername("zhangsan");
user.setAge(18);
ObjectMapper objectMapper = new ObjectMapper();
String s = objectMapper.writeValueAsString(user);
return s;
}

回写数据-返回对象或集合

--通过SpringMVC帮助我们对对象或集合进行json字符串的转换并回写,为处理器适配器配置消息转换参数,指定使用jackson进行对象或集合的转换,因此需要在spring-mvc.xml中进行如下配置:

   <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
        <property name="messageConverters">
            <list>
               <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"></bean>
            </list>
        </property>
    </bean>

Controller层

    @RequestMapping(value = "/ddd")
    @ResponseBody
    public userDetail say4(HttpServletResponse response) throws IOException {
        response.setContentType("text/html;charset=utf-8");
        userDetail user = new userDetail();
        user.setAge(18);
        user.setName("欧阳娜娜");

        return  user;

    }

我们可以使用mvc的注解驱动代替上述配置

在SpringMVC的各个组件中,处理器映射器、处理器适配器、视图解析器称为SpringMVC的三大组件。
使用mvc:annotation-driven自动加载RequestMappingHandlerMapping(处理映射器)和
RequestMappingHandlerAdapter(处理适配器),可用在Spring-xml.xml配置文件中使用
mvc:annotation-driven替代注解处理器和适配器的配置。
同时使用mvc:annotation-driven默认底层就会集成jackson进行对象或集合的json格式字符串的转换。

posted @ 2022-03-18 19:47  冰莫莫  阅读(61)  评论(0编辑  收藏  举报