May 25, 2018
Hot Topics:

Comparing the Struts 1 and Struts 2 Web Application Frameworks

  • November 8, 2007
  • By Michael Klaene
  • Send Email »
  • More Articles »

On to Struts 2...

Now that you've seen Struts 1, it's time to have a second go around at defect-tracker, this time using Struts 2. My goal is to not deviate too much from the original design to to keep comparisons valid. To implement defect-tracker as a Struts 2 application, the first thing you will need to do is modify the web.xml file. Struts 1 used a Java Servlet called ActionServlet to intercept requests matching a specified URI pattern ('*.do' by default). Struts 2 opts to use a Servlet Filter instead. The default extension for Struts 2 is not "*.do" but "*.action". This extension does not need to be specified in the Filter mapping, but will be assumed. It can be overridden, as many of Struts default properties can by editing a 'struts.properties' file and placing it in the classpath. Due to the different ways that each version of Struts maps requests, developers should have no problem using both Struts 1 and Struts 2 together in the same application if they choose.

Filter mappings in Struts 2 web.xml



Struts 1 utilized the 'struts-config.xml' file for Struts configuration. For example, the struts-config is where a developer maps path names to Actions, defines ActionForms that go along with those Actions, and specifies ActionForwards for different types of Action outcomes. Well, a lot has changed in this area in Struts 2. For starters, Struts 2 replaces struts-config with a file called 'struts.xml'. The 'struts.xml' file can be placed anywhere in the classpath. Peering into struts.xml, you will notice some big changes. First and foremost, ActionForms are gone. Struts 2 places no restrictions on where a developer stores a page's data. For example, you can pull the form data directly from the Action class (Actions have stuck around for Struts 2). Struts 2 replaces ActionForward objects with Result objects. Similar to Struts 1, an Action returns a string result that is matched against an entry in struts.xml. Below is how your two Actions in defect-tracker are configured in Struts 2. Note the various changes in element names too. For the most part, these new element names are fairly intuitive of what they represent.

<include file="struts-default.xml"/>

<constant name="struts.custom.i18n.resources"
          value="MessageResources" />

<package name="default" extends="struts-default">

   <action name="list" class="web.DefectsList">

   <action name="action_*" method="{1}"
      <result name="input">/pages/editDefect.jsp</result>
      <result type="redirect">list.action</result>



In struts.xml, developers group related-actions into a 'package'. You are simply using the default package here. However, if you decided to create a packaged called 'crud' with a similarly titled 'namespace' attribute, URI requests for Actions in that package would take the form of '/crud/*.action'. Developers can define constants here to override some default settings in the Struts 2 framework. Looking at the Action declarations, you will notice that a single result can be specified without a name. Struts 2 assumes this default entry to be the 'success' result and there is no need to specify it. If you will recall, you used a Struts 1 DispatchAction to handle the multiple CRUD events for a defect. There is no need to extend DispatchAction (in fact, DispatchAction does not exist in Struts 2). Rather, a wildcard/placeholding mechanism can be used instead to transfer control to a specific Action method. For example, in the Struts 2 DeffectsAction, a URI of 'action_go' will result in the invocation of a 'go' method on the relevant Action class.

The 'list.action' URI request maps to your Struts 2 version of the DefectsList Action class. Here is the code:

package web;

import business.DefectsManager;
import com.opensymphony.xwork2.ActionSupport;
import java.util.List;

public class DefectsList extends ActionSupport  {

   private List defects;
   private DefectsManager defectsManager;

   public List getDefects() {
      return defects;
   public void setDefects(List defects) {
      this.defects = defects;

   public void setDefectsManager(DefectsManager defectsManager) {
      this.defectsManager = defectsManager;

   public String execute() {
      this.defects = this.defectsManager.getDefects();

      return SUCCESS;


Page 4 of 6

Comment and Contribute


(Maximum characters: 1200). You have characters left.



Enterprise Development Update

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

By submitting your information, you agree that developer.com may send you developer offers via email, phone and text message, as well as email offers about other products and services that developer believes may be of interest to you. developer will process your information in accordance with the Quinstreet Privacy Policy.

Sitemap | Contact Us

We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.
Thanks for your registration, follow us on our social networks to keep up-to-date