SSH2 整合配置

本文详细介绍了如何在Struts2中整合Spring框架,包括配置web.xml进行字符集设置和Session管理,通过Struts2配置文件指定Action由Spring管理,以及Spring配置文件中对事务的两种管理方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

配置web.xml


<context-param>     
    <param-name>contextConfigLocation</param-name>     
   <param-value>/WEB-INF/classes/applicationContext.xml</param-value>     
 </context-param>
    <!-- 配置CharacterEncoding,设置字符集      -->  
  <filter>     
    <filter-name>characterEncodingFilter</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>     
    <init-param>     
      <param-name>forceEncoding</param-name>     
      <param-value>true</param-value>     
    </init-param>     
  </filter>     
    
  <filter-mapping>     
    <filter-name>characterEncodingFilter</filter-name>     
    <url-pattern>/*</url-pattern>     
  </filter-mapping>   
  
   <!-- 将HibernateSession开关控制配置在Filter,保证一个请求一个session,并对lazy提供支持 -->    
  <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>*.action</url-pattern>     
  </filter-mapping>  

  <!-- 配置spring -->     
  <listener>     
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>     
  </listener>     
       
  <!-- 页面session配置 -->
  <session-config>     
    <session-timeout>20</session-timeout>     
  </session-config>     
       
  <!-- 错误页面  -->  
  <error-page>     
    <error-code>404</error-code>     
    <location>/error404.html</location>     
  </error-page>


struts.xml配置

<!-- 将Action的创建交给spring来管理 -->     
<constant name="struts.objectFactory" value="spring" /> 

<!—配置struts的action -->  
<package name="dog" namespace="/dog" extends="struts-default">
    	<action name="dogAction" class="DogAction">
    	</action>
    </package>



applicationContext.xml 配置

<!—-头部标签的导入-->
<beans
	xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:aop="http://www.springframework.org/schema/aop"  
	xmlns:p="http://www.springframework.org/schema/p"
	xmlns:tx="http://www.springframework.org/schema/tx" 
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
	http://www.springframework.org/schema/context   
http://www.springframework.org/schema/context/spring-context-3.0.xsd  
http://www.springframework.org/schema/aop 
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd     
        http://www.springframework.org/schema/context 
http://www.springframework.org/schema/context/spring-context-3.0.xsd     
        http://www.springframework.org/schema/tx 
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">


   <!-- 第一种方法配置sessionFactory -->      
    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">     
        <property name="configLocation" value="classpath:config/hibernate.cfg.xml"></property>     
    </bean>

<!-- 第二种方法配置sessionFactory (建议)-->   
<bean id="sessionFactory"
		class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
		<property name="dataSource">
			<ref bean="dataSource" />
		</property>
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">
					org.hibernate.dialect.MySQLDialect
				</prop>
			</props>
		</property>
		<property name="mappingResources">
			<list>
			<value>org/gogoforward/model/Competition.hbm.xml</value>
			<value>org/gogoforward/model/Member.hbm.xml</value>
			<value>org/gogoforward/model/Area.hbm.xml</value></list>
		</property></bean>


<!—-第一种方式配置事务   -->  
	 
 <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">     
        <property name="sessionFactory">
			<ref bean="sessionFactory" />
		</property>    
    </bean>     
         
    <tx:advice id="txadvice" transaction-manager="transactionManager">     
        <tx:attributes>     
            <tx:method name="save*" propagation="REQUIRED"></tx:method>
            <tx:method name="insert*" propagation="REQUIRED"></tx:method>
            <tx:method name="add*" propagation="REQUIRED"></tx:method>
            <tx:method name="del*" propagation="REQUIRED"></tx:method>
            <tx:method name="remove*" propagation="REQUIRED"></tx:method>
            <tx:method name="update*" propagation="REQUIRED"></tx:method>
            <tx:method name="edit*" propagation="REQUIRED"></tx:method>
            <tx:method name="find*" read-only="true"></tx:method>
            <tx:method name="get*" read-only="true"></tx:method>
            <tx:method name="query*" read-only="true"></tx:method>
            <tx:method name="list*" read-only="true"></tx:method>
            <tx:method name="count*" read-only="true"></tx:method>
            <tx:method name="exist*" read-only="true"></tx:method>
            <tx:method name="*" read-only="true"></tx:method>     
        </tx:attributes>     
    </tx:advice>      
         
    <aop:config proxy-target-class="true">     
        <aop:pointcut id="daoMethods" expression="execution(* org.gogoforward.model.*Service.*(..))"/>     
        <aop:advisor advice-ref="txadvice" pointcut-ref="daoMethods"/>     
</aop:config>  


<!—-第二种方式配置事务   -->

<!-- sping事务代理配置 -->
	<bean id="transactionManager"
		class="org.springframework.orm.hibernate3.HibernateTransactionManager">
		<property name="sessionFactory" ref="sessionFactory" />
	</bean>
	<!-- 事务拦截器 -->
	<bean id="transactionInterceptor"
		class="org.springframework.transaction.interceptor.TransactionInterceptor">
		<property name="transactionManager" ref="transactionManager" />
		<property name="transactionAttributes">
			<props>
				<prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>
				<prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
				<prop key="*">PROPAGATION_REQUIRED</prop>
			</props>
		</property>
	</bean>
	<!-- 自动事务代理 -->
	<bean id="transactionAutoProxyCreator"
		class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
		<property name="beanNames">
			<list>
				<value>*Service</value>
			</list>
		</property>
		<property name="interceptorNames">
			<list>
				<value>transactionInterceptor</value>
			</list>
		</property>
		<property name="proxyTargetClass">
			<value>false</value>
		</property>
	</bean>

	<!—-第二种事务配置结束 end-->
	<!-- spring管理struts2的Action -->
	<bean id="DogAction" class="org.gogoforward.action.DogAction" scope="prototype">
		<property name="areaDao">
			<ref bean="AreaDAO"/>
		</property>
	</bean>

转载于:https://my.oschina.net/haopeng/blog/86564

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值