Strutsw2与Spring整合流程-简述
1.??????新建WEB工程;
2.??????導(dǎo)入struts2開發(fā)包,和資源配置文件
①?globalMessages.properties
②?struts.properties
3.??????導(dǎo)入Spring開發(fā)jar包
4.??????在web.xml中配置spring容器,讓服務(wù)器啟動時(shí)就加載
如下:
<!-- 指定spring的配置文件,默認(rèn)從web根目錄尋找配置文件,我們可以通過spring提供的classpath:前綴指定從類路徑下尋找 -->
<context-param><param-name>contextConfigLocation</param-name><param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!-- 對Spring容器進(jìn)行實(shí)例化 -->
<listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
5.??????Struts2清除緩存,上傳時(shí)用到
?
<!-- 清楚緩存,上傳時(shí)必須 --><filter><filter-name>struts-cleanup</filter-name>
<filter-class>org.apache.struts2.dispatcher.ActionContextCleanUp</fil ter-class></filter>
?
6.??????如果需要讓能在struts2中使用Forward功能則作如下配置,web.xml文件【紅色字體哪】
?
<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>*.do</url-pattern><span style="color:#ff0000;"><dispatcher>FORWARD</dispatcher><dispatcher>REQUEST</dispatcher></span></filter-mapping>
?
7.??????在spring容器中把對象都配置到applicationContext.xml中
這里需要注意的是:配置action的時(shí)候:如下例子
applicationContext.xml中配置如下:
<!-- 配置Action對象 --><bean id="loginAction"class="com.sx.web.actions.LoginAction" scope="prototype">
<property name="usrBiz" ref="usrBiz"></property>
</bean>
那么在struts.xml中action這要這樣配置:
?
?
?把spring交由spring管理的配置,當(dāng)然也可以通過屬性配置文件配置struts.objectFactory屬性值。在struts.properties中設(shè)置struts.objectFactory屬性值:struts.objectFactory= spring
也可以直接在struts.xml中作如下配置:
<!-- 把struts2交給spring管理 --><constant name="struts.objectFactory" value="spring"></constant>
??也就是說如果struts2如果與spring整合了后,struts2的action配置中的class屬性直接引用spring容器中action bean的id值即可;
<package name="usrBiz" namespace="/"extends="struts-default"><action name="usr_*" class="loginAction" method="{1}"><result >index.jsp</result></action>
</package>
??
??
?
?
8.??????解決懶加載的問題:
?
<!--避免hibernate延遲加載 的"著名的過濾器" --> <filter><filter-name>struts2</filter-name><filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class></filter><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>
?
?
?
9.??????Spring中的事務(wù)配置
方法①:
首先在容器中啟用事務(wù)管理器
<!-- 啟用注解 -->
<context:annotation-config/><!-- 配置統(tǒng)一管理事務(wù)-->
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"><property name="sessionFactory" ref="sessionFactory">
</property>
</bean>
<tx:annotation-driven transaction-manager="txManager"/>
?
這時(shí)候則在對應(yīng)需要事務(wù)處理的類文件中做如下注解(@Transactional):
這樣spring則會自動幫我們管理事務(wù),而無需我們自己去管理;
?
方法②:
?
①事物管理器
②advice通知(這里可以指定把事物應(yīng)用到那個(gè)方法,使用哪種級別傳播方式)
③aop切面
?
<!-- 事務(wù)配置方法② --><!-- 事務(wù)管理器 --><bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"><property name="sessionFactory" ref="sessionFactory"></property></bean><!-- 配置通知 --><tx:advice id="txAdvice" transaction-manager="txManager"><tx:attributes><tx:method name="update*" propagation="REQUIRED"/></tx:attributes></tx:advice><!-- 配置切面 --><aop:config><!-- 配置切入點(diǎn) --><aop:pointcut expression="execution(* com.s2sha.web.biz.impls.*.*(..))"id="usrBizOperaction"/><!-- 織入通知 --><aop:advisor advice-ref="txAdvice" pointcut-ref="usrBizOperaction"/></aop:config>
?
總結(jié)
以上是生活随笔為你收集整理的Strutsw2与Spring整合流程-简述的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java命令行读入密码_java-在命令
- 下一篇: 2022-2028年中国互联网+不良资产