struts2+hibernate4+spring4的整合demo

这两天终于把三大框架给学完了,期间遇到了很多异常问题,不过最后也是一个人自己挺了过来。原因就是自学看的视频都是比较老的。下面把我的配置过程给大家看一下。
我配置的过程是:spring+hibernate+struts2,配置完一个框架就写一个测试函数。接下来我把web.xml、struts.xml和applicationContext.xml给贴出来
web.xml
<?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>	
  <!-- 过滤器按照顺序执行,自己写的过滤器 解决乱码问题 -->
<!--   <filter>
 		<filter-name>encoding</filter-name>
 		<filter-class>com.bz.web.filter.EncodingFilter</filter-class>
 	</filter>
 	
 	<filter-mapping>
 		<filter-name>encoding</filter-name>
 		
 		<url-pattern>/*</url-pattern>
 	</filter-mapping>
 	 -->
<!-- 指定spring的配置文件,默认从web根目录寻找配置文件,我们可以通过spring提供的classpath:前缀指定从类路径下寻找 -->
<context-param>
   <param-name>contextConfigLocation</param-name>
   <param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!-- 对Spring容器进行实例化 -->
<listener>
      <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
 	  <!-- 使用spring解决hibernate因session关闭导致的延迟加载例外问题。OpenSessionInViewFilter需要在struts2之前配置 -->
<filter>
        <filter-name>OpenSessionInViewFilter</filter-name>
        <filter-class>org.springframework.orm.hibernate4.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.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
 	<filter-name>struts2</filter-name>
 	<url-pattern>*.html</url-pattern>
</filter-mapping>
 	
<span style="white-space:pre">	</span><!-- 使用 spring框架自己提供的过滤器解决乱码 -->
<filter>
	<filter-name>encoding</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>
</filter>
<filter-mapping>
	<filter-name>encoding</filter-name>
	<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>

struts.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC  
    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"  
    "http://struts.apache.org/dtds/struts-2.3.dtd">  
    
<struts>
	<constant name="struts.devMode" value="true"/>
	<constant name="struts.action.extension" value="html"/>
	<!-- 首先要告知Struts 2运行时使用Spring来创建对象 -->
	<constant name="struts.objectFactory" value="spring"/>
	<package name="default" extends="struts-default">
	<!-- 页面处理 -->
	<action name="login">
		<result>/WEB-INF/login.jsp</result>
	</action>
	<!-- 配置action -->
		<action name="employeeAction_*" class="employeeAction" method="{1}">
			<result name="showMessage">/WEB-INF/showMessage.jsp</result>
			<result name="error" type="chain">login</result>
		</action>
	</package>
	
</struts>

applicationContext.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"
 xmlns:tx="http://www.springframework.org/schema/tx"
 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"
 default-autowire="byName" default-lazy-init="true">
<!-- 测试 -->
 <bean id="test1" class="com.bz.test.Test1">
 	<property name="name" value="ok"/>
 </bean>
 
   <!-- 配置EmployeeServiceImp对象 -->
 <bean id="employeeServiceImp" class="com.bz.service.imp.EmployeeServiceImp" >
 	<property name="sessionFactroy" ref="sessionFactory"/>
 </bean>
 
  <bean id="departmentServiceImp" class="com.bz.service.imp.DepartmentServiceImp" >
 	<property name="sessionFactroy" ref="sessionFactory"/>
 </bean>

		<!-- 配置aciton -->
 <bean id="employeeAction" class="com.bz.web.action.EmployeeAction">
 	<property name="empService" ref="employeeServiceImp"/>
 </bean>

 <!-- 配置数据源(连接池),使用C3P0连接池 -->
 <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
 	<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/myssh1"/>
 	<property name="driverClass" value="com.mysql.jdbc.Driver"/>
 	<property name="user" value="root"/>
 	<property name="password" value="31129772"/>
 	
 	<!-- 每次增量,当需要创建Connection对象时,一次创建几个 -->
	<property name="acquireIncrement" value="3"/>
	<!-- 当创建池对象后,池中应该有几个Connection对象 -->
	<property name="initialPoolSize" value="10"/>
	<!-- 池中最少Connection个数,如果少于这个值,就会创建Connection -->
	<property name="minPoolSize" value="2"/>
	<!-- 池中最大连接个数,并发处理 -->
	<property name="maxPoolSize" value="500"/>
 </bean>

<!--  加载属性文件 -->
 <context:property-placeholder location="classpath:hibernateMessage.properties"/>
 
 <!--  配置会话工厂 org.springframework.orm.hibernate4.LocalSessionFactoryBean-->
 <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
 	<property name="dataSource" ref="dataSource"/><!-- 名字不能变 -->
 	<property name="mappingResources"><!-- 名字不能变 -->
 		<list>
 			<value>com/bz/domain/Employee.hbm.xml</value>
 			<value>com/bz/domain/Department.hbm.xml</value>
 		</list>
 	</property>
 	<!-- 配置hibernate的属性 -->
 	<property name="hibernateProperties"><!-- 名字不能变 -->
		<props >
			<prop key="hibernate.dialect">${hibernate.dialect}</prop>
			<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
			<prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
			
			<!--配置二级缓存。 如果是在java程序中则需要这条语句,如果在web中使用则会报错 -->
			<!-- <prop key="hibernate.current_session_context_class">thread</prop> -->
			<prop key="hibernate.cache.use_query_cache">true</prop> <!-- 同样成功 --> 
			<prop key="hibernate.cache.use_second_level_cache">true</prop>  	
			<prop key="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</prop> 
		</props>
 	</property>
 </bean>

<!-- 配置spring的事务管理器 -->
 <bean id="txManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
 	<!--sessionFactory:是HibernateTransactionManager的setSessionFactory方法  -->
 	<property name="sessionFactory" ref="sessionFactory"/><!-- 名字不能变 -->
 </bean>
 <!-- 使用事务注解 -->
 <tx:annotation-driven transaction-manager="txManager"/>
 
</beans>



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值