• SpringMVC的数据响应


    文章目录


    一、数据响应方式

    在这里插入图片描述

    二、页面跳转

    1.直接返回字符串

    在这里插入图片描述
    在SpringMVC快速入门已演示:快速入门案例

    2.通过ModelAndView对象返回

    1.方式一

     @RequestMapping(value = "/quick2")
        public ModelAndView save2(){
            ModelAndView modelAndView = new ModelAndView();
            //设置模型数据
            modelAndView.addObject("userName","小飞侠");
            //设置视图名称
            modelAndView.setViewName("success");
            return modelAndView;
    
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    运行测试:
    在这里插入图片描述

    2.方式二

     @RequestMapping(value = "/quick3")
        public ModelAndView save3(ModelAndView modelAndView){
            //设置模型数据
            modelAndView.addObject("userName","小马哥");
            //设置视图名称
            modelAndView.setViewName("success");
            return modelAndView;
    
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    运行测试:
    在这里插入图片描述

    3.方式三

    @RequestMapping(value = "/quick4")
        public String save4(Model model){
            //设置模型数据
          model.addAttribute("userName","马奎斯");
            return "success";
    
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    运行测试:
    在这里插入图片描述

    4.方式四(不常用)

     @RequestMapping(value = "/quick5")
        public String save5(HttpServletRequest hsr){
            //设置模型数据
           hsr.setAttribute("userName","罗西");
            return "success";
    
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    运行测试:
    在这里插入图片描述

    三、回写数据

    1.直接返回字符串

    1.方式一

    在这里插入图片描述

    //回写数据
        @RequestMapping(value = "/quick6")
        public void save6(HttpServletResponse response) throws IOException {
           response.getWriter().write("Hello World!");
    
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    运行测试:
    在这里插入图片描述

    1.方式二

    在这里插入图片描述

     @RequestMapping(value = "/quick7")
        @ResponseBody  //告诉SpringMVC框架不进行视图跳转,直接进行数据响应
        public String save7() throws IOException {
            return "hello World!";
    
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    运行测试:
    在这里插入图片描述

    1.方式三

    先创建一个User实体类

     @RequestMapping(value = "/quick8")
        @ResponseBody  //告诉SpringMVC框架不进行视图跳转,直接进行数据响应
        public String save8() throws IOException {
            return "{\"username\":\"jack\",\"age\":20}";
    
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    运行测试:
    在这里插入图片描述

    1.方式四

    先导入依赖:

     <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-databind</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
      @RequestMapping(value = "/quick9")
        @ResponseBody  //告诉SpringMVC框架不进行视图跳转,直接进行数据响应
        public String save9() throws IOException {
            User u=new User();
            u.setUsername("tom");
            u.setAge(45);
            //使用json转换工具将对象转换为json格式字符串再返回
            ObjectMapper objectMapper = new ObjectMapper();
            String json = objectMapper.writeValueAsString(u);
            return json;
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    运行测试:
    在这里插入图片描述

    2.返回对象和集合

    1. 配置处理器映射器

    在这里插入图片描述

    <!--  配置处理器映射器-->
        <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
            <property name="messageConverters">
                <list>
                    <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"></bean>
                </list>
            </property>
        </bean>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
      @RequestMapping(value = "/quick10")
        @ResponseBody  //告诉SpringMVC框架不进行视图跳转,直接进行数据响应
        public User save10() throws IOException {
            User u=new User();
            u.setUsername("world");
            u.setAge(18);
    
            return u;
    
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    运行测试:
    在这里插入图片描述

    2.注解

    在这里插入图片描述
    mvc注解驱动:

    <!-- mvc注解驱动-->
        <mvc:annotation-driven></mvc:annotation-driven>
    
    • 1
    • 2

    四、详细代码

    1.pom.xml

    <?xml version="1.0" encoding="UTF-8"?>
    
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
      <modelVersion>4.0.0</modelVersion>
    
      <groupId>org.example</groupId>
      <artifactId>SpringMVC-Demo1</artifactId>
      <version>1.0-SNAPSHOT</version>
      <packaging>war</packaging>
    
      <dependencies>
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-context</artifactId>
          <version>5.2.10.RELEASE</version>
        </dependency>
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-web</artifactId>
          <version>5.2.10.RELEASE</version>
        </dependency>
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-webmvc</artifactId>
          <version>5.2.10.RELEASE</version>
        </dependency>
        <dependency>
          <groupId>javax.servlet</groupId>
          <artifactId>javax.servlet-api</artifactId>
          <version>4.0.1</version>
        </dependency>
        <dependency>
          <groupId>javax.servlet.jsp</groupId>
          <artifactId>javax.servlet.jsp-api</artifactId>
          <version>2.3.3</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-databind</artifactId>
          <version>2.9.0</version>
        </dependency>
        <dependency>
          <groupId>com.fasterxml.jackson.core</groupId>
          <artifactId>jackson-annotations</artifactId>
          <version>2.9.0</version>
        </dependency>
      </dependencies>
    </project>
    
    
    • 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
    • 52
    • 53
    • 54
    • 55

    2.web.xml

    <!DOCTYPE web-app PUBLIC
     "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
     "http://java.sun.com/dtd/web-app_2_3.dtd" >
    
    <web-app>
     <!-- 配置SpringMVC前端控制器-->
      <display-name>Archetype Created Web Application</display-name>
      <servlet>
        <servlet-name>DispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
          <param-name>contextConfigLocation</param-name>
          <param-value>classpath:spring-mvc.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
      </servlet>
      <servlet-mapping>
        <servlet-name>DispatcherServlet</servlet-name>
        <url-pattern>/</url-pattern>
      </servlet-mapping>
    </web-app>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    3.spring-mvc.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:context="http://www.springframework.org/schema/context"
           xmlns:http="http://www.springframework.org/schema/c"
           xmlns:mvc="http://www.springframework.org/schema/mvc"
           xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
        <!--Controller的组件扫描-->
        <context:component-scan base-package="com.study.controller"></context:component-scan>
        <!--配置内部资源视图解析器-->
        <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="prefix" value="/jsp/"></property>
            <property name="suffix" value=".jsp"></property>
        </bean>
      <!--  配置处理器映射器-->
        <!--<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
            <property name="messageConverters">
                <list>
                    <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"></bean>
                </list>
            </property>
    
        </bean>-->
       <!-- mvc注解驱动-->
        <mvc:annotation-driven></mvc:annotation-driven>
    
    
    </beans>
    
    • 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

    4.success.jsp

    <%--
      Created by IntelliJ IDEA.
      User: 86166
      Date: 2022/6/26
      Time: 9:42
      To change this template use File | Settings | File Templates.
    --%>
    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <%@ page isELIgnored="false" %>
    <html>
    <head>
        <title>Title</title>
    </head>
    <body>
    <h1>Success.......${userName}</h1>
    </body>
    </html>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    5.User

    package com.study.domain;
    
    public class User {
        private String username;
        private int age;
    
        public String getUsername() {
            return username;
        }
    
        public void setUsername(String username) {
            this.username = username;
        }
    
        public int getAge() {
            return age;
        }
    
        public void setAge(int age) {
            this.age = age;
        }
    
        @Override
        public String toString() {
            return "User{" +
                    "username='" + username + '\'' +
                    ", age=" + age +
                    '}';
        }
    }
    
    
    • 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

    6.UserController

    package com.study.controller;
    
    import com.fasterxml.jackson.databind.ObjectMapper;
    import com.study.domain.User;
    import org.springframework.stereotype.Controller;
    import org.springframework.ui.Model;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.bind.annotation.ResponseBody;
    import org.springframework.web.servlet.ModelAndView;
    
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.io.IOException;
    
    @Controller
    @RequestMapping("/user")
    public class UserController {
    
        @RequestMapping(value = "/quick10")
        @ResponseBody  //告诉SpringMVC框架不进行视图跳转,直接进行数据响应
        public User save10() throws IOException {
            User u=new User();
            u.setUsername("world");
            u.setAge(18);
    
            return u;
    
        }
    
        @RequestMapping(value = "/quick9")
        @ResponseBody  //告诉SpringMVC框架不进行视图跳转,直接进行数据响应
        public String save9() throws IOException {
            User u=new User();
            u.setUsername("tom");
            u.setAge(45);
            //使用json转换工具将对象转换为json格式字符串再返回
            ObjectMapper objectMapper = new ObjectMapper();
            String json = objectMapper.writeValueAsString(u);
            return json;
        }
    
    
        @RequestMapping(value = "/quick8")
        @ResponseBody  //告诉SpringMVC框架不进行视图跳转,直接进行数据响应
        public String save8() throws IOException {
            return "{\"username\":\"jack\",\"age\":20}";
    
        }
    
        @RequestMapping(value = "/quick7")
        @ResponseBody  //告诉SpringMVC框架不进行视图跳转,直接进行数据响应
        public String save7() throws IOException {
            return "hello World!";
    
        }
    
       //回写数据
        @RequestMapping(value = "/quick6")
        public void save6(HttpServletResponse response) throws IOException {
           response.getWriter().write("Hello World!");
    
        }
    
    
        //不常用
        @RequestMapping(value = "/quick5")
        public String save5(HttpServletRequest hsr){
            //设置模型数据
           hsr.setAttribute("userName","罗西");
            return "success";
    
        }
    
        @RequestMapping(value = "/quick4")
        public String save4(Model model){
            //设置模型数据
          model.addAttribute("userName","马奎斯");
            return "success";
    
        }
    
        @RequestMapping(value = "/quick3")
        public ModelAndView save3(ModelAndView modelAndView){
            //设置模型数据
            modelAndView.addObject("userName","小马哥");
            //设置视图名称
            modelAndView.setViewName("success");
            return modelAndView;
    
        }
    
        @RequestMapping(value = "/quick2")
        public ModelAndView save2(){
            ModelAndView modelAndView = new ModelAndView();
            //设置模型数据
            modelAndView.addObject("userName","小飞侠");
            //设置视图名称
            modelAndView.setViewName("success");
            return modelAndView;
    
        }
    
        @RequestMapping(value = "/quick",method = RequestMethod.GET,params = {"userName"})
        public String save(){
            System.out.println("UserController....");
            /*return "success.jsp";*/
            /*return "/jsp/success.jsp";*/
            return "success";
        }
    }
    
    
    • 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
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
  • 相关阅读:
    Day52 前端开发 JS 函数 BOM DOM
    Oracle中sequence(序列)详解
    01背包问题
    元素内容必须由格式正确的字符数据或标记组成
    在Jetson Nano上安装Caffe
    【C语言基础】Chap. 2. 基本概念
    3-《PyTorch深度学习实践》-梯度下降算法
    在外SSH远程连接macOS服务器【cpolar内网穿透】
    【PG】PostgreSQL 预写日志(WAL)、checkpoint、LSN
    YOLO系列目标检测算法-YOLOv7
  • 原文地址:https://blog.csdn.net/qq_43514330/article/details/125496920