SpringMVC的数据响应(二)

4.6.SpringMVC的数据响应-回写数据-直接回写字符串(应用)

通过SpringMVC框架注入的response对象,使用response.getWriter().print(“hello world”) 回写数据,此时不需要视图跳转,业务方法返回值为void

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

    @RequestMapping(value = "/login6")  //请求地址
    public void login6(HttpServletResponse response) throws IOException {
        response.setContentType("text/html;charset=UTF-8");//设置编码格式
        response.getWriter().print("你好!");
    }

    @RequestMapping(value = "/login7")  //请求地址
    @ResponseBody     //告诉springMVC框架,该方法不进行视图跳转,直接进行数据响应
    public String login7() throws IOException {
        return "hello world";
    }

4.7.SpringMVC的数据响应-回写数据-直接回写json格式字符串(应用)

    @RequestMapping(value = "/login8")  //请求地址
    @ResponseBody     //告诉springMVC框架,该方法不进行视图跳转,直接进行数据响应
    public String login8() throws IOException {
        return "{\"username\":\"NanYu\",\"age\":18}";
    }

手动拼接json格式字符串的方式很麻烦,开发中往往要将复杂的java对象转换成json格式的字符串,我们可以使用web阶段学习过的json转换工具jackson进行转换,通过jackson转换json格式字符串,回写字符串

    @RequestMapping(value = "/login9")  //请求地址
    @ResponseBody     //告诉springMVC框架,该方法不进行视图跳转,直接进行数据响应
    public String login9() throws IOException {
        User user = new User();
        user.setName("zhangsan");
        user.setAge("18");
        //使用json的转换工具将对象转换成json对象或字符串
        ObjectMapper objectMapper = new ObjectMapper();
        String string = objectMapper.writeValueAsString(user);
        return string;
    }

4.8.SpringMVC的数据响应-回写数据-返回对象或集合(应用)

通过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"/>
            </list>
        </property>
    </bean>
 @RequestMapping(value = "/login10")  //请求地址
    @ResponseBody     //告诉springMVC框架,该方法不进行视图跳转,直接进行数据响应
    //期望springMVC自动将User转换成json格式的字符串
    public User login10() throws IOException {
        User user = new User();
        user.setName("zhangsan");
        user.setAge("18");
        return user;
    }

4.9.SpringMVC的数据响应-回写数据-返回对象或集合2(应用)

在方法上添加@ResponseBody就可以返回json格式的字符串,但是这样配置比较麻烦,配置的代码比较多,因此,我们可以使用mvc的注解驱动代替上述配置

开启mvc的自动注解驱动
<mvc:annotation-driven/>

在 SpringMVC 的各个组件中,处理器映射器处理器适配器视图解析器称为 SpringMVC 的三大组件。

使用<mvc:annotation-driven />自动加载 RequestMappingHandlerMapping(处理映射器)和

RequestMappingHandlerAdapter( 处 理 适 配 器 ),可用在Spring-xml.xml配置文件中使用

<mvc:annotation-driven />替代注解处理器和适配器的配置。

同时使用<mvc:annotation-driven />

默认底层就会集成jackson进行对象或集合的json格式字符串的转换

4.10.SpringMVC的数据响应-知识要点小结(理解,记忆)

1) 页面跳转

直接返回字符串

通过ModelAndView对象返回

2) 回写数据

直接返回字符串

HttpServletResponse 对象直接写回数据,HttpServletRequest对象带回数据,Model对象带回数据或者@ResponseBody将字符串数据写回

返回对象或集合

@ResponseBody+<mvc:annotation-driven/>

  • 178
    点赞
  • 168
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 166
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 166
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

楠黎倾风

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值