javaEE SSH三大框架整合,Spring整合Hibernate,扩大Hibernate与数据库连接会话session的作用范围

为了避免使用Hibernate懒加载时出现no-session问题,需要扩大session的作用范围。(session:Hibernate与数据库的连接会话)

WEB-INF/web.xml(web项目配置,(过滤器)扩大session的作用范围):

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
	<display-name>MyWeb</display-name>
	
	<!-- 通过监听器Listener,可以让spring容器随web项目的启动而创建,随项目的关闭而销毁.(可以保证Spring容器是单例的) -->
	<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>
	
	<!-- 扩大Hibernate中与数据库的连接会话session的作用范围 (避免Hibernate中懒加载,出现no-session的问题)
		注意: 任何filter一定要在struts的filter之前调用
	 -->
	 <filter>
		<filter-name>openSessionInView</filter-name>
		<!-- Spring自带的过滤器;要根据Spring对应的版本填写 -->
		<filter-class>org.springframework.orm.hibernate5.support.OpenSessionInViewFilter</filter-class>
	</filter>
	
	<!-- struts2核心过滤器 -->
	<filter>
		<filter-name>struts2</filter-name>
		<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
	</filter>
	
	<filter-mapping>
		<filter-name>openSessionInView</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
	<filter-mapping>
		<filter-name>struts2</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
	
	<welcome-file-list>
	  <welcome-file>index.html</welcome-file>
	  <welcome-file>index.htm</welcome-file>
	  <welcome-file>index.jsp</welcome-file>
	  <welcome-file>default.html</welcome-file>
	  <welcome-file>default.htm</welcome-file>
	  <welcome-file>default.jsp</welcome-file>
	</welcome-file-list>
</web-app>

猜你喜欢

转载自blog.csdn.net/houyanhua1/article/details/82144616