migrating from Struts 2.0 to 2.1

来源:百度文库 编辑:神马文学网 时间:2024/04/24 05:07:33
Troubleshooting guide migrating from Struts 2.0.x to 2.1.x Edit Page   Browse Space   Add Page   Add News Added by Giovanni Azua, last edited by Greg Lindholm on Mar 05, 2009  (view change) About the Migration guide

This guide describes how to migrate an existing Struts 2.0.x application to Struts 2.1.x. It is intended to be read from top to bottom but you may skip-ahead to known problems and common exceptions. Please edit this page or provide comments if you encounter additional issues.

Table of Contents

/**/
  • Update Dependencies
  • Update Custom Type Converters
  • Update struts.xml Configuration
  • Migrate plugin configuration
    • Tiles 2
    • Ajax Theme / Dojo
  • Convert EL expressions to OGNL
  • Refactor usage of static methods via OGNL
  • Reduce verbose logging
    • Missing Properties
    • TextProvider missing keys
    • Redirect and RedirectAction parameters
  • Update Unit Tests
  • Explicitly set the includeParams attribute on all url tags to "get" if it is not specified.
  • Filter Mapping, default Action extensions, and Servlets
  • Trouble-shooting
    • Result type "redirect-action" was renamed to "redirectAction":
    • Tiles 2.1.x plugin tiles.xml now requires DOCTYPE:
    • is obsolete, use a different theme:
    • Ajax UI tags were moved to the new dojo plugin, use /struts-dojo-tags taglib instead of (or in addition to) /struts-tags:
    • User-defined converter (subclassing StrutsTypeConverter) will no longer be needed when using datetimepicker:
    • ActionMappingParams

Update Dependencies

Update your project dependences to use struts2-core-2.1.x and the plugins struts2-core-2.1.x.

There have been significant changes to the Configuration API between 2.0 and 2.1. Third-party plugins for 2.0 may not be compatible with 2.1.x.

Maven users can update their project's pom.xml to reference the new core and plugin versions. Ensure no dependencies in the freemarker groupId are used as the latest version used by Struts is now under the org.freeemarker groupId and will cause classpath conflicts.

If you need releases not considered General Availability you can use a staging repository where they are usually available:

Non-Maven users can can download the jars and dependencies in a Distribution 2.1.6

DELETE OLD VERSIONS NOW: It's essential that old versions of the jars are removed from your project as well as the deployment directories. Don't trust your IDE to delete unused versions.
eg. Your application is now dependent on xwork-2.1.x, not xwork-2.0.x. Ensure xwork-2.0.x is completely removed or you will encounter compile-time and run-time exceptions.

Update Custom Type Converters

If your project implements custom type converters you may need to change the imports statements to use the com.opensymphony.xwork2.conversion package. Failing to do so will cause a compile-time error.

Before:

After:

Note: this may also affect conversion classes listed in the xwork-conversion.properties file.

Update struts.xml Configuration

Results and Interceptors have been renamed to use camelCase instead of hyphenated names (eg. now redirectAction instead of redirect-action).
Review all custom interceptor stacks, interceptor refs and results in struts.xml and remove the hyphen.

Interceptors:

From To external-ref externalRef model-driven modelDriven static-params staticParams scoped-model-driven scopedModelDriven servlet-config servletConfig token-session tokenSession

Results:

From To redirect-action redirectAction plaintext plainText Forgetting to rename an interceptor or result reference will prevent your WebApp from starting. The following is a typical stacktrace for an invalid result type.

Migrate plugin configuration

Tiles 2

If you use the Tiles 2 plugin, check your tiles.xml file(s) to ensure they contain a DOCTYPE.

A missing DOCTYPE in your Tiles configuration will prevent your WebApp from starting. The following is a typical stacktrace for a missing Tiles DOCTYPE.

Ajax Theme / Dojo

If your WebApp uses the ajax theme bundled with Struts 2.0 (ajax tags), you need to update every pageas the Dojo support has been moved into a separate plugin and tag library.

1. Add the Dojo plugin as a new dependency for your project

2. Modify ALL pages that use the ajax theme to use the Dojo tag library.

For each page there are three tasks to complete:

  1. reference the new Dojo tag library
  2. update the head tag
  3. update all ajax themed tags
  4. check inline scripts
  5. check inline widgets If this is a major undertaking for your application, it's recommended to modify, test and validate each page one at a time.

Reference the new Dojo Tag Library

Reference the new taglib in your JSP or FTL pages.
Before

After

Many pages will require both the core and dojo tags. The sx: prefix is preferred for the Dojo tags.

Update the head tag

Review the new attributes of the new head tag. You need to decide which values for the cache, compressed, parseContent and extraLocales attributes are appropriate for your application. These settings have major performance implications.

Before:

After:

Update all ajax themed tags

Some tags are available only in the Dojo plugin taglib. It will be immediately obvious from your IDE that these tags need to change to the sx: prefix.
Some tags are available in both the core and Dojo plugin taglibs. Any tag that uses the ajax theme needs to be changed to the sx: prefix.

Before:

After:

If you forget to migrate a tag reference from the core to the dojo plugin you will receive an exception similar to the one below:

Check inline javascript

If your ajax html results contain inline javascript that needs to be executed after updating the DOM, ensure your ajax tags set executeScripts="true" and separateScripts="true". These attributes instruct the widget to search for javascript in the result,

extract it, update the DOM with the result and then execute the javascript.

eg. The following bind will execute scripts within the result when the Submit button is pressed

