从01开始 从01开始
首页
  • 计算机科学导论
  • 数字电路
  • 计算机组成原理

    • 计算机组成原理-北大网课
  • 操作系统
  • Linux
  • Docker
  • 计算机网络
  • 计算机常识
  • Git
  • JavaSE
  • Java高级
  • JavaEE

    • Ant
    • Maven
    • Log4j
    • Junit
    • JDBC
    • XML-JSON
  • JavaWeb

    • 服务器软件
    • Servlet
  • Spring
  • 主流框架

    • Redis
    • Mybatis
    • Lucene
    • Elasticsearch
    • RabbitMQ
    • MyCat
    • Lombok
  • SpringMVC
  • SpringBoot
  • 学习网课的心得
  • 输入法
  • 节假日TodoList
  • 其他
  • 关于本站
  • 网站日记
  • 友人帐
  • 如何搭建一个博客
GitHub (opens new window)

peterjxl

人生如逆旅,我亦是行人
首页
  • 计算机科学导论
  • 数字电路
  • 计算机组成原理

    • 计算机组成原理-北大网课
  • 操作系统
  • Linux
  • Docker
  • 计算机网络
  • 计算机常识
  • Git
  • JavaSE
  • Java高级
  • JavaEE

    • Ant
    • Maven
    • Log4j
    • Junit
    • JDBC
    • XML-JSON
  • JavaWeb

    • 服务器软件
    • Servlet
  • Spring
  • 主流框架

    • Redis
    • Mybatis
    • Lucene
    • Elasticsearch
    • RabbitMQ
    • MyCat
    • Lombok
  • SpringMVC
  • SpringBoot
  • 学习网课的心得
  • 输入法
  • 节假日TodoList
  • 其他
  • 关于本站
  • 网站日记
  • 友人帐
  • 如何搭建一个博客
GitHub (opens new window)
  • JavaSE

  • JavaSenior

  • JavaEE

  • JavaWeb

  • Spring

  • 主流框架

  • SpringMVC

    • SpringMVC基本概念
    • SpringMVC入门案例
    • SpringMVC中的组件介绍
    • RequestMapping注解的作用
    • 请求参数的绑定
    • 自定义类型转换器
    • 使用原生的Servlet-API
    • SpringMVC常用注解
    • 响应数据和结果视图
      • 环境准备
      • 返回字符串
      • 返回值是void
      • 返回值是ModelAndView对象
      • SpringMVC框架提供的转发和重定向
      • ResponseBody响应JSON数据
      • 源码
    • 文件上传
    • 文件上传之跨服务器
    • SpringMVC的异常处理
    • SpringMVC的拦截器
    • SSM整合
    • SpringMVC
  • SpringBoot

  • Java并发

  • Java源码

  • JVM

  • 韩顺平

  • Java
  • Java
  • SpringMVC
2023-05-15
目录

响应数据和结果视图

# 80.响应数据和结果视图

控制器返回的数据类型有很多,不仅仅是字符串,本文就来讲解关于返回值的分类   ‍

# 环境准备

为了将接下来的案例,我们可以将java目录下的代码都删掉,并且将JSP也删掉:

​​

‍

‍

同时,springmvc.xml文件中,类型转换器也可以删掉,此时文件的配置如下:

    <!-- 开启注解扫描 -->
    <context:component-scan base-package="com.peterjxl"/>

    <!-- 视图解析器 -->
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/pages/"/> <!-- 前缀,可以理解为是文件的目录 -->
        <property name="suffix" value=".jsp"/>  <!-- 后缀,可以理解为是文件后缀名 -->
    </bean>

    <!-- 开启SpringMVC注解驱动 -->
    <mvc:annotation-driven/>
</beans>
1
2
3
4
5
6
7
8
9
10
11
12

‍

然后我们新建index.jsp:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
    <head>
        <title>Title</title>
    </head>
    <body>
        <h1>hello world</h1>
    </body>
</html>
1
2
3
4
5
6
7
8
9

‍

webapp/WEB-INF/pages/success.jsp:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
    <head>
        <title>Title</title>
    </head>
    <body>
      <h3>执行成功</h3>
    </body>
</html>

1
2
3
4
5
6
7
8
9
10

‍

‍

‍

# 返回字符串

先来演示下返回字符串的情况。

‍

新建webapp/response.jsp页面:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
    <head>
        <title>Title</title>
    </head>
    <body>
      <a href="user/testString">testString</a>
    </body>
</html>

1
2
3
4
5
6
7
8
9
10

‍

‍

新建控制器类:

@RequestMapping("/testString")
public String testString() {
    System.out.println("testString() is running...");
    return "success";
}
1
2
3
4
5

‍

此时我们重启,并测试,可以看到能正常跳转。

我们在实际开发中,经常遇到的需求是从数据库中查询出数据,然后返回给前端;为了简单,我们这里就不查询数据库,而是模拟

‍

新建User类。注:自行生成其余方法

