SpringMVC的数据响应

1)页面跳转

直接返回字符串

通过ModelAndView对象返回

复制代码
//方式三(model和view拆开)
    @RequestMapping("/quick4")
    public String save4(Model model){
        model.addAttribute("username","lisi3");
        return "success";
    }

    //方式二
    @RequestMapping("/quick3")
    public ModelAndView save3(ModelAndView modelAndView){
        modelAndView.addObject("username","lisi2");
        modelAndView.setViewName("success");
        return modelAndView;
    }
    
    //方式一
    @RequestMapping("/quick2")
    public ModelAndView save2(){
        /*
        *   Model: 模型 作用为封装数据
        *   View: 视图 作用为展示数据
        * */
        ModelAndView modelAndView = new ModelAndView();
        //设置模型数据
        modelAndView.addObject("username","lisi");
        //设置视图名称
        modelAndView.setViewName("success");
        return modelAndView;
    }

2)回写数据

直接返回字符串

Web基础阶段,客户端访问服务器端,如果想直接回写字符串作为响应体返回的话,只需要使用response.getWriter0).print("hello world")即可,那么在Controller中想直接回写字符串该怎样呢?

① 通过SpringMVC框架注入的response对象,使用response.getWriter0.print("hello world")回写数

据,此时不需要视图跳转,业务方法返回值为void。

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

复制代码
//回写数据(解耦)
    @RequestMapping("/quick7")
    @ResponseBody//告诉SPring框架, 不进行视图跳转, 直接进行数据响应
    public String save7() throws IOException {
        return "hello,SpringMVC";
    }

    //回写数据
    @RequestMapping("/quick6")
    public void save6(HttpServletResponse response) throws IOException {
        response.getWriter().print("hello,SpringMVC");
    }

回写json格式字符串

复制代码
@RequestMapping("/quick8")
    @ResponseBody//告诉SPring框架, 不进行视图跳转, 直接进行数据响应
    public String save8() throws IOException {
        User user = new User();
        user.setUsername("zhangsan");
        user.setAge(18);
        //使用json转换工具将对象转换成json字符串在进行返回
        ObjectMapper objectMapper = new ObjectMapper();
        String json = objectMapper.writeValueAsString(user);
        return json;
        
        
        //手写json不切实际
        //return "{\"username\":\"zhangsan\",\"age\":18}";
    }

//简写
@RequestMapping("/quick9")
    @ResponseBody//告诉SPring框架, 不进行视图跳转, 直接进行数据响应
    public User save9() throws IOException {
        User user = new User();
        user.setUsername("zhangsan");
        user.setAge(18);
        return user;
    }



<!--    配置处理器映射器-->
    <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>

返回对象或集合

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

相关推荐
小马爱打代码4 小时前
Spring Boot:模块化实战 - 保持清晰架构
java·spring boot·架构
遇到困难睡大觉哈哈4 小时前
Harmony os 静态卡片(ArkTS + FormLink)详细介绍
前端·microsoft·harmonyos·鸿蒙
青青草原技术员灰太狼4 小时前
Nginx的https搭建
linux·服务器·网络
小坏讲微服务4 小时前
SpringBoot4.0整合knife4j 在线文档完整使用
java·spring cloud·在线文档·knife4j·文档·接口文档·swagger-ui
8***Z894 小时前
springboot 异步操作
java·spring boot·mybatis
i***13245 小时前
Spring BOOT 启动参数
java·spring boot·后端
用户47949283569155 小时前
Bun 卖身 Anthropic!尤雨溪神吐槽:OpenAI 你需要工具链吗?
前端·openai·bun
坚持不懈的大白5 小时前
后端:SpringMVC
java
IT_Octopus5 小时前
(旧)Spring Securit 实现JWT token认证(多平台登录&部分鉴权)
java·后端·spring
kk哥88995 小时前
Spring详解
java·后端·spring