March 2, 2021
Hot Topics:

Comparing the Struts 1 and Struts 2 Web Application Frameworks

  • By Michael Klaene
  • Send Email »
  • More Articles »

Data form for creating and modifying defects

<s:form action="action!save" method="post"  >
   <s:hidden name="defect.id" value="%{defect.id}" />
   <s:textfield label="%{getText('defect.description')}"
      name="defect.description" value="%{defect.description}"
      size="100" maxlength="200" required="true" />
   <s:select label="%{getText('defect.priority')}"
      name="defect.priority" value="%{defect.priority}"
      list="#{'1':'1','2':'2','3':'3','4':'4'}" />
   <s:if test="defect.id > 0">
      <s:textfield label="%{getText('defect.resolution')}"
         name="defect.resolution" value="%{defect.resolution}"
         size="100" maxlength="200" />
   <s:submit value="%{getText('defect.editrecord')} " />
   <s:submit value="%{getText('defect.cancel')}" action="list" />

Struts 2 utilizes the OGNL library to provide support for expressions. This allows you to efficiently display labels and model data together. The Struts 2 <if> tag can be used to toggle the display of the resolution field, depending on whether an existing id can be found. The form's action attribute specifies 'action!save', meaning the 'save' method will be invoked upon submit.

Finally, I will quickly discuss validation in Struts 2. Struts 2 uses the validation framework provided by WebWork. It too offers a similar declarative method for validation, but it is a more fine-grained approach. You have two choices, validate on a per-model basis or per-action. To validate at the Action level, you would simply create a file that takes the name {your action}-validation.xml and place it in the same package as the Action class. To validate at the model level, you would create a similar file that takes the name of the model object. A common approach is to validate at the model level, as you have done in the file Defect-validation.xml, then direct your Action validation file to validate per the rules in the model's validation file:

Defect validation

   <field name="defect.description">
      <field-validator type="requiredstring">
         <message key="errors.required"/>

Action validator, delegates validation to the model with the 'type="visitor" ' attribute

   <field name="defect">
      <field-validator type="visitor">
         <param name="appendPrefix">false</param>

In Struts 2, validation is enabled by default. There is no 'validate' method to invoke. Struts 2 also avoids firing validation on Actions that return 'input' so you can avoid writing code to suppress validation when it is not desired. There is a similar set of tags in Struts 2 for displaying validation error messages.


There is no denying that there is a lot to learn in Struts 2. Changes have been numerous, but the influence of its predecessor is still evident throughout the framework. Most of the new features and capabilities provided by Struts 2, I think you will agree, make it a worthy successor. This article was intended to provide a brief comparison between Struts 1 and Struts 2. The source files from this article can be downloaded here.There is a good deal of information that this article did not cover and I strongly encourage you to visit the Struts 2 web site and peruse the tutorials and documentation available there to make the most out of the framework.

About the Author

Michael Klaene is a Principal Consultant with Sogeti USA. He has over 10 years of experience in IT, and specializes in J2EE, .NET, and Oracle design and development.

Page 6 of 6

This article was originally published on November 8, 2007

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date