域对象共享数据
框架搭建
web.xml
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<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>springMVC</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>springMVC</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
SpringMVC.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"
xsi:schemaLocation="
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--开启组件扫描-->
<context:component-scan base-package="com.louis.controller"></context:component-scan>
<bean id="ThymeleafViewResolver" class="org.thymeleaf.spring5.view.ThymeleafViewResolver">
<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>
firstPage.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" xmlns:th="http://www.thymeleaf.org">
<title>首页</title>
</head>
<body>
首页
</body>
</html>
success.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" xmlns:th="http://www.thymeleaf.org">
<title>Title</title>
</head>
<body>
success
</body>
</html>
controller
@Controller
public class SetFirstPage {
@RequestMapping("/")
public String testFirstPage(){
return "firstPage";
}
}
域对象共享数据
1、使用servletAPI
firstPage.html
<a th:href="@{/testRequestByServletAPI}">通过servletAPI向request域对象共享数据</a>
ScopeControoler
@Controller
public class ScopeController {
//使用servletAPI向request域对象共享数据
@RequestMapping("/testRequestByServletAPI")
public String testRequestByServletAPI(HttpServletRequest request){
request.setAttribute("testRequestScope", "hello servletAPI");
return "success";
}
}
success.html
<!--操作标签中的文本内容,使用$-->
<p th:text="${testRequestScope}"></p>
2、使用SpringMVC提供的API
①ModelAndView
firstPage.html
<a th:href="@{/testModelAndView}">通过ModelAndView向request域对象共享数据</a>
ScopeController
@RequestMapping("/testModelAndView")
//使用ModelAndView返回值必须是ModelAndView
public ModelAndView testModelAndView(){
ModelAndView mav = new ModelAndView();
//处理模型数据,即向请求域request数据
mav.addObject("testScope", "Hello ModelAndView");
//设置视图名称,相当于平时返回success
mav.setViewName("success");
return mav;
}
success.html
<p th:text="${testScope}"></p>
②Model
firstPage.html
<a th:href="@{/testModel}">通过Model向request域对象共享数据</a>
ScopeController
@RequestMapping("/testModel")
public String testModel(Model model){
model.addAttribute("testScope", "Hello model");
return "success";
}
③map
firstPage.html
<a th:href="@{/testMap}">通过Map向request域对象共享数据</a><br/>
ScopeController
@RequestMapping("/testMap")
public String testMap(Map map){
map.put("testScope", "Hello Map");
return "success";
}
④ModelMap
firstPage.html
<a th:href="@{/testModelMap}">通过ModelMap向request域对象共享数据</a><br/>
ScopeController
@RequestMapping("/testModelMap")
public String testModelMap(ModelMap modelMap){
modelMap.addAttribute("testScope", "Hello ModelMap");
return "success";
}
Model、ModelMap、Map之间的关系
Model
、ModelMap
、Map
类型的参数其实本质上都是BindingAwareModelMap
类型
public interface Model{}
public class ModelMap extendes LinkedHasMap<String, Object>{}
public clsss ExtendedModelMap extends ModeMap implements Model{}
public class BindingAwarModelMap extends ExtendedModelMap{}
3、session域共享数据
firstPage.html
<a th:href="@{/testSession}">通过servletApi向Session域对象共享数据</a><br/>
ScopeController
@RequestMapping("/testSession")
public String testSession(HttpSession session){
session.setAttribute("testSessionScope", "Hello session");
return "success";
}
success.html
<!--在thymeleaf中获取session中数据-->
<p th:text="${session.testSessionScope}"></p>
4、application域共享数据
firstPage.html
<a th:href="@{/testApplication}">通过servletApi向Application域对象共享数据</a><br/>
ScopeController
@RequestMapping("/testApplication")
public String testApplication(HttpSession session){
ServletContext application = session.getServletContext();
application.setAttribute("testApplicationScope", "Hello Application");
return "success";
}
success.html
<p th:text="${application.testApplicationScope}"></p>