# 6 Spring MVC响应输出结果

  • @ResponseBody - 产生响应文本
  • ModelAndView - 利用模板引擎渲染输出

# 导航

回到Spring MVC导航页

# @ResponseBody

  • @ResponseBody直接产生响应体的数据集,过程不设计任何视图
  • @ResponseBody可产生标准字符串,例如:JSON、XML等格式数据
  • @ResponseBody被StringHttpMessageConverter所影响

# ModelAndView

  • ModelAndView对象是指"模型(数据)与视图(界面)"对象
  • 通过ModelAndView可将包含数据对象与模板引擎进行绑定
  • SpringMVC中默认的View是JSP,也可以配置其他模板引擎

# ModelAndView实例代码如下:

view.jsp

<%--
  Created by IntelliJ IDEA.
  User: Torey
  Date: 2021/5/5
  Time: 16:03
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<h2>测试ModelAndView,这是一个视图页面</h2>
<h2>用户名:${u.username}</h2>
</body>
</html>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package com.torey.springmvc.controller;

import com.sun.org.apache.xpath.internal.operations.Mod;
import com.torey.springmvc.dto.UserDto;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.servlet.ModelAndView;

/**
 * @Author http://torey611.gitee.io/li-tao-feng/
 * @Email torey6061@qq.com
 * @Date 2021/5/5 16:00
 * @描述:
 */
@Controller
public class ModelAndViewTestController {
    @GetMapping("/view")
    public ModelAndView viewTest(Integer userId){
        UserDto userDto=new UserDto();
        if(userId==1){
            userDto.setUsername("小明");
        }else {
            userDto.setUsername("小红");
        }
        ModelAndView modelAndView = new ModelAndView("/view.jsp");
        //addObject:在当前的请求中增加一个对象,这个对象的别名为:u,数据为userDto,这样就可以在view.jsp页面中使用数据了
        modelAndView.addObject("u",userDto);
        return modelAndView;
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30

modelAndView

# ModelAndView对象核心用法

  • mav.addObject()方法设置的属性默认存放在当前请求中
  • 默认ModelAndView使用请求转发(forward)至页面
  • 重定向使用new ModelAndView("redirect:/index.jsp")

# 请求转发:forward

默认ModelAndView使用请求转发(forward)至页面,是把当前请求原封不动的传递给jsp页面,浏览器的url地址不会发生变化,jsp页面与Controller是共享的一个request对象

# 重定向:redirect

重定向:redirect是通知浏览器重新建立一个连接,是一个新的请求,所以浏览器的url地址会发生变化,原本放在请求中的数据就会丢失,不会共享request对象

@GetMapping("/view")
    public ModelAndView viewTest(Integer userId){
        UserDto userDto=new UserDto();
        if(userId==1){
            userDto.setUsername("小明");
        }else {
            userDto.setUsername("小红");
        }
//        ModelAndView modelAndView = new ModelAndView("/view.jsp");
        
        ModelAndView modelAndView = new ModelAndView("redirect:/view.jsp");
        //addObject:在当前的请求中增加一个对象,这个对象的别名为:u,数据为userDto,这样就可以在view.jsp页面中使用数据了
        //使用redirect重定向,在view.jsp页面就获取不到u这个对象了
        modelAndView.addObject("u",userDto);
        return modelAndView;
    }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

# 使用重定向redirect的场景

如果请求与页面的关系不大,不需要数据共享,就可以使用重定向redirect,比如注册成功后,跳转到首页

# Controller方法返回String的情况

package com.torey.springmvc.controller;

import com.sun.org.apache.xpath.internal.operations.Mod;
import com.torey.springmvc.dto.UserDto;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.servlet.ModelAndView;

/**
 * @Author http://torey611.gitee.io/li-tao-feng/
 * @Email torey6061@qq.com
 * @Date 2021/5/5 16:00
 * @描述:
 */
@Controller
public class ModelAndViewTestController {
    @GetMapping("/view")
    public ModelAndView viewTest(Integer userId){
        UserDto userDto=new UserDto();
        if(userId==1){
            userDto.setUsername("小明");
        }else {
            userDto.setUsername("小红");
        }
//        ModelAndView modelAndView = new ModelAndView("/view.jsp");

        ModelAndView modelAndView = new ModelAndView("redirect:/view.jsp");
        //addObject:在当前的请求中增加一个对象,这个对象的别名为:u,数据为userDto,这样就可以在view.jsp页面中使用数据了
        //使用redirect重定向,在view.jsp页面就获取不到u这个对象了
        modelAndView.addObject("u",userDto);
        return modelAndView;
    }
    //Controller方法返回String的情况
    //1. 方法被@ResponseBody描述,Spring MVC直接响应String字符串本身
    //2. 方法不存在@ResponseBody,则String MVC处理String指带的视图(页面)
    //ModelMap参数不是必须的
    @GetMapping("/view2")
    public String showView(Integer userId, ModelMap modelMap){
        String view="/view.jsp";
        UserDto userDto=new UserDto();
        if(userId==1){
            userDto.setUsername("小明");
        }else {
            userDto.setUsername("小红");
        }
        modelMap.addAttribute("u",userDto);
        return view;
    }
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51

# 导航,上一页,下一页

5springMVC请求中文乱码问题
7SpringMVC整合Freemarker

# 支持我-微信扫一扫-加入微信公众号

Aseven公众号

# 赞赏作者

赞赏作者

# 种一棵树,最好的时间是十年前,其次是现在

立志用功,如种树然。方其根芽,犹未有干;及其有干,尚未有枝;枝而后叶,叶而后花、实。初种根时,只管栽培灌溉,勿作枝想,勿作实想。悬想何益?何不忘栽培之功,怕没有枝叶花实?