Introduction to Jakarta Struts Framework
Pages: 1, 2
The model is an object that takes the request from the user and stores the results for the duration of the process. Usually a JavaBean passes along the information in the process. Model objects are typically application specific since they are basically the business logic of the application. Struts provide the ActionForm and Action classes, which extends to model objects creation. It is in these model objects where form validation, or any type of preprocessing of request data that is necessary, usually takes place. It's possible to have a one-to-one correlation between model objects and request pages, but it's not necessarily required. It's also quite possible to reuse a model object for multiple page requests. If there is no action necessary for a model object, the controller can send the request directly to the view object, as specified in the
struts-config.xml file is really the glue of the entire framework. While
web.xml defines where a request should go upon arriving,
struts-config.xml determines exactly what will happen to it. It's the Vito Corleone of the Struts framework; nothing happens without its okay and knowledge. This is where the mappings of actions are defined. As with the growing number of XML configuration files, the advantage of using one is in keeping a system modular and easy to maintain. It prevents the hard coding of objects to be called within a component. Changes can be made on the fly by changing the configuration files without having to re-deploy applications.
The controller uses the
struts-config.xml file to determine which object to call for a given action request. The file is read on startup and the relationships are stored in memory for optimal performance. A bare minimal
struts-config.xml is shown below. This just defines a login action so that when a login request is received, it will be forwarded to an instance of the action object
com.oreilly.ui.authentication.actions.LoginAction. The return of that class will determine which page to forward to from there. This sample also illustrates the ability to forward to other model objects; otherwise, it returns back to a view object, in this case the
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.0//EN"
<!-- ========== Action Mapping Definitions=================== -->
<!-- Attributes of the <action> element -->
type - Fully qualified Java class name of the Action
implementation class used by this mapping.
name - The name of the form bean defined in the config
file that this action will use
path - The request URI path that is matched to select this
mapping unknown - Set to true if this action should be
configured as the default for this application to
handle all requests not handled by another action.
Only one action can be defined as a default within
a single application.
validate - Set to true if the validate() method of the
action associated with this mapping should be called.
<!-- ~~~~~~~~~~~~~~~~~~~~~ -->
<!-- O'Reilly Main Actions -->
<!-- ~~~~~~~~~~~~~~~~~~~~~ -->
<forward name="success" path="/DisplayMainPage.action"/>
<forward name="failure" path="/login.jsp"/>
The view object is often a JSP page. The Struts framework doesn't actually provide the JSP, but it does provide a number of tag libraries that easily allow JSP integration into the Struts framework. The interaction of Struts with JSP allows for storing data of an input form in a form bean; that is the ActionForm we mentioned earlier. The ActionForm is, then, used by the Action class for doing field validation. If errors are found during validation, there is a shared mechanism available in the framework for raising and displaying error messages.
The Struts framework includes a number of custom tag libraries, which are used in a variety of ways. While the use of these libraries is not required to use the framework, chances are that you will want to explore them. These libraries include:
n struts-htmltag library - used for creating dynamic HTML user interfaces and forms.
n struts-beantag library - provides substantial enhancements to the basic capability provided by
n struts-logictag library - can manage conditional generation of output text, looping over object collections for repetitive generation of output text, and application flow management.
n struts-templatetag library - contains tags that are useful in creating dynamic JSP templates for pages which share a common format.
Additionally, the custom tags in the Struts framework use the internationalization features built into the Java platform by using resource files. To provide messages for another language, simply add another resource file.
Strutting Your Stuff
The Struts framework, while relatively new, is definitely holding its own with developers. It's found in a number of applications, and can assist you with new development or existing JSP projects you might be working on.
For example, in a large database application such as the one I am working on for a client, the business-logic beans are implemented as EJBs. A JSP is displayed in the browser. Depending on the form that is being displayed, a specific action takes place, as defined in the
struts-config.xml file. The appropriate action class receives the request, does some validation on the form object, and calls the execute method of the appropriate EJB service bean. The results from the EJB are returned in XML that is then parsed. The content is stored in a form bean and then displayed by a JSP.
The entire control of the flow is done by the
struts-config.xml file, depending on return values from the various action classes. This is all centrally located and easy to adjust. Neither the action's servlet nor the JSP need to know (and in proper n-tier development, shouldn't have to care) where the result set that is being displayed comes from.
The only drawback that I've found with the framework from my current project is this: since it is a large-scale project, there are many screens and many request types. It seems that there are many Action classes, since we basically need only one per request type. I think that this could be avoided by doing some careful planning upfront, but for new product development it isn't always possible to identify the common requests. That information gets acquired over time as the application takes shape and new business requirements are defined. Of course, there weren't many Struts samples to use six months ago, either. That one issue aside, Struts has helped save time in coding an entire MVC model, and has definitely been stable enough to use and benefit from in production products.
In Part Two of this series, we will walk through building a simple Struts application and tie all of the pieces together in a coding sample.
Read more JSP and Servlets columns.
Return to ONJava.com.
2005-06-27 02:47:41 Jaganmohan.D [View]
2005-09-12 07:35:26 navirk [View]
2005-01-08 10:35:59 Richard_Pelletier [View]
2004-09-01 06:09:41 konjeti [View]
2003-11-24 07:37:28 anonymous2 [View]
2003-11-03 15:42:39 anonymous2 [View]
2003-07-14 02:53:37 anonymous2 [View]
2005-01-31 01:06:07 j2eebabu [View]
2006-05-20 05:22:01 jaya/java [View]
2005-01-30 23:46:12 j2eebabu [View]
2003-06-23 04:41:46 arin_sen [View]
2003-05-30 07:58:56 anonymous2 [View]
2005-05-06 02:28:10 avisahu [View]
2003-06-01 17:23:37 Sue Spielman | [View]
2003-06-01 17:21:57 Sue Spielman | [View]
2003-06-01 17:21:43 Sue Spielman | [View]
2003-05-13 10:35:25 anonymous2 [View]
2003-04-01 20:00:12 anonymous2 [View]
2003-02-18 01:45:08 anonymous2 [View]
2003-02-18 01:44:48 anonymous2 [View]
2003-05-20 18:00:42 anonymous2 [View]
2003-02-03 12:10:29 Sue Spielman | [View]
need a help to understand Struct better
2005-08-17 20:49:03 shreeji [View]
2003-02-03 12:02:56 anonymous2 [View]
2003-02-03 11:59:48 anonymous2 [View]
2002-12-13 06:59:18 anonymous2 [View]
2002-11-24 07:05:55 anonymous2 [View]
2002-11-20 02:35:13 anonymous2 [View]
Confusion in the readers mind
2003-05-29 13:51:19 anonymous2 [View]
2002-11-18 16:10:39 anonymous2 [View]
2002-10-20 23:00:11 dee_xlncy [View]
2002-04-21 19:01:00 pmattioc [View]
2001-11-23 10:43:13 billmil [View]
Null ActionForms and action names
2002-07-25 07:52:14 tlozina [View]
2001-10-24 03:28:27 chater [View]
2001-09-18 08:48:44 browns [View]
Model in MVC
2004-12-30 04:46:51 viku [View]