首先將spring加入到web容器
在web.xml中加入
<context-param>
<param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/applicationContext-*.xml,classpath*:applicationContext-*.xml</param-value>
(說明:爲了管理方便設置了多個spring的配置文件
applicationContext-common.xml、applicationContext-actions.xml
applicationContext-managers.xml、applicationContext-daos.xml)
</context-param>
然後加入spring監聽器:
<listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
接下來Spring與struts整合
Spring的applicationContext-actions.xml配置文件中的bean:
<bean name="/today" class="com.adcc.inves.web.action.today.TodayAction" scope="prototype" >
<property name="todayManager" ref="todayManager"></property>
<property name="envRouteManager" ref="envRouteManager"></property>
<property name="fmeFlightsTodayService" ref="fmeFlightsTodayService"></property>
<property name="invesAirportManager" ref="invesAirportManager"></property>
</bean>
然後我們配置Struts.xml中的Action就要這樣配:
<action path="/today"
type="org.springframework.web.struts.DelegatingActionProxy" name="todayForm" scope="request" parameter="method">
<forward name="allAirporttomorrowPlan" path="/jsp/common/tomorrowcontainer.jsp"></forward>
<forward name="allAirportStatistics" path="/jsp/common/statisticscontainer.jsp"></forward>
</action>
接下來整合spring與hibernate。
將hibernate交由spring管理:
在spring配置文件applicationContext-common.xml中加入
<!-- 配置SessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation">
<value>classpath:hibernate.cfg.xml</value>
</property>
</bean>
<!-- 配置事務管理器 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref local="sessionFactory"/>
</property>
</bean>
<!-- 配置事務的傳播特性 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager" >
<tx:attributes>
<!-- 在開發的時候可以這樣定義,但部署的時候一定要詳細定義 -->
<tx:method name="*" propagation="REQUIRED"/>
<!--
<tx:method name="add*" propagation="REQUIRED"/>
<tx:method name="del*" propagation="REQUIRED"/>
<tx:method name="update*" propagation="REQUIRED"/>
<tx:method name="*" read-only="true"/>
-->
<tx:method name="modify*" isolation="DEFAULT" propagation="REQUIRED" read-only="false"/>
</tx:attributes>
</tx:advice>
<!-- 配置哪些類哪些方法使用事務 -->
<aop:config>
<aop:pointcut id="modifyCapicity" expression="execution(* com.adcc.inves.manager.*.*.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="modifyCapicity"/> </aop:config>
在web.xml中加入:
1、 spring關閉session
<filter>
<filter-name>hibernateFilter</filter-name>
<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>hibernateFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
2、 spring處理字符編碼
<filter>
<filter-name>Spring character encoding filter</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>Spring character encoding filter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>