微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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>

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>

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/>

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/>

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/>

Model、ModelMap、Map的关系

在控制台打印这三个对象的信息


可以发现这三个对象都是BindingAwareModelMap类型的


在几个方法上打断点,用debug模式启动服务器,访问/testServlet,可以在方法栈中看到前端控制器的方法


F9跳过这个断点,进入下一个断点,可以发现数据被封装到一个ModelAndView上


访问/testModelAndView


访问/testModel


最后得出结论,使用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>

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>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