Migrating to Apache Struts 2: A tutorial for Struts 1 Developers.
Lab Notes
The tutorial features several mini-labs that step through migrating a Struts 1 application to Struts 2.
Lab: Where should a migration begin?
The elements we need to add are
- The Struts 2 Filter
- A filter mapping
- Struts2 JARs
Lab: Is the Struts 2 configuration file different?
To convert a copy of our Hello World configuration from Struts 1 to Struts 2, first we
- Replace the DTD
- Change <struts-config> to <struts>
- Add <include file="struts-default.xml"/>
- Remove the <form-beans> element
- Change <action-mappings> to <package name="hello-default" extends="struts-default">
- Update each <action> element
To update each <action> element, we
- Remove from <action> the "name" attribute
- Change the <action> "path" attribute to "name", and "type" to "class"
- Change the <forward> element into a <result> element.
Lab: Do Action classes change too?
To convert our Hello World Action class from Struts 1 to Struts 2, first we
- Update or remove imports. (A well-factored Struts 2 Action should not need to refer to anything from the servlet.http package.)
- Move the Message property from the ActionForm to the Action, and remove the ActionForm object completely. It is obsolete.
- Change the Struts 2 Action to extend ActionSupport. Extending ActionSupport is optional, but it is usually helpful. In this case, we are just using the predefined SUCCESS token.
- Remove the ActionForm cast and reference the Message property directly, since it is now a property on the Action class.
- Remove the SUCCESS static, since it is already defined on ActionSupport.
Lab: What about the tags?
To convert from Struts 1 to Struts 2 the Hello World server pages shown in the listings, first we
- Replace the <%@ taglib @%> directive
- Change the <bean:write ... /> tag to a <s:property ... /> tag
Lab: Can we still localize messages?
- Lookup message by key in Action
- Set localized message to property
- Tags can output localized messages, based on a key
Lab: How do we change locales?
Pass "request_locale=es" as parameter
- "es" can be any standard locale code.
- Framework updates locale for session.
Lab: Does Struts 2 use commons Validator?
Struts 1: numeric parameter
errors.required= {0} is required
Struts 2: expression language
requiredstring= ${getText(fieldName)} is required.
