Dashboard > Struts University > Home > MailReader Training Course > Topics > Migrating-Notes
Added by Ted Husted, last edited by Ted Husted on Jan 21, 2007  (view change)

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

  1. The Struts 2 Filter
  2. A filter mapping
  3. 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

  1. Replace the DTD
  2. Change <struts-config> to <struts>
  3. Add <include file="struts-default.xml"/>
  4. Remove the <form-beans> element
  5. Change <action-mappings> to <package name="hello-default" extends="struts-default">
  6. Update each <action> element

To update each <action> element, we

  1. Remove from <action> the "name" attribute
  2. Change the <action> "path" attribute to "name", and "type" to "class"
  3. 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

  1. Update or remove imports. (A well-factored Struts 2 Action should not need to refer to anything from the servlet.http package.)
  2. Move the Message property from the ActionForm to the Action, and remove the ActionForm object completely. It is obsolete.
  3. 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.
  4. Remove the ActionForm cast and reference the Message property directly, since it is now a property on the Action class.
  5. 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

  1. Replace the <%@ taglib @%> directive
  2. 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.

Site running on a free Atlassian Confluence Open Source Project License granted to OSS. Evaluate Confluence today.
Powered by Atlassian Confluence, the Enterprise Wiki. (Version: 2.5.5 Build:#811 Jul 25, 2007) - Bug/feature request - Contact Administrators