`
mingo
  • 浏览: 40569 次
最近访客 更多访客>>
社区版块
存档分类
最新评论

webwork2.2.2迁移到struts2.0.6之记录

    博客分类:
  • Java
阅读更多

听说struts2出来了,与webwork2的差别也不是很大,robbin说迁移很easy,禁不住诱惑,我也尝试了一把,然则发现其实好像没那么容易啊,下面把一些过程记下来。

1、JDK版本,首先我们用的是JDK1.4,一直不太敢使用JDK5,因为我们开发的系统很有可能还要在用户很久以前的系统上跑,怕出兼容问题,所以struts2我们只能选用其jdk4的那个版本了。放入的jar是:

xwork-j4-2.0.1.jar、struts2-core-j4-2.0.6.jar、retrotranslator-transformer-1.2.0.jar、retrotranslator-runtime-1.2.0.jar、backport-util-concurrent-3.0.jar

2、受到一个jar包的名字的引诱,就是struts2-spring-plugin-2.0.6.jar,于是将这个jar放到了lib中,出现了版本不兼容的异常,系统不能启动,于是找到源码,用jdk4重新编译了一下,好了。然后将struts-plugin.xml也放到了classes下面。这时候还是一直启动不了。郁闷。非常郁闷,找到以前的项目不停的对比。最后发现struts2-spring-plugin-2.0.6.jar这个jar完全没用,也就是说里面的那个类StrutsSpringObjectFactory对系统来说是没用的。删掉,然后将struts-plugin.xml也删掉了。最后的各种配置文件如下:

struts.properties文件内容如下,注意的是:
struts.objectFactory = spring这句要注释掉
struts.objectFactory.spring.autoWire=name要保留
struts.configuration.files=struts-default.xml,struts.xml(里面不能有struts-plugin.xml文件)
  1. ### Struts default properties   
  2. ###(can be overridden by a struts.properties file in the root of the classpath)   
  3. ###   
  4.   
  5. ### Specifies the Configuration used to configure Struts    
  6. ### one could extend org.apache.struts2.config.Configuration   
  7. ### to build one's customize way of getting the configurations parameters into Struts   
  8. # struts.configuration=org.apache.struts2.config.DefaultConfiguration   
  9.   
  10. ### This can be used to set your default locale and encoding scheme   
  11. # struts.locale=en_US   
  12. struts.i18n.encoding=UTF-8  
  13.   
  14. ### if specified, the default object factory can be overridden here   
  15. ### Note: short-hand notation is supported in some cases, such as "spring"  
  16. ###       Alternatively, you can provide a com.opensymphony.xwork2.ObjectFactory subclass name here     
  17. #struts.objectFactory = spring   
  18. #上面这句一定要注释掉。   
  19. ### specifies the autoWiring logic when using the SpringObjectFactory.   
  20. ### valid values are: name, type, auto, and constructor (name is the default)   
  21. struts.objectFactory.spring.autoWire = name   
  22.   
  23. ### indicates to the struts-spring integration if Class instances should be cached   
  24. ### this should, until a future Spring release makes it possible, be left as true  
  25. ### unless you know exactly what you are doing!   
  26. ### valid values are: truefalse (true is the default)   
  27. struts.objectFactory.spring.useClassCache = true  
  28.   
  29. ### if specified, the default object type determiner can be overridden here   
  30. ### Note: short-hand notation is supported in some cases, such as "tiger" or "notiger"  
  31. ###       Alternatively, you can provide a com.opensymphony.xwork2.util.ObjectTypeDeterminer implementation name here   
  32. ### Note: if you have the xwork-tiger.jar within your classpath, GenericsObjectTypeDeterminer is used by default  
  33. ###       To disable tiger support use the "notiger" property value here.   
  34. #struts.objectTypeDeterminer = tiger   
  35. #struts.objectTypeDeterminer = notiger   
  36.   
  37. ### Parser to handle HTTP POST requests, encoded using the MIME-type multipart/form-data   
  38. # struts.multipart.parser=cos   
  39. # struts.multipart.parser=pell   
  40. struts.multipart.parser=jakarta   
  41. # uses javax.servlet.context.tempdir by default  
  42. struts.multipart.saveDir=   
  43. struts.multipart.maxSize=2097152  
  44.   
  45. ### Load custom property files (does not override struts.properties!)   
  46. # struts.custom.properties=application,org/apache/struts2/extension/custom   
  47.   
  48. ### How request URLs are mapped to and from actions   
  49. #struts.mapper.class=org.apache.struts2.dispatcher.mapper.DefaultActionMapper   
  50.   
  51. ### Used by the DefaultActionMapper   
  52. ### You may provide a comma separated list, e.g. struts.action.extension=action,jnlp,do  
  53. struts.action.extension=action   
  54.   
  55. ### Used by FilterDispatcher   
  56. ### If true then Struts serves static content from inside its jar.    
  57. ### If false then the static content must be available at <context_path>/struts   
  58. struts.serve.static=true  
  59.   
  60. ### Used by FilterDispatcher   
  61. ### This is good for development where one wants changes to the static content be   
  62. ### fetch on each request.    
  63. ### NOTE: This will only have effect if struts.serve.static=true  
  64. ### If true -> Struts will write out header for static contents such that they will   
  65. ###             be cached by web browsers (using Date, Cache-Content, Pragma, Expires)   
  66. ###             headers).   
  67. ### If false -> Struts will write out header for static contents such that they are   
  68. ###            NOT to be cached by web browser (using Cache-Content, Pragma, Expires   
  69. ###            headers)   
  70. struts.serve.static.browserCache=true  
  71.   
  72. ### Set this to false if you wish to disable implicit dynamic method invocation   
  73. ### via the URL request. This includes URLs like foo!bar.action, as well as params   
  74. ### like method:bar (but not action:foo).    
  75. ### An alternative to implicit dynamic method invocation is to use wildcard    
  76. ### mappings, such as <action name="*/*" method="{2}" class="actions.{1}">   
  77. struts.enable.DynamicMethodInvocation = true  
  78.   
  79. ### Set this to true if you wish to allow slashes in your action names.  If false,   
  80. ### Actions names cannot have slashes, and will be accessible via any directory   
  81. ### prefix.  This is the traditional behavior expected of WebWork applications.   
  82. ### Setting to true is useful when you want to use wildcards and store values   
  83. ### in the URL, to be extracted by wildcard patterns, such as    
  84. ### <action name="*/*" method="{2}" class="actions.{1}"> to match "/foo/edit" or    
  85. ### "/foo/save".   
  86. struts.enable.SlashesInActionNames = false  
  87.   
  88. ### use alternative syntax that requires %{} in most places   
  89. ### to evaluate expressions for String attributes for tags   
  90. struts.tag.altSyntax=true  
  91.   
  92. ### when set to true, Struts will act much more friendly for developers. This   
  93. ### includes:   
  94. ### - struts.i18n.reload = true  
  95. ### - struts.configuration.xml.reload = true  
  96. ### - raising various debug or ignorable problems to errors   
  97. ###   For example: normally a request to foo.action?someUnknownField=true should   
  98. ###                be ignored (given that any value can come from the web and it   
  99. ###                should not be trusted). However, during development, it may be   
  100. ###                useful to know when these errors are happening and be told of   
  101. ###                them right away.   
  102. struts.devMode = true  
  103.   
  104. ### when set to true, resource bundles will be reloaded on _every_ request.   
  105. ### this is good during development, but should never be used in production   
  106. struts.i18n.reload=true  
  107.   
  108. ### Standard UI theme   
  109. ### Change this to reflect which path should be used for JSP control tag templates by default  
  110. struts.ui.theme=jsxhtml   
  111. struts.ui.templateDir=template   
  112. #sets the default template type. Either ftl, vm, or jsp   
  113. struts.ui.templateSuffix=ftl   
  114.   
  115. ### Configuration reloading   
  116. ### This will cause the configuration to reload struts.xml when it is changed   
  117. struts.configuration.xml.reload=true  
  118.   
  119. ### Location of velocity.properties file.  defaults to velocity.properties   
  120. ###struts.velocity.configfile = velocity.properties   
  121.   
  122. ### Comma separated list of VelocityContext classnames to chain to the StrutsVelocityContext   
  123. ###struts.velocity.contexts =   
  124.   
  125. ### Location of the velocity toolbox   
  126. ###struts.velocity.toolboxlocation=   
  127.   
  128. ### used to build URLs, such as the UrlTag   
  129. struts.url.http.port = 80  
  130. struts.url.https.port = 443  
  131. ### possible values are: none, get or all   
  132. struts.url.includeParams = get   
  133.   
  134. ### Load custom default resource bundles   
  135. # struts.custom.i18n.resources=testmessages,testmessages2   
  136.   
  137. ### workaround for some app servers that don't handle HttpServletRequest.getParameterMap()   
  138. ### often used for WebLogic, Orion, and OC4J   
  139. struts.dispatcher.parametersWorkaround = false  
  140.   
  141. ### configure the Freemarker Manager class to be used   
  142. ### Allows user to plug-in customised Freemarker Manager if necessary   
  143. ### MUST extends off org.apache.struts2.views.freemarker.FreemarkerManager   
  144. #struts.freemarker.manager.classname=org.apache.struts2.views.freemarker.FreemarkerManager   
  145.   
  146. ### See the StrutsBeanWrapper javadocs for more information   
  147. struts.freemarker.wrapper.altMap=true  
  148.   
  149. ### configure the XSLTResult class to use stylesheet caching.   
  150. ### Set to true for developers and false for production.   
  151. struts.xslt.nocache=false  
  152.   
  153. ### A list of configuration files automatically loaded by Struts   
  154. struts.configuration.files=struts-default.xml,struts.xml   
  155. #不能加载struts-plugin.xml,在classpath下面也不能有这个文件   
  156. ### Whether to always select the namespace to be everything before the last slash or not   
  157. struts.mapper.alwaysSelectFullNamespace=false  
struts.xml 的代码如下,规模开发时,每个人拥有自己的配置文件,避免文件提交冲突。这个文件曾经犯晕,将struts-plugin.xml文件include进来了,结果总是启动不了,报空指针异常。去掉。
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!DOCTYPE struts PUBLIC   
  3.     "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"   
  4.     "http://struts.apache.org/dtds/struts-2.0.dtd">  
  5.   
  6. <struts>  
  7.     <include file="struts-default.xml" />  
  8.     <include file="struts-application.xml" />  
  9.     <include file="struts-common.xml" />  
  10.     <include file="struts-dym.xml" />  
  11.     <include file="struts-zxw.xml" />  
  12.     <include file="struts-hw.xml" />  
  13.     <include file="struts-zw.xml" />  
  14.     <include file="struts-lb.xml" />  
  15.     <include file="struts-lx.xml" />  
  16.     <include file="struts-cjh.xml" />  
  17.     <include file="struts-wyf.xml" />  
  18. </struts>  
 
下面的代码比较重要,其中相比webwork2.2.2时,增加了一个拦截器autowiring,把它放在了拦截器栈的最上面。这样就可以将spring配置文件的service示例,以名字的形式,自动注入到action中,完成action对业务service的调用。
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!DOCTYPE struts PUBLIC   
  3.     "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"   
  4.     "http://struts.apache.org/dtds/struts-2.0.dtd">  
  5.   
  6. <struts>  
  7.     <package name="default" extends="struts-default">  
  8.         <interceptors>  
  9.             <!-- Interceptor to catch exceptions and display mapped exception result -->  
  10.             <interceptor name="exceptionHandler" class="com.broadtext.framework.web.interceptor.ExceptionHandleInterceptor" />  
  11.             <interceptor name="authorizationHandler" class="com.broadtext.framework.web.interceptor.AuthorizationInterceptor" />  
  12.             <interceptor name="autowiring" class="com.opensymphony.xwork2.spring.interceptor.ActionAutowiringInterceptor"/>  
  13.                    
  14.             <interceptor-stack name="appDefaultStack">  
  15.                 <interceptor-ref name="autowiring"/>  
  16.                 <interceptor-ref name="exception"/>  
  17.                 <interceptor-ref name="alias"/>  
  18.                 <interceptor-ref name="servlet-config"/>  
  19.                 <interceptor-ref name="prepare"/>  
  20.                 <interceptor-ref name="chain"/>  
  21.                 <interceptor-ref name="model-driven"/>  
  22.                 <interceptor-ref name="fileUpload"/>  
  23.                 <interceptor-ref name="static-params"/>  
  24.                 <interceptor-ref name="params"/>  
  25.                 <interceptor-ref name="conversionError"/>  
  26.                 <interceptor-ref name="exceptionHandler" />  
  27.                 <interceptor-ref name="authorizationHandler" />  
  28.             </interceptor-stack>  
  29.         </interceptors>  
  30.         <default-interceptor-ref name="appDefaultStack" />  
  31.            
  32.            
  33.         <global-results>  
  34.             <result name="appBaseError">/pages/common/appBaseError.jsp</result>  
  35.             <result name="runtimeError">/pages/common/runtimeError.jsp</result>  
  36.             <result name="unkownError">/pages/common/runtimeError.jsp</result>  
  37.         </global-results>  
  38.     </package>  
  39. </struts>  
 
这是userService的spring配置代码,使用的是Spring2.0.1。
  1. <bean id="userService" class="com.broadtext.cmshzhgl.security.service.impl.UserServiceImpl">  
  2.         <property name="dao" ref="baseDao"></property>  
  3.         <property name="utilService" ref="utilService"></property>  
  4.         <property name="roleService" ref="roleService"></property>  
  5.     </bean>  
这是测试Action的 代码,用来测试一下是不是可以调用spring配置的业务service了。
  1. public class LoginAction extends BaseAction {   
  2.     private UserService userService;   
  3.     public String login(){   
  4.         String aaa = userService.getTestString();   
  5.         System.out.println(aaa);   
  6.         return SUCCESS;   
  7.     }   
  8.     public void setUserService(UserService userService) {   
  9.         this.userService = userService;   
  10.     }   
  11. }  
action与jsp之间的struts配置文件代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!DOCTYPE struts PUBLIC   
  3.     "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"   
  4.     "http://struts.apache.org/dtds/struts-2.0.dtd">  
  5.   
  6. <struts>  
  7.     <package name="test" extends="default" namespace="/pages/test">  
  8.         <action name="login" class="com.broadtext.cmshzhgl.security.action.LoginAction" method="login">               
  9.             <result name="success">/pages/common/currentUser.jsp</result>  
  10.         </action>  
  11.     </package>  
  12. </struts>  

 

总体感觉是:对于具体的开发业务的配置,xwork.xml变到struts.xml比较简单,但是spring如何注入还是比较麻烦的,要改几个文件的内容,而不是简单的将webwork和xwork改成struts就ok,主要问题struts提供了好几种spring注入的选择,弄得有些犯晕。上面我只列出其中一种配置方案,更多的配置方法慢慢再体验吧。

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics