Tip

Produce more flexible Web apps using Struts

You know how important struts are to a car, but what about struts for Web development? If you're building Web applications -- or looking to -- you should know about the Apache Struts framework. Many companies, including IBM, are standardizing Web applications around the Struts framework to lower development costs and produce more flexible Web applications.

Using Struts as delivered by Apache, requires Java Web application development skills. IBM's new WebSphere Development Studio Client Advanced edition, version 5 is lowering the skill requirements somewhat with wizards that help generate Struts code. Without Java skills, you should be able to appreciate the payoffs for the Struts framework and understand where you might want to have developers use it in your applications.


Jim Mason

If you've built Web applications integrating Java Server Pages (JSPs), servlets, Java beans and more, you may have created your own framework to coordinate the application flow. It's time consuming to build one, requires significant expertise and isn't very portable since no one else is using your framework.

All but the most trivial Java Web applications have a wide number of architectural issues to deal with on design:

  • How to build Web applications productively with HTML, Java beans, JSPs, Javascript, XML
  • How to create modular designs following MVC concepts separating view, controller, logic
  • How to improve adaptability of applications for change in environment, configuration
  • How to support dynamic workflow
  • How to reuse common services easily in Web applications as beans and JSP tags
  • How to make Web applications easy to maintain
  • How to standardize Web design across applications and developers
  • How to validate form data and handle errors consistently
  • How to accommodate internationalization of applications easily

Struts can help with many of those issues. The Struts framework will probably be a framework you come to use in almost all of your Web application development. It has grown rapidly in popularity since Craig R. McClanahan introduced it a few years ago. The Struts project team now maintains the framework. A wide variety of vendors, including IBM, have adopted Struts as the primary framework for building Web applications. I think customers always benefit when IBM adopts an open-source framework as opposed to building more proprietary ones. Struts is now supported in a variety of IBM environments: WebSphere Development Studio tools and the WebFacing Advanced tool.

The Struts framework is packaged as a set of jar files and tag libraries. You should use a Java Web application server that supports Servlets 2.3 and JSP 1.2 levels: Apache Tomcat 4.x, WebSphere Express version 5, WebSphere version 5, etc.

Based on MVC concepts
Struts is based on the Model-View-Controller-2 (MVC2) architecture for a Web application.

  • The Model represents the objects providing the business logic in the application (here, usually Java beans, data, legacy applications). The model is usually composed of business objects (things like anOrder, anItem) and actions (such as shipOrder, allocateQuantity etc). In Struts, the Action and ActionForm classes can be considered part of the model.
  • The View is the objects that present the application interface (usually JSPs, JSP tag libraries, HTML pages and more). Struts uses Struts tag libraries and reuses many of the Apache Taglib project libraries as well. The Apache JSP tag libraries cut down the work to build JSP applications in many areas, including database access.
  • The Controller controls the application flow: the controller receives the user request (usually on form input), processes the request invoking the Model objects and returns a response by invoking the View objects. The controller is usually a servlet. In Struts, the class ActionServlet is usually the single servlet for an application. It is configured to handle requests for any action needed in the application.

The advantages for Struts- and MVC-based applications are many:

  • They promote reuse of application logic
  • They separate presentation, logic and control
  • Modular applications are more maintainable
  • Struts actions are configurable at runtime

The challenge for a component-based application like Struts is the developer's ability to understand the application easily decreases.

Eclipse is a sophisticated, open-source Interactive Development Environment (IDE) that allows a developer to work more productively in a component-based application. The new IBM WebSphere Development Studio Client Advanced edition, version 5 (shipping at the end of April), will make Struts development even easier.

Struts follows Web application processing life cycle
Struts applications have a life cycle like any Web application. For any of the application events, a Struts application can customize the application behavior by overriding methods or registering listeners. The application events include the following:

  • Application startup
  • Session startup
  • Request processing
  • Session termination
  • Application termination

If needed, you can define your own sub-class of ActionServlet and add more processing control, overriding the process method.

In addition to the defined extension and control points in Struts, Servlets 2.3 allows the use of Filters for more fine-grained control over request, response processing of servlets. Using a chain of command pattern, Filters are servlets that can be chained together to handle processing the HTTP request or response to a client. Look first to accomplish what you want in Struts. If that fails, look at using Filters to customize Web application environmental processing further.

