SpringMVC-域物件共享資料
阿新 • • 發佈:2021-11-24
目錄
環境
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>