转自:http://www.gjrencai.com/newsShow.asp?dataID=1547
一、配置Struts2
跟webwork配置基本一致,主要是struts2.properties和struts.xml 2个配置文件,我的struts2.properties如下配置:
struts.tag.altSyntax = true
struts.devMode = true
### These can be used to set the default HTTP and HTTPS ports
struts.url.http.port = 80
#webwork.url.https.port = 443
### This can be used to set your locale and encoding scheme
struts.custom.i18n.resources=ApplicationResources
struts.locale=zh_CN
struts.i18n.encoding=utf-8
# uses javax.servlet.context.tempdir by default
struts.multipart.parser=com.opensymphony.webwork.dispatcher.multipart.PellMultiPartRequest
#struts.multipart.saveDir=tmp
struts.multipart.saveDir=/tmp
struts.multipart.maxSize=512000000
struts.configuration.xml.reload=true
struts.objectFactory = spring
struts.xml 就是配置action了,由于设定了struts.objectFactory = spring,因此struts2会自动将action转为spring的bean,struts.xml可以直接配置我们的action路径,在action中我们只需要设置某个service文件的set方法即可调用事务管理bean。
<!--sp-->xml version="1.0" encoding="UTF-8" ?> <!--CTYPE struts PUBLIC </sp--> "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <include file="struts-default.xml" /> <package name="default" extends="struts-default"> <default-interceptor-ref name="completeStack" /> <global-results> <result name="login" type="redirect">login!default.actionresult> <result name="unauthorized">unauthorized.jspresult> global-results> <action name="login" class="com.baseframe.action.LoginAction"> <result name="input">login.jspresult> <result name="success">main.jspresult> <result name="error">login.jspresult> action> <action name="logout" class="com.baseframe.action.LogoutAction"> <result name="success">login.jspresult> action> package> struts>
二、配置Spring2
添加dist下的3个spring包,因为不知道哪些包是必须的,把lib里的包都加了,去除jakarta-commons/commons-attributes-compiler.jar、j2ee/servlet-api.jar、j2ee/jsp-api.jar,然后建立配置文件,我分了三个文件:
<!--sp-->xml version="1.0" encoding="UTF-8"?> <!--CTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" </sp--> "http://www.springframework.org/dtd/spring-beans.dtd"> <beans> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> ... bean> <bean id="hibernateProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean"> ... bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="hibernateProperties"> <ref bean="hibernateProperties"/> property> <property name="mappingResources"> <list> <value>com/baseframe/model/AppUser.hbm.xmlvalue> <!-- </span> </li> <li class=""><span><span class="comments"> 此处增加hbm </span> </span></li> <li class="alt"><span><span class="comments"> --> list> property> bean> <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="*">PROPAGATION_REQUIREDprop> <prop key="amount*">PROPAGATION_REQUIRED,readOnlyprop> <prop key="find*">PROPAGATION_REQUIRED,readOnlyprop> props> property> bean> <bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator"> <property name="beanNames"> <value>*Servicevalue> property> <property name="interceptorNames"> <list> <value>transactionInterceptorvalue> <!-- </span> </li> <li class="alt"><span><span class="comments"> 此处增加新的Interceptor </span> </span></li> <li class=""><span><span class="comments"> --> list> property> bean> <bean class="org.springframework.transaction.interceptor.TransactionAttributeSourceAdvisor"> <property name="transactionInterceptor" ref="transactionInterceptor"/> bean> beans>
<!--sp-->xml version="1.0" encoding="UTF-8"?> <!--CTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" </sp--> "http://www.springframework.org/dtd/spring-beans.dtd"> <beans default-autowire="byName"> <bean id="userDao" class="com.baseframe.dao.impl.UserDaoImpl"/> beans>
<!--sp-->xml version="1.0" encoding="UTF-8"?> <!--CTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" </sp--> "http://www.springframework.org/dtd/spring-beans.dtd"> <beans default-autowire="byName"> <bean id="userService" class="com.baseframe.service.impl.UserServiceImpl"/> beans>
有一点很重要,struts的filter需要放在最底,不然action接收的中文可能会乱码,不知道是不是这个原因,反正配置了就没有乱码了,加上org.springframework.orm.hibernate3.support.OpenSessionInViewFilter就不用考虑hibernate的lazy问题了:
filter> <filter-name>encodingFilterfilter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class> <init-param> <param-name>encodingparam-name> <param-value>UTF-8param-value> init-param> filter> <filter-mapping> <filter-name>encodingFilterfilter-name> <url-pattern>/*url-pattern> filter-mapping> hibernateFilter org.springframework.orm.hibernate3.support.OpenSessionInViewFilter hibernateFilter /* <filter> extremetable的filter filter> <filter-mapping> extremetable的filter mapping filter-mapping> <filter> <filter-name>struts2filter-name> <filter-class>org.apache.struts2.dispatcher.FilterDispatcherfilter-class> filter> <filter-mapping> <filter-name>struts2filter-name> <url-pattern>/*url-pattern> filter-mapping>