当前位置: 首页 > news >正文

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的注解驱动代替上述配置,

相关文章:

  • 13-Leveldb快照原理及其实现
  • 嵌入式工程师多线程编程(四)裸机编程实现多任务调度
  • 2026考研数学张宇武忠祥复习视频课,高数基础班+讲义PDF
  • FreeCAD 使用的是 GNU Lesser General Public License (LGPL) 许可证
  • C# Winform 入门(14)之如何使用线程池
  • Kube Scheduler 可观测性最佳实践
  • 【根据源码分析Vue 组件中 props的实现原理 】
  • TA学习之路——1.5纹理基础
  • 人工智能基础知识详解:从概念到前沿技术与应用
  • RAG中构建个人知识库
  • 第3课:MCP协议接口定义与开发实践
  • 医学图像分割效率大幅提升!U-Net架构升级,助力精度提升5%!
  • iPaaS集成平台使用的最佳实践:开发、测试和生产部署
  • rhcsa第三次作业
  • 解释 Git 的基本概念和使用方式
  • ZLMediaKit部署与配置
  • 【嵌入式系统设计师】知识点:第4章 嵌入式系统软件基础知识
  • 刷题(删除倒数第N个节点、搜索插入位置、二进制求和、求x平方根、爬楼梯)
  • 如何在 Windows 11 上查找计算机的 IP 地址?
  • 理解 Rust 中的 String 分配机制
  • 国内logo设计网站/网络广告营销有哪些
  • 真做视频网站/seo关键词分析表
  • 电商网站开发背景/下载安装百度一下
  • 长白山网站学做管理平台/友博国际个人中心登录
  • 中石化第四建设有限公司网站/天津网站优化公司
  • 企业网站导航代码/制作一个网站需要多少费用