March 7, 2021
Hot Topics:

Comparing the Struts 1 and Struts 2 Web Application Frameworks

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

DefectsList execute method

public ActionForward execute(
      ActionMapping mapping,
      ActionForm form,
      HttpServletRequest request,
      HttpServletResponse response) {
   DefectsListForm defectsListForm = (DefectsListForm) form;

   //Get data from business layer and assign it to the form

   return mapping.findForward("list");

The execute method obtains the application data and assigns it to a Collection of Defect instances using the setDefects() method on ActionForm DefectsListingActionForm class.

DefectsListForm's Defects property

   private List defects;

   public List getDefects() {
      return defects;

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

The defects.jsp file utilizes Struts 1 tag libraries to display these defects and to display labels and messages from a .properties file that you declared in struts-config.xml.

defects.jsp displays all defects

Click here for a larger image.

The user can choose to add, edit, or delete a defect. When the user wants to edit a record, for example, the handler first obtains the ID from the HTTPServletRequest, which is passed as a parameter to the Action's execute method. The execute method then will invoke a method in the business layer to obtain the defect, and then it transfers to an editable form which will be populated with data. When adding a new defect, a blank form appears. Because it does not make sense to allow a user to resolve a defect they have just entered, the add form provides only a subset of fields.

addDefect.jsp to enter information about new defects

Click here for a larger image.

editDefect.jsp for updating defects

Click here for a larger image.

Struts 1 does not always handle the conversion of input strings very well so sometimes the developer needs to convert form data before passing it to a domain object. The DateConverter class accomplishes this for date fields. After a successful add, edit, or delete, the user is returned to the list again. The first Action we saw, DefectsList, had its 'execute' method automatically invoked. Sometimes an execute method is all you need, however, in order to help developers consolidate related actions in a single Action object, Struts 1 offers the 'DispatchAction' class. DispatchAction uses the 'method' request parameter to tell the Struts Action which method to invoke. So,instead of 'execute', we can define methods like 'add', 'edit', and 'delete' to handle each type of request. In order to use DispatchAction, your Action object needs to extend DispatchAction (not Action) and define a form parameter that identifies the method to invoke. The method for deleting a defect from DefectsAction is shown here:

Page 2 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