Check inline widgets

If your ajax html results include dojo widgets, ensure the head tag's parseContent attribute is true. This instructs dojo to parse the ajax responses for widgets. Note that Struts2.0 always enabled this feature and it could not be turned off. Struts2.1 disables this feature and allows it to be turned on as there is a significant performance impact.

Convert EL expressions to OGNL

Struts2.1 tags do not allow evaluation of JSP EL within their attributes. Instead, Struts2 tags evaluate attribute values as OGNL. Allowing both expression languages within the same attribute opens major security vulnerabilities.

todo: how to convert

Forgetting to convert attributes to OGNL expressions will produce exceptions similar to the one below:

Refactor usage of static methods via OGNL

The default behaviour of allowing access to static methods (via an expression like "@org.somewhere.FooUtls@fooMethod()") has been disabled for security reasons in the 2.1 branch. See https://issues.apache.org/struts/browse/WW-2160 for details. You should either remove these cases, or re-enable the use of static methods explicitly in your configuration by using the struts.ognl.allowStaticMethodAccess property.

Reduce verbose logging

Struts 2.1 introduces more verbose logging than Struts 2.0. While extremely valuable, some users may find these annoying.

Missing Properties

This message states that the framework searched for a property in the value stack and failed to find it.

To hide these messages, turn off the WARN level logging for OgnlValueStack.

eg. Include a new limit category in your log4j.xml file (only log errors or worse):

TextProvider missing keys

These messages state that the framework searched for text in a resource bundle and failed to find it.

To hide those messages, turn off the WARN level logging for TextProviderHelper.

eg. Include a new limit category in your log4j.xml file (only log errors or worse):

Redirect and RedirectAction parameters

A (incorrect) change in 2.1.6 has ObjectFactory reporting ERROR's when you attempt to set parameters on a Redirect result.

So a redirect result that sets a parameter like this:

Will always give you an error like this:

You can safely ignore and suppress the error by setting the logging level to 'fatal' like this:

Update Unit Tests

There are two known major migration issues affecting user's unit tests.

  • The Configuration API now uses a Builder pattern
  • The ActionContext.getContext() method does not create a context on demand.

Review the Unit Tests included with Struts2 for recommended practices to setup the Configuration and context.

A quick fix for ActionContext.getContext() returning null :

Explicitly set the includeParams attribute on all url tags to "get" if it is not specified.

The default value of this attribute changed from "get" to "none". See https://issues.apache.org/struts/browse/WW-2901

Filter Mapping, default Action extensions, and Servlets

The default action extension list (struts.action.extension) has changed from just 'action' to 'action' plus "" (no extension). If your application has servlets or other requests that have no extension then they will be mistaken as actions and you will get a "There is no Action mapped for ..." exception like below.

To fix this problem you can explicitly set the default action extension back to just "action" or you can change your filter mapping for the struts filter.

To set the action extension list back to just "action" add this to your struts.xml file:

If all your actions use the ".action" suffix then you may want to change the Struts filter-mapping so that only actions are handled by the struts filter. Also the FilterDispatcher has been deprecated and replaced by StrutsPrepareAndExecuteFilter.

The web.xml before (matching all requests):

Change to use the new StrutsPrepareAndExecuteFilter and match just struts requests.

The web.xml after:

Trouble-shooting

The issues are listed in the same order as encountered after changing jars over from 2.0.x to 2.1.x. Noteworthy, the migration was done under the following setup: Fedora core 6, JDK 1.6.0_2 and Tomcat 6.0.10 running from MyEclipse plugin.

Result type "redirect-action" was renamed to "redirectAction":

The struts.xml before:

To resolve this issue modify the struts.xml action definition to:

or to:

Tiles 2.1.x plugin tiles.xml now requires DOCTYPE:

Problem Symptom:

Reason for this

This is due to the fact that Tiles 2.0.5 now turns validation on as default.

The tiles.xml page before:

To resolve the issue simply add:

is obsolete, use a different theme:

Problem Symptom: Accessing any page that includes produces the following error:

Resolution is to either remove the "" or use a different theme e.g.

Ajax UI tags were moved to the new dojo plugin, use /struts-dojo-tags taglib instead of (or in addition to) /struts-tags:

Problem Symptom: Accessing a page containing ajax UI tags through using the struts 2 taglib will produce the following error:

JSP before migration:

Resolution is to import and use struts-dojo-tags plugin instead:

Other Ajax UI tags were also moved to the dojo plugin

Note the use of remote div is now through the dojo plugin taglib sx. Other ui tags are also no longer available through the /struts-tags taglib but only through the /struts-dojo-tags taglib: datetimepicker and autocompleter.

User-defined converter (subclassing StrutsTypeConverter) will no longer be needed when using datetimepicker:

Problem Symptom: Your custom StrutsTypeConverter implementation does not longer work in version 2.1.x. In 2.0.x you needed to implement a custom StrutsTypeConverter e.g. StringToDateConverter to be able to parse and convert to Date the String posted from a datetimepicker control into the action. In version 2.0.x datetimepicker was posting a String formatted as specified in the "displayFormat" field e.g.

In version 2.1.x datetimepicker will post a String Date in RFC 3339 format, so you can define your setter to receive a Date directly and avoid using converters for this purpose e.g.

ActionMappingParams

Parameters set by the action mapping are not set/not available through ParameterAware (This change is only needed when going to 2.1.x (where x>0))

These are now only available if you use the new interceptor named "actionMappingParams".