Struts services
Since Struts builds on many of the Apache JSP tag libraries, it offers a rich set of services:

  • Simplified JSPs with a rich tag library for HTML, Java beans, conditional logic
  • Configurable actions (in struts-config.xml) using Action sub-classes to handle named requests by URL
  • Form validation of data using an ActionForm sub-class
  • Customizable standard error handling
  • Configurable application workflow using ActionForward objects to direct the chain of request processing (completing an OrderPosting successfully would lead to an OrderAcknowledgement action for instance)
  • Internationalization using ApplicationResources files for application messages mapped by Locale
  • The ability to work with any data or object access framework: JDBC, Standard Taglib, EJB etc

An ActionMapping defines the relationship between an action name (e.g., logon) and a specific servlet or JSP class that implements the action (e.g., com.ebt.Web.Logon.jsp).

Struts version 1.1, now in final release form, extends Struts version 1.0.2 in several areas:

  • Tiles framework is supported for "assembling" Web pages dynamically
  • DynaActionForm classes are configured to eliminate the need to create an ActionForm class for every JSP.
  • Nested contexts are supported so pages can be nested in other pages and referenced (elegant)
  • Sub-applications are definable so Struts applications can now scale well and easily be partitioned across development teams for larger projects

Struts version 1.1 supports the Tiles presentation framework
Tiles is another open-source framework that builds on the "include" feature provided by the JavaServer Page specification to provided a full-featured, robust framework for assembling presentation pages from component parts. Each part, or tile, can be reused as often as needed throughout your application. This reduces the amount of markup that needs to be maintained and makes it easier to change the look and feel of a Web site.

As of version 1.1, Apache has incorporated the Tiles framework into Struts. You can define your JSP pages to support modular, dynamic page building with panels from different JSPs. Tiles pages are configured in XML files. For more on Tiles, see http://jakarta.apache.org/struts/userGuide/dev_tiles.html.

Struts applications are configurable at runtime
Struts is a very adaptable framework, since all of the processing rules for a Web application are externalized to configurable XML files. You can even override the framework reasonably well using these external configuration files. A Struts application will have specific entries in the application configuration file (Web.xml). Here you handle the definition of the ActionServlet and URL mapping rules for actions, etc.

The real definition of how your Struts application works is stored in the struts-config.xml file. Here you define each action and how it's processed, add event handlers to listen for application events, etc.

What does Struts mean to your e-business plans?
If you are ready to deploy a project now, go ahead. Maybe for the next major update or version, you can look at migrating to Struts. I've migrated some small test applications, and it was work but not hard work.

If you are about to build an application, reconsider the architecture and bring in a Struts consultant to evaluate what you are doing and make some recommendations. In most cases, good architectures followed some Struts principles, so adapting a good design to Struts shouldn't be too difficult.

If you are planning an e-business application, find a Struts resource to help you design your application.

Understand that while Struts version 1.1 is about to released in final version, many of the tools and vendors that use Struts are just getting their products out the door over the next few months. IBM, fortunately, has been one of the most aggressive vendors about driving Struts technology into their WebSphere Studio tools.

Where to go for more on Struts
The Struts sub-project at the Apache Web site.

We are building a new tutorial series: 10-minute Tutor that will be available for download free at www.quickWebsupport.com (enrollment is free). One of the first tutorials will cover a very simple Struts application. A future WebSphere Strategies Tip also will include Struts application example code from that tutorial.

At ebt-now, we are in the process of migrating some of our tools and applications to Struts as IBM is. We also can help customers looking to use Struts in their own applications.

--------------------------
About the author: Jim Mason is president of ebt-now.com, and he writes, consults, teaches, designs and develops iSeries Web applications using Java, WebSphere, DB2, Lotus Domino and the WebSphere Development Tools for iSeries.

=======================
MORE INFORMATION
=======================

  • WebSphere Express key to iSeries future in e-business
    The new WebSphere Express for iSeries is affordable, easy-to-use and packed with features that previously couldn't run on low-end servers. And if you're interested in using the Apache Struts framework, struts.jar is included.
  • Custom JSP tags speed Web development
    Building Web applications doesn't have to be hard, according to Web development experts Jim Mason and David Slater. Learn how to build and use custom JSP tags in WebSphere Development tools to build scalable Web applications easily.


This was first published in March 2003

There are Comments. Add yours.

 
TIP: Want to include a code block in your comment? Use <pre> or <code> tags around the desired text. Ex: <code>insert code</code>

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
Sort by: OldestNewest

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to:

Disclaimer: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.