配置struts2.0.6+spring2.0.3+hibernane3备忘

一、struts2。

跟webwork配置基本一致,主要是struts2.properties和struts.xml 2个配置文件,我的struts2.properties如下配置:

struts2.properties
  1. struts.tag.altSyntax = true  
  2. struts.devMode = true  
  3.   
  4. ### These can be used to set the default HTTP and HTTPS  ports   
  5. struts.url.http.port = 80  
  6. #webwork.url.https.port = 443  
  7.   
  8. ### This can be used to set your locale and encoding scheme   
  9. struts.custom.i18n.resources=ApplicationResources  
  10. struts.locale=zh_CN  
  11. struts.i18n.encoding=utf-8   
  12.   
  13. # uses javax.servlet.context.tempdir by default   
  14. struts.multipart.parser=com.opensymphony.webwork.dispatcher.multipart.PellMultiPartRequest   
  15. #struts.multipart.saveDir=tmp  
  16. struts.multipart.saveDir=/tmp   
  17. struts.multipart.maxSize=512000000  
  18. struts.configuration.xml.reload=true  
  19.   
  20. struts.objectFactory = spring  

struts.xml 就是配置action了,由于设定了struts.objectFactory = spring,因此struts2会自动将action转为spring的bean,struts.xml可以直接配置我们的action路径,在 action中我们只需要设置某个service文件的set方法即可调用事务管理bean。

struts.xml
  1. <!--sp-->xml version="1.0" encoding="UTF-8" ?>  
  2. <!--CTYPE struts PUBLIC   </sp-->
  3.     "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"   
  4.     "http://struts.apache.org/dtds/struts-2.0.dtd">  
  5.   
  6. <struts>  
  7.   
  8.     <include file="struts-default.xml" />  
  9.   
  10.     <package name="default" extends="struts-default">  
  11.         <default-interceptor-ref name="completeStack" />  
  12.   
  13.         <global-results>  
  14.             <result name="login" type="redirect">login!default.actionresult>  
  15.             <result name="unauthorized">unauthorized.jspresult>  
  16.         global-results>  
  17.            
  18.         <action name="login" class="com.baseframe.action.LoginAction">  
  19.             <result name="input">login.jspresult>  
  20.             <result name="success">main.jspresult>  
  21.             <result name="error">login.jspresult>  
  22.         action>  
  23.         <action name="logout" class="com.baseframe.action.LogoutAction">  
  24.             <result name="success">login.jspresult>  
  25.         action>  
  26.     package>  
  27.   
  28. struts>  

二、spring2。

添加dist下的3个spring包,因为不知道哪些包是必须的,把lib里的包都加了,去除jakarta-commons/commons-attributes-compiler.jar、j2ee/servlet-api.jar、j2ee/jsp-api.jar,然后建立配置文件,我分了三个文件:

主配置文件
  1. <!--sp-->xml version="1.0" encoding="UTF-8"?>  
  2. <!--CTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"   </sp-->
  3.     "http://www.springframework.org/dtd/spring-beans.dtd">  
  4. <beans>    
  5.     <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">  
  6.         ...   
  7.     bean>  
  8.        
  9.     <bean id="hibernateProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">  
  10.         ...   
  11.     bean>  
  12.        
  13.     <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">  
  14.         <property name="dataSource" ref="dataSource"/>  
  15.         <property name="hibernateProperties">  
  16.             <ref bean="hibernateProperties"/>  
  17.         property>  
  18.         <property name="mappingResources">  
  19.             <list>  
  20.                 <value>com/baseframe/model/AppUser.hbm.xmlvalue>  
  21.                 <!--    </span> </li> <li class=""><span><span class="comments">                                此处增加hbm   </span> </span></li> <li class="alt"><span><span class="comments">                        -->  
  22.             list>  
  23.         property>  
  24.     bean>  
  25.        
  26.     <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">  
  27.         <property name="sessionFactory" ref="sessionFactory"/>  
  28.     bean>  
  29.        
  30.     <bean id="transactionInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor">  
  31.         <property name="transactionManager" ref="transactionManager"/>  
  32.         <property name="transactionAttributes">  
  33.             <props>  
  34.                 <prop key="*">PROPAGATION_REQUIREDprop>  
  35.                 <prop key="amount*">PROPAGATION_REQUIRED,readOnlyprop>  
  36.                 <prop key="find*">PROPAGATION_REQUIRED,readOnlyprop>  
  37.             props>  
  38.         property>  
  39.     bean>    
  40.        
  41.     <bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">  
  42.         <property name="beanNames">  
  43.             <value>*Servicevalue>  
  44.         property>  
  45.         <property name="interceptorNames">  
  46.             <list>  
  47.                 <value>transactionInterceptorvalue>  
  48.                 <!--    </span> </li> <li class="alt"><span><span class="comments">                                此处增加新的Interceptor   </span> </span></li> <li class=""><span><span class="comments">                        -->  
  49.             list>  
  50.         property>  
  51.     bean>  
  52.        
  53.     <bean class="org.springframework.transaction.interceptor.TransactionAttributeSourceAdvisor">  
  54.         <property name="transactionInterceptor" ref="transactionInterceptor"/>  
  55.     bean>  
  56. beans>  
 
dao配置文件
  1. <!--sp-->xml version="1.0" encoding="UTF-8"?>  
  2.   
  3. <!--CTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"   </sp-->
  4.     "http://www.springframework.org/dtd/spring-beans.dtd">  
  5.   
  6. <beans default-autowire="byName">  
  7.        
  8.     <bean id="userDao" class="com.baseframe.dao.impl.UserDaoImpl"/>  
  9.        
  10. beans>  

 

service/manager配置 
  1. <!--sp-->xml version="1.0" encoding="UTF-8"?>  
  2.   
  3. <!--CTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"   </sp-->
  4.     "http://www.springframework.org/dtd/spring-beans.dtd">  
  5.   
  6. <beans default-autowire="byName">  
  7.        
  8.     <bean id="userService" class="com.baseframe.service.impl.UserServiceImpl"/>  
  9.        
  10. beans>  

 

三、web.xml

有一点很重要,struts的filter需要放在最底,不然action接收的中文可能会乱码,不知道是不是这个原因,反正配置了就没有乱码了:

xml 代码
  1. <filter>  
  2.       <filter-name>encodingFilterfilter-name>  
  3.       <filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>  
  4.       <init-param>  
  5.           <param-name>encodingparam-name>  
  6.           <param-value>UTF-8param-value>  
  7.       init-param>  
  8.   filter>  
  9.   <filter-mapping>  
  10.       <filter-name>encodingFilterfilter-name>  
  11.       <url-pattern>/*url-pattern>  
  12.   filter-mapping>  
  13.   
  14.   <filter>  
  15.        extremetable的filter   
  16.   filter>  
  17.   <filter-mapping>  
  18.        extremetable的filter mapping   
  19.   filter-mapping>  
  20.   
  21.   <filter>  
  22.       <filter-name>struts2filter-name>  
  23.       <filter-class>org.apache.struts2.dispatcher.FilterDispatcherfilter-class>  
  24.   filter>  
  25.   <filter-mapping>  
  26.       <filter-name>struts2filter-name>  
  27.       <url-pattern>/*url-pattern>  
  28.   filter-mapping>  

你可能感兴趣的:(spring,bean,struts,配置管理,Webwork)