---
title: "webwork2.2.2迁移到struts2.0.6之记录"
author: "mingo"
date: "2007-03-22T11:16:11Z"
tags: []
slug: "webwork2-2-2-struts2-0-6"
draft: false
lastmod: "2007-03-22T11:16:11Z"
id: 64616
url: "https://mingo.iteye.com/blog/64616"
---

听说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文件）

001. ### Struts default properties
002. ###(can be overridden by a struts.properties file in the root of the classpath)
003. \###
004. ### Specifies the Configuration used to configure Struts
005. ### one could extend org.apache.struts2.config.Configuration
006. ### to build one's customize way of getting the configurations parameters into Struts
007. # struts.configuration=org.apache.struts2.config.DefaultConfiguration
008. ### This can be used to set your default locale and encoding scheme
009. # struts.locale=en\_US
010. struts.i18n.encoding=UTF-8
011. ### if specified, the default object factory can be overridden here
012. ### Note: short-hand notation is supported in some cases, such as "spring"
013. ###       Alternatively, you can provide a com.opensymphony.xwork2.ObjectFactory subclass name here
014. #struts.objectFactory = spring
015. #上面这句一定要注释掉。
016. ### specifies the autoWiring logic when using the SpringObjectFactory.
017. ### valid values are: name, type, auto, and constructor (name is the default)
018. struts.objectFactory.spring.autoWire = name
019. ### indicates to the struts-spring integration if Class instances should be cached
020. ### this should, until a future Spring release makes it possible, be left as true
021. ### unless you know exactly what you are doing!
022. ### valid values are: true, false (true is the default)
023. struts.objectFactory.spring.useClassCache = true
024. ### if specified, the default object type determiner can be overridden here
025. ### Note: short-hand notation is supported in some cases, such as "tiger" or "notiger"
026. ###       Alternatively, you can provide a com.opensymphony.xwork2.util.ObjectTypeDeterminer implementation name here
027. ### Note: if you have the xwork-tiger.jar within your classpath, GenericsObjectTypeDeterminer is used by default
028. ###       To disable tiger support use the "notiger" property value here.
029. #struts.objectTypeDeterminer = tiger
030. #struts.objectTypeDeterminer = notiger
031. ### Parser to handle HTTP POST requests, encoded using the MIME-type multipart/form-data
032. # struts.multipart.parser=cos
033. # struts.multipart.parser=pell
034. struts.multipart.parser=jakarta
035. # uses javax.servlet.context.tempdir by default
036. struts.multipart.saveDir=
037. struts.multipart.maxSize=2097152
038. ### Load custom property files (does not override struts.properties!)
039. # struts.custom.properties=application,org/apache/struts2/extension/custom
040. ### How request URLs are mapped to and from actions
041. #struts.mapper.class=org.apache.struts2.dispatcher.mapper.DefaultActionMapper
042. ### Used by the DefaultActionMapper
043. ### You may provide a comma separated list, e.g. struts.action.extension=action,jnlp,do
044. struts.action.extension=action
045. ### Used by FilterDispatcher
046. ### If true then Struts serves static content from inside its jar.
047. ### If false then the static content must be available at &lt;context\_path&gt;/struts
048. struts.serve.static=true
049. ### Used by FilterDispatcher
050. ### This is good for development where one wants changes to the static content be
051. ### fetch on each request.
052. ### NOTE: This will only have effect if struts.serve.static=true
053. ### If true -&gt; Struts will write out header for static contents such that they will
054. ###             be cached by web browsers (using Date, Cache-Content, Pragma, Expires)
055. ###             headers).
056. ### If false -&gt; Struts will write out header for static contents such that they are
057. ###            NOT to be cached by web browser (using Cache-Content, Pragma, Expires
058. ###            headers)
059. struts.serve.static.browserCache=true
060. ### Set this to false if you wish to disable implicit dynamic method invocation
061. ### via the URL request. This includes URLs like foo!bar.action, as well as params
062. ### like method:bar (but not action:foo).
063. ### An alternative to implicit dynamic method invocation is to use wildcard
064. ### mappings, such as &lt;action name="\*/\*" method="{2}" class="actions.{1}"&gt;
065. struts.enable.DynamicMethodInvocation = true
066. ### Set this to true if you wish to allow slashes in your action names.  If false,
067. ### Actions names cannot have slashes, and will be accessible via any directory
068. ### prefix.  This is the traditional behavior expected of WebWork applications.
069. ### Setting to true is useful when you want to use wildcards and store values
070. ### in the URL, to be extracted by wildcard patterns, such as
071. ### &lt;action name="\*/\*" method="{2}" class="actions.{1}"&gt; to match "/foo/edit" or
072. ### "/foo/save".
073. struts.enable.SlashesInActionNames = false
074. ### use alternative syntax that requires %{} in most places
075. ### to evaluate expressions for String attributes for tags
076. struts.tag.altSyntax=true
077. ### when set to true, Struts will act much more friendly for developers. This
078. ### includes:
079. ### - struts.i18n.reload = true
080. ### - struts.configuration.xml.reload = true
081. ### - raising various debug or ignorable problems to errors
082. ###   For example: normally a request to foo.action?someUnknownField=true should
083. ###                be ignored (given that any value can come from the web and it
084. ###                should not be trusted). However, during development, it may be
085. ###                useful to know when these errors are happening and be told of
086. ###                them right away.
087. struts.devMode = true
088. ### when set to true, resource bundles will be reloaded on \_every_ request.
089. ### this is good during development, but should never be used in production
090. struts.i18n.reload=true
091. ### Standard UI theme
092. ### Change this to reflect which path should be used for JSP control tag templates by default
093. struts.ui.theme=jsxhtml
094. struts.ui.templateDir=template
095. #sets the default template type. Either ftl, vm, or jsp
096. struts.ui.templateSuffix=ftl
097. ### Configuration reloading
098. ### This will cause the configuration to reload struts.xml when it is changed
099. struts.configuration.xml.reload=true
100. ### Location of velocity.properties file.  defaults to velocity.properties
101. ###struts.velocity.configfile = velocity.properties
102. ### Comma separated list of VelocityContext classnames to chain to the StrutsVelocityContext
103. ###struts.velocity.contexts =
104. ### Location of the velocity toolbox
105. ###struts.velocity.toolboxlocation=
106. ### used to build URLs, such as the UrlTag
107. struts.url.http.port = 80
108. struts.url.https.port = 443
109. ### possible values are: none, get or all
110. struts.url.includeParams = get
111. ### Load custom default resource bundles
112. # struts.custom.i18n.resources=testmessages,testmessages2
113. ### workaround for some app servers that don't handle HttpServletRequest.getParameterMap()
114. ### often used for WebLogic, Orion, and OC4J
115. struts.dispatcher.parametersWorkaround = false
116. ### configure the Freemarker Manager class to be used
117. ### Allows user to plug-in customised Freemarker Manager if necessary
118. ### MUST extends off org.apache.struts2.views.freemarker.FreemarkerManager
119. #struts.freemarker.manager.classname=org.apache.struts2.views.freemarker.FreemarkerManager
120. ### See the StrutsBeanWrapper javadocs for more information
121. struts.freemarker.wrapper.altMap=true
122. ### configure the XSLTResult class to use stylesheet caching.
123. ### Set to true for developers and false for production.
124. struts.xslt.nocache=false
125. ### A list of configuration files automatically loaded by Struts
126. struts.configuration.files=struts-default.xml,struts.xml
127. #不能加载struts-plugin.xml，在classpath下面也不能有这个文件
128. ### Whether to always select the namespace to be everything before the last slash or not
129. struts.mapper.alwaysSelectFullNamespace=false