package com.peterjxl.domain;
import java.io.Serializable;

public class User implements Serializable {
    private String username;
    private String password;
    private Integer age;
}
1
2
3
4
5
6
7
8

‍

‍

修改控制器方法

@RequestMapping("/testString")
public String testString(Model model) {
    System.out.println("testString() is running...");
    // 模拟从数据库中查询出User对象
    User user = new User();
    user.setUsername("王小美");
    user.setPassword("123");
    user.setAge(18);
    model.addAttribute("user", user);
    return "success";
}
1
2
3
4
5
6
7
8
9
10
11

‍

‍

修改success.jsp:在头部设置EL表达式属性为false,然后去除user的属性:

<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<html>
    <head>
        <title>Title</title>
    </head>
    <body>
        <h3>执行成功</h3>
        ${user.username}
        ${user.password}
    </body>
</html>
1
2
3
4
5
6
7
8
9
10
11

‍

测试:

​​

‍

# 返回值是void

‍

新增超链接:

 <a href="user/testVoid">testVoid</a>
1

‍

新增控制器方法:

@RequestMapping("/testVoid")
public void testVoid(Model model) {
    System.out.println("testVoid() is running...");
}
1
2
3
4

‍

当我们没有返回值的时候,直接点击超链接,是会报错404的。虽然方法是执行了(有打印testVoid() is running...​)

​

‍

可以看到其报错 /WEB-INF/pages/user/testVoid.jsp 未找到,也就是说默认值是两个路径拼接起来的 JSP页面。那要怎么解决呢?我们可以用request对象转发请求:

@RequestMapping("/testVoid")
public void testVoid(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    System.out.println("testVoid() is running...");
    request.getRequestDispatcher("/WEB-INF/pages/success.jsp").forward(request, response);
}
1
2
3
4
5

‍

注意,我们自己转发的时候,是不会去帮我们调用视图解析器的,因此得写完整的文件路径。

‍

‍

除此之外,还可以用重定向,此时会重新发一次请求,也就是浏览器会重新请求一个新的路径,此时就视图解析器就起作用了:

@RequestMapping("/testVoid")
public void testVoid(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    System.out.println("testVoid() is running...");
    response.sendRedirect(request.getContextPath() + "/index.jsp");
}
1
2
3
4
5

‍

‍

还有一种情况,方法中直接输出内容给浏览器:

@RequestMapping("/testVoid")
public void testVoid(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    System.out.println("testVoid() is running...");

    // 解决中文乱码
    response.setCharacterEncoding("UTF-8");
    response.setContentType("text/html;charset=UTF-8");
    response.getWriter().println("你好, SpringMVC!");
}
1
2
3
4
5
6
7
8
9

‍

# 返回值是ModelAndView对象

ModelAndView对象是Spring提供的一个对象,其相当于是Model + view。

例如,我们返回字符串的时候,是设置了一个Model,然后返回一个静态资源文件名:

@RequestMapping("/testString")
    public String testString(Model model) {
        System.out.println("testString() is running...");
        // 模拟从数据库中查询出User对象
        User user = new User();
        user.setUsername("王小美");
        user.setPassword("123");
        user.setAge(18);
        model.addAttribute("user", user);
        return "success";
    }
1
2
3
4
5
6
7
8
9
10
11

‍

而使用ModelAndView可以一步到位,在ModelAndView里设置Model,然后再设置静态资源的文件,然后返回ModelAndView对象。

其实返回字符串的时候,其底层也是返回一个ModelAndView对象。

‍

下面我们来演示下:

