1. 程式人生 > >s2sh配置技巧----------------------------OpenSessionInViewFilter

s2sh配置技巧----------------------------OpenSessionInViewFilter

OpenSessionInView和你說的業務類沒有什麼關係。OpenSessionInView實際上就是在你每個*.do的請求之前開啟Session,在*.do的整個URL請求執行完畢關閉Session而已,還是仔細檢查一下你的MVC控制邏輯吧。所以吧struts的中央控制器配置在OpensessionInView的後面。如下

OpenSessionInViewFilter 的配置情況(web.xml)

web.xml 整個配置,用於整合ssh框架

 

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
    xmlns="http://java.sun.com/xml/ns/javaee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <display-name></display-name>    
  
  <!-- spring配置檔案位置 -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext-c3p0.xml</param-value>
    </context-param>
    
    <!-- openSessionInView配置 -->
    <filter>
        <filter-name>openSessionInViewFilter</filter-name>
        <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
        <init-param>
            <param-name>singleSession</param-name>
            <param-value>true</param-value>
        </init-param>
        <init-param>
              <param-name>flushMode</param-name>
              <param-value>AUTO</param-value>  
        </init-param> 
    </filter>
    
    <!-- Struts2配置 -->
    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>openSessionInViewFilter</filter-name>   <!-- 要留意此處的順序,一定是要在strust2之前進行配置

 -->
        <url-pattern>*.action</url-pattern>
    </filter-mapping>
    
    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>*.action</url-pattern>
    </filter-mapping>
    
    <!-- spring監聽器 -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  
</web-app>