struts.xml 的代码如下，规模开发时，每个人拥有自己的配置文件，避免文件提交冲突。这个文件曾经犯晕，将struts-plugin.xml文件include进来了，结果总是启动不了，报空指针异常。去掉。

01. &lt;?xml version="1.0" encoding="UTF-8"?&gt;
02. &lt;!DOCTYPE struts PUBLIC
03. "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
04. "http://struts.apache.org/dtds/struts-2.0.dtd"&gt;
05. &lt;struts&gt;
06. &lt;include file="struts-default.xml" /&gt;
07. &lt;include file="struts-application.xml" /&gt;
08. &lt;include file="struts-common.xml" /&gt;
09. &lt;include file="struts-dym.xml" /&gt;
10. &lt;include file="struts-zxw.xml" /&gt;
11. &lt;include file="struts-hw.xml" /&gt;
12. &lt;include file="struts-zw.xml" /&gt;
13. &lt;include file="struts-lb.xml" /&gt;
14. &lt;include file="struts-lx.xml" /&gt;
15. &lt;include file="struts-cjh.xml" /&gt;
16. &lt;include file="struts-wyf.xml" /&gt;
17. &lt;/struts&gt;

 

下面的代码比较重要，其中相比webwork2.2.2时，增加了一个拦截器autowiring，把它放在了拦截器栈的最上面。这样就可以将spring配置文件的service示例，以名字的形式，自动注入到action中，完成action对业务service的调用。

