栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

SpringMVC的数据响应方式

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SpringMVC的数据响应方式

SpringMVC的数据响应方式 页面跳转 1.直接返回字符串形式

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

返回带有前缀的字符串:

转发:forward:/WEB-INF/views/index.jsp重定向:redirect:/index.jsp 2.通过ModelAndView对象返回

package com.study.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;


@Controller("userController")
public class UserController {
    //页面跳转的两种方式
    //直接返回字符串形式
    @RequestMapping("/quick")
    public String save(){
        System.out.println("UserController save running...");
        return "success";
    }

    //返回ModelAndView对象形式
    @RequestMapping("/quick2")
    public ModelAndView save2(){
        
        ModelAndView modelAndView = new ModelAndView();
        //设置模型数据
        modelAndView.addObject("username","save2");
        //设置视图名称
        modelAndView.setViewName("success");
        return modelAndView;
    }

    //返回ModelAndView对象形式(依赖注入)
    @RequestMapping("/quick3")
    public ModelAndView save3(ModelAndView modelAndView){
        //设置模型数据
        modelAndView.addObject("username","save3");
        //设置视图名称
        modelAndView.setViewName("success");
        return modelAndView;
    }

    //返回ModelAndView对象形式(Model和View拆开)
    @RequestMapping("/quick4")
    public String save4(Model model){
        model.addAttribute("username","save4");
        return "success";
    }
    
}
回写数据

注解:@ResponseBody,该方法返回的字符串不是用于页面跳转 1.直接返回字符串

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

    通过SpringMVC框架注入response对象,使用response.getWriter().print() 回写数据,此时不需要试图跳转,业务方法返回值是void。将需要回写的字符串直接返回,但此时需要通过 @ResponseBody注解告知SpringMVC框架,方法返回的字符串不是跳转是直接在http响应体中放回
//回写数据的方式
//直接返回字符串:response.getWriter().print()
@RequestMapping("/quick5")
public void save5(HttpServletResponse response) throws IOException {
    response.getWriter().print("hello save5");
}
//直接返回字符串:使用注解@ResponseBody告知SpringMVC不进行页面跳转,而是进行数据回写
@RequestMapping("/quick6")
@ResponseBody
public String save6() {
    return "hello save6";
}
//回写JSON字符串格式的数据
@RequestMapping("/quick7")
@ResponseBody
public String save7() {
    User user = new User();
    user.setUsername("user1");
    user.setAge(18);
    //使用json转换工具将对象转换成json格式字符串再返回
    String json = JSON.toJSONString(user);
    return json;
}
2.返回对象或集合

    配置处理器适配器后使用 @ResponseBody 注解

    
        
            
                
            
        
    
    

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

    
    

    在SpringMVC的各个组件中,处理器映射器、处理器适配器、视图解析器成为SpringMVC的三大组件。使用 自动加载RequestMappingHandlerMapping(处理器映射器)和RequestMappingHandlerAdapter(处理器适配器),可用在Spring-mvc.xml配置文件中使用替代注解处理器和适配器的配置。

    同时使用默认底层就会集成jackson进行对象或集合的JSON格式字符串的转换。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/755216.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号