四、SpringMVC数据响应

1. SpringMVC的数据响应方式

  1. 页面跳转
  • 直接返回字符串
  • 通过ModelAndView对象返回
  1. 回写数据
  • 直接返回字符串
  • 返回对象或集合

2. 页面跳转

2.1 返回字符串形式

直接返回字符串:此种方式会将返回的字符串与视图解析器的前后缀拼接后跳转。
在这里插入图片描述

2.2 返回ModelAndView对象

几种不同方式:

@Controller
public class UserController {

    @RequestMapping("/quickstart5")//不常用
    public String save5(HttpServletRequest request) {
        request.setAttribute("username","数据5");
        return "success";
    }

    @RequestMapping("/quickstart4")
    public String save4(Model model) {
        model.addAttribute("username","数据4");
        return "success";
    }

    @RequestMapping("/quickstart3")
    public ModelAndView save3(ModelAndView modelAndView) {
        //设置Model模型数据
        modelAndView.addObject("username","数据3");
        //设置View视图名称
        modelAndView.setViewName("success");
        return modelAndView;
    }

    @RequestMapping("/quickstart2")
    public ModelAndView save2() {
        /*
            Model:模型 作用封装数据
            View: 视图 作用展示数据
         */
        ModelAndView modelAndView = new ModelAndView();
        //设置Model模型数据
        modelAndView.addObject("username","数据2");
        //设置View视图名称
        modelAndView.setViewName("success");
        return modelAndView;
    }

    @RequestMapping("/quickstart")
    public String save() {
        System.out.println("Controller save running......");
        return "success";
    }

}

3. 回写数据

3.1 直接返回数据串

Web中:response.getWriter().print(“hello”)

Controller中:
在这里插入图片描述
在这里插入图片描述
返回json数据:

@RequestMapping("/quickstart9")
    @ResponseBody
    public String save9() throws IOException {
        User user = new User();
        user.setUsername("tom");
        user.setAge(18);
        //使用json转换工具将对象转换成json格式字符串再返回
        ObjectMapper objectMapper = new ObjectMapper();
        String json = objectMapper.writeValueAsString(user);
        
        return json;
    }

    @RequestMapping("/quickstart8")
    @ResponseBody //告知SpringMVC框架 不进行视图跳转 直接进行数据相应
    public String save8() throws IOException {
        return "{\"username\":\"zhangsan\",\"age\":18}";
    }

3.2 返回对象或集合

配置处理器映射器:

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

Controller:

    @RequestMapping("/quickstart10")
    @ResponseBody
    //期望SpringMVC自动将User转换成json字符串
    public User save10() throws IOException {
        User user = new User();
        user.setUsername("tom");
        user.setAge(18);
        return user;
    }

使用mvc注解驱动代替上述配置:
在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值