January 17, 2021
Hot Topics:

Comparing the Struts 1 and Struts 2 Web Application Frameworks

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

DefectsAction's delete method

public ActionForward delete(
      ActionMapping mapping,
      ActionForm form,
      HttpServletRequest request,
      HttpServletResponse response) {

   long id = Long.parseLong(request.getParameter("id"));

   //Invoke business layer to delete the object using the id...

   return mapping.findForward("list");

Struts 1 developers use the Apache Commons Validator framework to validate data. To enable validation, you must register the Validator plug-in in the struts-config.xml file, and then make sure your own ActionForms extend ValidatorForm instead of ActionForm. You can declare validation rules in a validation.xml file. For example, the validation.xml file you use states that a defect's description is required. The 'validate' method is invoked by the Action before saving a record. When the user does not provide a description, the Struts ActionErrors collection is populated and its contents displayed on the view page with the help of an <html:errors/> tag.

<form name="actionForm">
   <field property="defect.description" depends="required">
      <arg0 key="defects.description"/>

Validating the form upon save

   ActionMessages errors = form.validate(mapping, request);
   if (!errors.isEmpty()) {
      saveErrors(request, errors);

Validation error message is displayed on addDefect.jsp

Click here for a larger image.

That just about wraps up your look at the Struts 1 version of defect-tracker. Hopefully, it will have been mostly a review for you. However, before you move on, it is obvious that you have not really produced a working application. Struts provides the web framework, but what about the portion of the application where you implement your business logic and save data to the database? Struts cannot help with this piece. Business logic is best kept in a separate business layer implemented with EJBs (Enterprise JavaBeans) or plain Java classes that have no concept of Struts. One popular framework for orchestrating the business layer logic is the Spring framework. Spring, for those unfamiliar with the framework, is essentially a dependency injection framework that declares Java classes and their dependencies in a configuration file. At runtime, instead of having to obtain dependent objects manually, they are injected for you. Beyond this, Spring offers numerous other useful features and integration points for many other Java frameworks and toolsets. For defect-tracker, I have chosen the Spring framework primarily for two reasons. First, Spring is very popular and integrates well with Struts. A great many Struts 1 applications already use Spring. Secondly, Struts 2, as you will see, is even more closely tied to Spring, making an even stronger argument for its use with Struts.

Because so many Spring developers use Struts, Spring offers supporting classes for working with Struts 1. You have two options: Use Spring's ContextLoaderPlugin to manage injections or use Spring's ActionSupport classes to obtain Spring's application context. Both options are fairly easy to implement. This article is about Struts, so I will not dive into the details of the business and data tiers of the application, but I have implemented these two layers using Spring, with the help of the Hibernate framework for persistence. Here, briefly, are the steps I have used to wire Spring to the defect-tracker application.

First, the Spring configuration file needs to be mentioned in the web.xml file:



Using Spring's ActionSupport classes is the simplest approach and the one used here. You obtain a Spring-managed bean in your Action method and use it to invoke business logic. For example, in the save method on the DefectsAction class, you obtain Spring's context, and invoke the methods provided in an implementation of the DefectManager business interface:


WebApplicationContext ctx = getWebApplicationContext();
DefectsManager mgr = (DefectsManager) ctx.getBean("defectsManager");

DefectManager is where any business rules should be coded. To illustrate this approach, when the user enters a resolution to a defect, the DefectManager will automatically audit that user (obtaining an actual name in a real application of course), and set the Defect's isResolved flag to 'true'. Then, Spring calls upon Hibernate to handle the necessary database interaction. Click here to view the complete 'Spring-enabled' Action classes: DefectsList and DefectsAction.

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