Learning Jakarta Struts, Part 3

11/14/2001

This is the final article in a three-part series on the Struts framework.

In the first article, Introduction to Jakarta Struts Framework, I defined the Struts framework, discussed what it can accomplish, and provided an overview of the various components used throughout the framework. In the second article, Learning Jakarta Struts, I described building a simple sample application from scratch using Struts 1.0. This article will show you how to use the Struts tags to access the ApplicationResource file from a JSP.

The action classes are the link between the Struts framework and your business application logic. You tend to want to keep action classes as thin as possible, primarily because the true logic in the application should be in a separate logic tier. If you are doing n-tier application development, you want to keep the interfaces as clean as possible between the tiers. The fact that the main method in the action class is called "perform" indicates that the action classes should do something. Every action needs to be extended from org.apache.struts.action.Action. For a small application, it is possible to do just that, extend action; however, I've found that there are usually common features for your action classes in a given application. It is therefore a better design (in my opinion) to create a base class used by all actions in the application. I've set the StrutsSample application up this way, with a typical method that might be of use in an application action base class as well as some samples of general forward definitions.

This is the base class from which all action classes that use
Struts can be derived. It takes into consideration some
general architecture that would most likely be needed in a real
application. For the purpose of this article, those methods that
are not directly related to the Struts framework will be black-boxed
and commented so that you can use this as a skeleton and fill in those
methods as you see fit while you are doing development.
All action classes need to be derived from org.apache.struts.action.Action.

This is a blackbox method -- lookup the EJB instance necessary. Typically, action classes work with EJB session beans (or just javabeans) that contain the business logic for your application. In large scale projects, you want to keep a clean seperation of tiers. You would get the JNDI context, get an instance of the context, and then do a lookup on the given JNDI name for the
EJB you are interested in to retrieve the home interface.
This is provided as just a sample of what is needed.

This is the main action called from the Struts framework.
You can have this method call an abstract performAction method
that is then implemented by each action class, and do any specifics
in this method that might be common to all actions, like logging.
For this example, we are simply using the perform as the asbstract class.