SpringMVC-域对象共享数据

目录

环境

IDE:idea 2020.3
构建工具:maven-3.8.1
服务器:tomcat9
Spring版本:5.3.1

<dependencies>
    <!-- SpringMVC -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>5.3.1</version>
    </dependency>

    <!-- 日志 -->
    <dependency>
        <groupId>ch.qos.logback</groupId>
        <artifactId>logback-classic</artifactId>
        <version>1.2.3</version>
    </dependency>

    <!-- ServletAPI -->
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>3.1.0</version>
        <scope>provided</scope>
    </dependency>

    <!-- Spring5和Thymeleaf整合包 -->
    <dependency>
        <groupId>org.thymeleaf</groupId>
        <artifactId>thymeleaf-spring5</artifactId>
        <version>3.0.12.RELEASE</version>
    </dependency>
</dependencies>
<?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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">

<!--   springMVC.xml -->
    <!-- 自动扫描包 -->
    <context:component-scan base-package="com.fly.controller"/>

    <!-- 配置Thymeleaf视图解析器 -->
    <bean id="viewResolver" class="org.thymeleaf.spring5.view.ThymeleafViewResolver">
        <property name="order" value="1"/>
        <property name="characterEncoding" value="UTF-8"/>
        <property name="templateEngine">
            <bean class="org.thymeleaf.spring5.SpringTemplateEngine">
                <property name="templateResolver">
                     <bean class="org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver">
                    <!-- 视图前缀 -->
                    <property name="prefix" value="/WEB-INF/templates/"/>
                    <!-- 视图后缀 -->
                    <property name="suffix" value=".html"/>
                    <property name="templateMode" value="HTML5"/>
                    <property name="characterEncoding" value="UTF-8" />
                    </bean>
                 </property>
            </bean>
        </property>
    </bean>

</beans>
 <!--  web.xml  -->
    <filter>
        <filter-name>CharacterEncodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
        <init-param>
            <param-name>forceResponseEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>

    <filter-mapping>
        <filter-name>CharacterEncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <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:springMVC.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>

request

ServletAPI

package com.fly.mvc.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;

/**
 * @author 26414
 */
@Controller
public class IndexController {

  @RequestMapping("/")
  public String index() {
    return "index";
  }

  @RequestMapping("/testServlet")
  public String testServlet(HttpServletRequest request) {
    request.setAttribute("testScope","servlet");
    return "hello";
  }

}

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
  <meta charset="UTF-8">
  <title>首页</title>
</head>
<body>
<!--index.html-->
<a th:href="@{/testServlet}">通过ServletAPI向request存放数据</a>
</body>
</html>
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
  <meta charset="UTF-8">
  <title>hello</title>
</head>
<body>
<!--hello.html-->
<h2>hello</h2>
<p th:text="${testScope}"></p>
</body>
</html>

SpringMVC-域对象共享数据

ModelAndView

  /**
   *IndexController
   */
  @RequestMapping("/testModelAndView")
  public ModelAndView testModelAndView(ModelAndView modelAndView) {
    //向请求域共享数据
    modelAndView.addObject("testScope","ModelAndView");
    //设置视图
    modelAndView.setViewName("hello");
    return modelAndView;
  }
<!--index.html-->
<a th:href="@{/testModelAndView}">通过ModelAndView向request存放数据</a>

SpringMVC-域对象共享数据

Model

  /**
   *IndexController
   */
  @RequestMapping("/testModel")
  public String testModel(Model model) {
    model.addAttribute("testScope","model");
    return "hello";
  }
<!--index.html-->
<a th:href="@{/testModel}">通过Model向request存放数据</a><br/>

SpringMVC-域对象共享数据

map

 /**
   *IndexController
   */
  @RequestMapping("/testMap")
  public String testMap(Map<String,Object> map) {
    map.put("testScope","map");
    return "hello";
  }
<!--index.html-->
<a th:href="@{/testMap}">通过Map向request存放数据</a><br/>

SpringMVC-域对象共享数据

ModelMap

 /**
   *IndexController
   */
  @RequestMapping("/testModelMap")
  public String testModelMap(ModelMap map) {
    map.put("testScope","modelMap");
    return "hello";
  }
<!--index.html-->
<a th:href="@{/testModelMap}">通过ModelMap向request存放数据</a><br/>

SpringMVC-域对象共享数据

Model、ModelMap、Map的关系

在控制台打印这三个对象的信息
SpringMVC-域对象共享数据

SpringMVC-域对象共享数据
可以发现这三个对象都是BindingAwareModelMap类型的
SpringMVC-域对象共享数据

SpringMVC-域对象共享数据

SpringMVC-域对象共享数据

SpringMVC-域对象共享数据

SpringMVC-域对象共享数据

SpringMVC-域对象共享数据
在几个方法上打断点,用debug模式启动服务器,访问/testServlet,可以在方法栈中看到前端控制器的方法
SpringMVC-域对象共享数据

SpringMVC-域对象共享数据

SpringMVC-域对象共享数据
F9跳过这个断点,进入下一个断点,可以发现数据被封装到一个ModelAndView上
SpringMVC-域对象共享数据
访问/testModelAndView
SpringMVC-域对象共享数据
访问/testModel
SpringMVC-域对象共享数据
最后得出结论,使用Model、ModelMap、Map最后数据都会封装到ModelAndView上

session

  /**
   *IndexController
   */
  @RequestMapping("/testSession")
  public String testSession(HttpSession session) {
    session.setAttribute("testSessionInfo","session");
    return "hello";
  }
<!--index.html-->
<a th:href="@{/testSession}">通过ServletAPI向session存放数据</a><br/>
<!--hello.html-->
<p th:text="${session.testSessionInfo}"></p>

SpringMVC-域对象共享数据

application

 /**
   *IndexController
   */
  @RequestMapping("/testApplication")
  public String testApplication(HttpSession session) {
    ServletContext application = session.getServletContext();
    application.setAttribute("testApplicationInfo","application");
    return "hello";
  }
<!--index.html-->
<a th:href="@{/testApplication}">通过ServletAPI向application存放数据</a><br/>
<!--hello.html-->
<p th:text="${application.testApplicationInfo}"></p>

SpringMVC-域对象共享数据

上一篇:SpringBoot注解


下一篇:【Canal】数据同步的终极解决方案,阿里巴巴开源的Canal框架当之无愧!!