05、域对象共享数据
5.1、概述
1、域对象有四种:
pageContext
:指JSP
页面的范围,范围最小。request
:指一次请求范围session
:指一次会话范围,浏览器开启到浏览器关闭的范围application
(ServletContext
):指整个应用范围,整个服务器开启到服务器关闭的范围
2、session
中钝化与活化:
- 钝化:当服务器关闭了,但是浏览器未关闭,说明会话仍在继续,这时存储在
session
中的数据,经过序列化到磁盘上。 - 活化:如果浏览器仍未关闭,且此时服务器重新开启了,此时会将钝化后的文件内容重新读取到
session
中,这个过程叫活化。
3、如何选择域对象?
- 应该选择能够实现功能但范围最小的域对象。
4、配置web.xml
<?xml version="1.0" encoding="UTF-8"?>
<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">
<!--配置SpringMVC的编码过滤器-->
<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>
<!--配置SpringMVC的前端控制器DispatcherServlet-->
<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>
</web-app>
5、配置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/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">
<!--扫描组件-->
<context:component-scan base-package="com.ssm.mvc.controller"></context:component-scan>
<!--配置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>
5.2、向request域对象共享数据
5.2.1、使用ServletAPI向request域对象共享数据
【index.html】
<a th:href="@{/testRequestByServletAPI}">通过ServletAPI向request域对象共享数据</a>
【ScopeController.java】
@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>
5.2.2、使用ModelAndView向request域对象共享数据
1、ModelAndView
有Model
和View
的功能
Model
:主要用于向请求域共享数据View
:主要用于设置视图,实现页面跳转
2、代码演示:
【index.html】
<a th:href="@{/testRequestByModelAndView}">通过ModelAndView向request域对象共享数据</a><br>
【ScopeController.java】
@Controller
public class ScopeController {
@RequestMapping("/testRequestByModelAndView")
public ModelAndView testRequestByModelAndView() {
ModelAndView mav = new ModelAndView();
//处理模型数据,即向请求域request共享数据
mav.addObject("testRequestScope","hello,ModelAndView");
//设置视图名称
mav.setViewName("success");
return mav;
}
}
注意:ModelAndView必须作为返回值
5.2.3、使用Model向request域对象共享数据
【index.html】
<a th:href="@{/testRequestByModel}">通过Model向request域对象共享数据</a><br>
【ScopeController.java】
@Controller
public class ScopeController {
@RequestMapping("/testRequestByModel")
public String testRequestByModel(Model model) {
model.addAttribute("testRequestScope","hello,model");
return "success";
}
}
5.2.4、使用Map向request域对象共享数据
【index.html】
<a th:href="@{/testRequestByMap}">通过Map向request域对象共享数据</a><br>
【ScopeController.java】
@Controller
public class ScopeController {
@RequestMapping("/testRequestByMap")
public String testRequestByMap(Map<String,Object> map) {
map.put("testRequestScope","hello,map");
return "success";
}
}
5.2.5、使用ModelMap向request域对象共享数据
【index.html】
<a th:href="@{/testRequestByModelMap}">通过ModelMap向request域对象共享数据</a><br>
【ScopeController.java】
@Controller
public class ScopeController {
@RequestMapping("/testRequestByModelMap")
public String testRequestByModelMap(ModelMap modelMap) {
modelMap.addAttribute("testRequestScope","hello,ModelMap");
return "success";
}
}
5.2.6、Model、ModelMap、Map的关系
Model
、ModelMap
、Map
类型的参数其实本质上都是 BindingAwareModelMap
类型的
public interface Model{}
public class ModelMap extends LinkedHashMap<String, Object> {}
public class ExtendedModelMap extends ModelMap implements Model {}
public class BindingAwareModelMap extends ExtendedModelMap {}
5.2.7、总结
控制器方法执行之后都会返回统一的ModelAndView
对象
5.3、向session域共享数据
【index.html】
<a th:href="@{/testSession}">通过ServletAPI向session域对象共享数据</a><br>
【ScopeController.java】
@Controller
public class ScopeController {
@RequestMapping("/testSession")
public String testSession(HttpSession session) {
session.setAttribute("testSessionScope","hello,session");
return "success";
}
}
【success.html】
<p th:text="${session.testSessionScope}"></p>
5.4、向application域共享数据
【index.html】
<a th:href="@{/testApplication}">通过ServletAPI向application域对象共享数据</a><br>
【ScopeController.java】
@Controller
public class 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>