01. &lt;?xml version="1.0" encoding="UTF-8"?&gt;
02. &lt;!DOCTYPE struts PUBLIC
03. "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
04. "http://struts.apache.org/dtds/struts-2.0.dtd"&gt;
05. &lt;struts&gt;
06. &lt;package name="default" extends="struts-default"&gt;
07. &lt;interceptors&gt;
08. &lt;!-- Interceptor to catch exceptions and display mapped exception result --&gt;
09. &lt;interceptor name="exceptionHandler" class="com.broadtext.framework.web.interceptor.ExceptionHandleInterceptor" /&gt;
10. &lt;interceptor name="authorizationHandler" class="com.broadtext.framework.web.interceptor.AuthorizationInterceptor" /&gt;
11. &lt;interceptor name="autowiring" class="com.opensymphony.xwork2.spring.interceptor.ActionAutowiringInterceptor"/&gt;
12. &lt;interceptor-stack name="appDefaultStack"&gt;
13. &lt;interceptor-ref name="autowiring"/&gt;
14. &lt;interceptor-ref name="exception"/&gt;
15. &lt;interceptor-ref name="alias"/&gt;
16. &lt;interceptor-ref name="servlet-config"/&gt;
17. &lt;interceptor-ref name="prepare"/&gt;
18. &lt;interceptor-ref name="chain"/&gt;
19. &lt;interceptor-ref name="model-driven"/&gt;
20. &lt;interceptor-ref name="fileUpload"/&gt;
21. &lt;interceptor-ref name="static-params"/&gt;
22. &lt;interceptor-ref name="params"/&gt;
23. &lt;interceptor-ref name="conversionError"/&gt;
24. &lt;interceptor-ref name="exceptionHandler" /&gt;
25. &lt;interceptor-ref name="authorizationHandler" /&gt;
26. &lt;/interceptor-stack&gt;
27. &lt;/interceptors&gt;
28. &lt;default-interceptor-ref name="appDefaultStack" /&gt;
29. &lt;global-results&gt;
30. &lt;result name="appBaseError"&gt;/pages/common/appBaseError.jsp&lt;/result&gt;
31. &lt;result name="runtimeError"&gt;/pages/common/runtimeError.jsp&lt;/result&gt;
32. &lt;result name="unkownError"&gt;/pages/common/runtimeError.jsp&lt;/result&gt;
33. &lt;/global-results&gt;
34. &lt;/package&gt;
35. &lt;/struts&gt;

 

这是userService的spring配置代码，使用的是Spring2.0.1。

1. &lt;bean id="userService" class="com.broadtext.cmshzhgl.security.service.impl.UserServiceImpl"&gt;
2. &lt;property name="dao" ref="baseDao"&gt;&lt;/property&gt;
3. &lt;property name="utilService" ref="utilService"&gt;&lt;/property&gt;
4. &lt;property name="roleService" ref="roleService"&gt;&lt;/property&gt;
5. &lt;/bean&gt;

这是测试Action的 代码，用来测试一下是不是可以调用spring配置的业务service了。

01. public class LoginAction extends BaseAction {
02. private UserService userService;
03. public String login(){
04. String aaa = userService.getTestString();
05. System.out.println(aaa);
06. return SUCCESS;
07. }
08. public void setUserService(UserService userService) {
09. this.userService = userService;
10. }
11. }

action与jsp之间的struts配置文件代码

01. &lt;?xml version="1.0" encoding="UTF-8"?&gt;
02. &lt;!DOCTYPE struts PUBLIC
03. "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
04. "http://struts.apache.org/dtds/struts-2.0.dtd"&gt;
05. &lt;struts&gt;
06. &lt;package name="test" extends="default" namespace="/pages/test"&gt;
07. &lt;action name="login" class="com.broadtext.cmshzhgl.security.action.LoginAction" method="login"&gt;
08. &lt;result name="success"&gt;/pages/common/currentUser.jsp&lt;/result&gt;
09. &lt;/action&gt;
10. &lt;/package&gt;
11. &lt;/struts&gt;

 

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