问题
We are currently using Struts2.0, and planning to upgrade it to 2.5.X. Is there any migration process which helps me to directly upgrade to 2.5.X?, cause V2.0 is very old and lot of new things are implemented in later versions.
Some have suggested that don't migrate directly to 2.5.X as there are a lot of changes happened. However, first upgrade it to 2.3.X and then 2.5.X.
- How to migrate Struts from 2.0 to 2.5?
- What to modify when upgrading Struts?
回答1:
I have updated links in my answer to your question mentioned above. However there's no direct link to the migration guide. You can use this link if it could help you in the migration process.
Struts 2.3 to 2.5 migration
Update Struts dependencies to 2.5.
Remove the following plugin dependencies because they were dropped and aren't supported anymore.
- Dojo Plugin
- Codebehind Plugin
- JSF Plugin
- Struts1 Plugin
Please be aware that the framework is using Log4j2 now as a main logging layer, the existing old logging layer is deprecated and will be removed soon. Log4j2 supports many different logging implementations, please check documentations for more details.
StrutsPrepareAndExecuteFilter
The org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter was moved to org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter.
In web.xml replace this:
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
with that:
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
There were other package changes, please read Version Notes 2.5 for more details.
DTD Struts DTD was updated to 2.5 version.
In struts.xml replace 2.3 DTD version:
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd">
with 2.5:
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN" "http://struts.apache.org/dtds/struts-2.5.dtd">
HTML 5 All the core themes are now HTML 5 compliant which means using a required attribute in your tags will produce a proper browser's validation.
Tags attributes The id attribute was replaced with var attribute in the following tags.
<s:action> <s:append> <s:bean> <s:date> <s:generator> <s:iterator> <s:merge> <s:number> <s:set> <s:sort> <s:subset> <s:text> <s:url>
If you have something like that in your code:
<s:url id="url" action="login"> change it to:
<s:url var="url" action="login">
The <s:set> tag name attribute is replaced with var attribute.
From:
<s:set id="str1" value="'string1 value'" /> <s:set name="str2" value="'string2 value'" /> to:
<s:set var="str1" value="'string1 value'" /> <s:set var="str2" value="'string2 value'" /> Also escape attribute was renamed to escapeHtml attribute.
From:
<s:property escape="true" var="someProperty"/> to:
<s:property escapeHtml="true" var="someProperty"/> Div tag The <s:div> tag was dropped.
Replace <s:div> with plain HTML tag.
Field names If you have field names which starts with single lower case letter, for example:
private String sTrng; public String getSTrng() {...} public void setSTrng(String str) {...}
change accessors to getsTrng and setsTrng.
Or better yet, change field names to not contain single lower case letter:
private String strng; public String getStrng() {...} public void setStrng(String str) {...}
For additional info see WW-3909.
Tiles Depending on from which version of struts you upgrade and whether you used tiles-plugin or tiles3-plugin you may need to do different steps.
Struts 2.5 just provides a tiles-plugin which uses Tiles3. So support for Tiles2 has been dropped as well as the name tiles3-plugin.
Now the only maven dependency looks like this:
maven dependecy for tiles-plugin org.apache.struts struts2-tiles-plugin ${struts2.version}
You may need to update DTD in your tiles.xml files to Tiles3:
tiles3 dtd
A Listener in web.xml is required. It is not necessary to configure paths to tiles.xml files here as they are picked up automatically.
StrutsTilesListener in web.xml org.apache.struts2.tiles.StrutsTilesListener
Optionally you may remove TilesDefinitions from XML and annotate actions instead. See Tiles Plugin for more details.
Temp/Work directory of ApplicationServer/ServletContainer Users reported it was necessary for them to remove temp/work directory of their ApplicationServer/ServletContainer. Likely to force server to recompile JSPs.
New Locale aware conversion logic As from Struts 2.5.12 a new conversion logic was introduced which can affect your applications when using uncommon solutions. One of these is to use a number literals in Freemarker template. In such case Freemarker treats them as numbers (as BigDecimals) and Struts logic converts them to a string with decimal zero, see the example below:
<@s.textfield name="userId" value=35/> this snippet will produce the following Html control:
To resolves this problem you must add quotes around the value:
<@s.textfield name="userId" value="35"/> This is due how Freemarker treats a number literals.
来源:https://stackoverflow.com/questions/57973028/how-to-migrate-struts-from-v2-0-to-v2-5