@RequestMapping("/testModelAndView")
public ModelAndView testModelAndView() {
    System.out.println("testModelAndView() is running...");
    // 模拟从数据库中查询出User对象
    User user = new User();
    user.setUsername("咕噜咕噜滚下山真君");
    user.setPassword("123");
    user.setAge(18);

    ModelAndView mv = new ModelAndView();
    mv.addObject("user", user);
    mv.setViewName("success");
    return mv;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14

‍

ModelAndView中有一个成员变量是ModelMap​类型的,因此存入到ModelAndView后,相当于存入到了ModelMap,部分源码如下:

public class ModelAndView {

    @Nullable
    private ModelMap model;

    public ModelMap getModelMap() {
        if (this.model == null) {
            this.model = new ModelMap();
        }
        return this.model;
    }

    public ModelAndView addObject(String attributeName, Object attributeValue) {
        this.getModelMap().addAttribute(attributeName, attributeValue);
        return this;
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

‍

‍

新建超链接

<a href="user/testModelAndView">ModelAndView</a>
1

‍

重启,访问并测试,可以看到能正常跳转,success.jsp也正常显示了数据

‍

‍

‍

# SpringMVC框架提供的转发和重定向

除了使用原生的Servlet API转发和重定向,SpringMVC也提供了。

新增超链接:

<a href="user/testForward">testForward</a>
<a href="user/testRedirect">testRedirect</a>
1
2

‍

‍

新增两个控制器:注意转发的时候,需要写完整的路径。而重定向的时候,我们没有写完整的项目名,这是因为SpringMVC帮我们加上了。

@RequestMapping("/testForward")
public String testForward() {
    System.out.println("testForward() is running...");
    return "forward:/WEB-INF/pages/success.jsp";
}

@RequestMapping("/testRedirect")
public String testRedirect() {
    System.out.println("testRedirect() is running...");
    return "redirect:/index.jsp";
}
1
2
3
4
5
6
7
8
9
10
11

‍

重启,并测试,可以看到正常跳转。

‍

‍

‍

# ResponseBody响应JSON数据

之前我们都是跳转或转发一个JSP页面给浏览器,但有时候浏览器是发送Ajax请求,此时我们需要响应数据,而不是页面。

‍

# 引入JQuery

为了方便,我们引入JQuery,我们在webapp目录下新建js文件夹,然后放入JQuery.js。读者可以从我的源码中获取。

然后我们在response.jsp文件中引入:

<head>
    <title>Title</title>
    <script src="js/jquery-3.3.1.min.js"></script>
</head>
1
2
3
4

‍

‍

然后我们新增一个按钮,并绑定事件:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
    <head>
        <title>Title</title>
        <script src="js/jquery.min.js"></script>
        <script>
             $(function () {
                $("#btn").click(function () {
                    alert("王小美")
                })
            })
        </script>
    </head>
    <body>
        <button id="btn">发送Ajax</button>
    </body>
</html>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

‍

‍

此时我们重启,并点击按钮,会发现没有弹框;这是因为我们配置了前端控制器,当请求JQuery的资源,也会被拦截。因此我们得配置,不拦截静态资源。

我们在springmvc.xml文件中配置:

<!-- 前端控制器,哪些静态资源不拦截 -->
<mvc:resources mapping="/js/**" location="/js/" />
1
2

‍

后续开发中,我们也可以配置css和图片不被拦截:

<mvc:resources location="/images/" mapping="/images/**"/> <!-- 图片 -->
<mvc:resources location="/css/" mapping="/css/**"/> <!-- 样式 -->
1
2

‍

此时我们重新点击按钮,是会有弹框的。

‍

‍

# 发送Ajax请求

下面我们来配置发送Ajax请求:修改下方法,发送一个JSON的字符串

$(function () {
    $("#btn").click(function () {
        $.ajax({
            url:"user/testAjax",
            contentType:"application/json;charset=UTF-8", // 发送的数据类型
            data: '{"username":"zhangsan","password":"123", "age":18}',
            dataType:"json", // 服务器返回的数据类型
            type:"post",
            success:function (data) {
                alert(data.username + data.age);
            }
        })
    })
})
1
2
3
4
5
6
7
8
9
10
11
12
13
14

‍

新增控制器方法:

@RequestMapping("/testAjax")
public void testAjax(@RequestBody String body) {
    System.out.println("testAjax() is running...");
    System.out.println(body);
}
1
2
3
4
5

‍

此时打印的是:

{"name":"zhangsan","password":"123", "age":18}
1

‍

‍

# 将JSON转换为JavaBean对象

我们想要将发送过来的数据,转换为JavaBean对象,要怎么做呢?其实SpringMVC已经帮我们做好了。

我们先引用Jackson的依赖:用来将对象转换JSON,或者JSON转对象

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.9.0</version>
</dependency>
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-core</artifactId>
    <version>2.9.0</version>
</dependency>
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-annotations</artifactId>
    <version>2.9.0</version>
</dependency>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

‍

‍

然后我们将方法参数改为User对象:

@RequestMapping("/testAjax")
public void testAjax(@RequestBody User user) {
    System.out.println("testAjax() is running...");
    System.out.println(user);
}
1
2
3
4
5

‍

那我们如何响应JSON数据呢?只需加上@Response注解,然后返回一个User对象即可:

@RequestMapping("/testAjax")
@ResponseBody
public User testAjax(@RequestBody User user) {
    System.out.println("testAjax() is running...");
    System.out.println(user);

    // 做响应,模拟查询数据库
    user.setUsername("王小美");
    user.setAge(19);
    return user;
}
1
2
3
4
5
6
7
8
9
10
11

‍

比起之前,自己拿到user对象转换成JSON,方便很多。然后我们进行测试,可以看到IDEA能打印出对象,并且浏览器能正常接受到数据

​

‍

​

# 源码

本项目已将源码上传到GitHub (opens new window)和Gitee (opens new window)上。并且创建了分支demo7,读者可以通过切换分支来查看本文的示例代码。

在GitHub上编辑此页 (opens new window)
上次更新: 2023/5/16 20:28:49
SpringMVC常用注解
文件上传

← SpringMVC常用注解 文件上传→

Theme by Vdoing | Copyright © 2022-2023 粤ICP备2022067627号-1 粤公网安备 44011302003646号
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式