天天看點

[原創] Hibernate Open Session In View 的完整配置資訊 跑通無誤!

web.xml 注意兩個filter的順序 位置放反會報no session的異常

<filter>     

  <filter-name>hibernateFilter</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>     

 </filter>

 <filter-mapping>     

  <filter-name>hibernateFilter</filter-name>     

  <url-pattern>/*</url-pattern>     

 </filter-mapping>

 <filter>

  <filter-name>struts2</filter-name>

  <filter-class>

   org.apache.struts2.dispatcher.FilterDispatcher

  </filter-class>

 </filter>

 <filter-mapping>

  <filter-name>struts2</filter-name>

  <url-pattern>/*</url-pattern>

 </filter-mapping>

ApplicationContext.xml

<bean id="baseTransaction" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" abstract="true">

 <property name="transactionManager" ref="transactionManager"/>

 <property name="proxyTargetClass" value="true"/>

 <property name="transactionAttributes">

  <props>

  <prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>

  <prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>

  <prop key="load*">PROPAGATION_REQUIRED,readOnly</prop>

  <prop key="save*">PROPAGATION_REQUIRED</prop>

  <prop key="add*">PROPAGATION_REQUIRED</prop>

  <prop key="update*">PROPAGATION_REQUIRED</prop>

  <prop key="remove*">PROPAGATION_REQUIRED</prop>

  </props>

 </property>

</bean>

<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">

  <property name="sessionFactory">

   <ref local="sessionFactory" />

  </property>

</bean>

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">

  <property name="configLocation" value="classpath:hibernate.cfg.xml">

  </property>

</bean>

<bean id="userService" parent="baseTransaction">

       <property name="target">

            <bean class="com.taoxiaobao.business.UserServiceImpl">

                 <property name="userDAO" ref="userDAO" />

            </bean>

       </property>

</bean>

<bean id="productService" parent="baseTransaction">

       <property name="target">

            <bean class="com.taoxiaobao.business.ProductServiceImpl">

                 <property name="productDAO" ref="productDAO" />

            </bean>

       </property>

</bean>

<bean id="userDAO" class="com.taoxiaobao.db.dao.UserDAO" >

 <property name="sessionFactory">

   <ref bean="sessionFactory" />

   </property>

</bean> 

<bean id="productDAO" class="com.taoxiaobao.db.dao.ProductDAO" >

 <property name="sessionFactory">

   <ref bean="sessionFactory" />

   </property>

</bean> 

<bean name="loginAction" class="com.taoxiaobao.web.action.LoginAction" scope="prototype">

 <property name="userService">

  <ref bean="userService"/>

 </property>

</bean>

<bean name="showProductListAction" class="com.taoxiaobao.web.action.ShowProductListAction" scope="prototype">

 <property name="productService">

  <ref bean="productService"/>

 </property>

</bean>