- SSH與SSM學習之SSH整合10擴大session作用範圍
- 一session作用問題
- 二配置filter
- 三源碼下載下傳
SSH與SSM學習之SSH整合10——擴大session作用範圍
一、session作用問題
為了避免使用懶加載時出現no-session問題.需要擴大session的作用範圍
二、配置filter
Spring提供了擴大session作用範圍的過濾器 OpenSessionInViewFilter。
把過濾器配置到 web.xml中
需要注意的是,一定要配置在 Struts2 的核心過濾器之前。其實所有的過濾器都要配置在
Struts2 的核心過濾器之前。
<!-- 配置spring配置檔案位置參數 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!--擴大session作用範圍,任何過濾器一定要配置在struts2的核心過濾器之前-->
<filter>
<filter-name>openSessionInViewFilter</filter-name>
<filter-class>org.springframework.orm.hibernate5.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>openSessionInViewFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
完整的 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_3_1.xsd"
version="3.1">
<!-- 讓spring随web啟動而建立的監聽器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 配置spring配置檔案位置參數 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!--擴大session作用範圍,任何過濾器一定要配置在struts2的核心過濾器之前-->
<filter>
<filter-name>openSessionInViewFilter</filter-name>
<filter-class>org.springframework.orm.hibernate5.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>openSessionInViewFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!--struts2核心過濾器-->
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
三、源碼下載下傳
https://github.com/wimingxxx/ssh_crm