Apache Struts

From Wikipedia, the free encyclopedia

(Redirected from Struts) Jump to: navigation, search

Apache Struts (formerly under the Apache Jakarta Project, Struts is now a top level project) is an open-source framework for developing Java EE web applications. It uses and extends the Java Servlet API to encourage developers to adopt an MVC architecture. It was originally created by Craig McClanahan and donated to the Apache Foundation in May, 2000. This framework enables design and implementation of large web applications to be handled by different groups of people. In other words, page designers, component developers and other developers can handle their own bit of the project, all in tandem and in a decoupled manner. It features I18N (internationalization), a powerful custom tag library, tiled displays and form validation. It also supports a variety of presentation layers, including JSP, XML/XSLT, JavaServer Faces (JSF), and Velocity, as well as a variety of model layers, including JavaBeans and EJB.

• • • •

1 Competing MVC frameworks 2 See also 3 References 4 External links

Competing MVC frameworks
Although Struts is a very well documented, mature and popular framework for building front ends to Java applications, it is facing new challenges from newer "light weight" MVC frameworks such as Spring and Tapestry. The new XForms standards and


frameworks may also be another option to building complex web Form validations with Struts in the future. In December 2005, Struts joined forces with another popular J2EE framework, WebWork. WebWork is being adopted as the 2.0 release of Apache Struts. WebWork spun off from Struts several years ago, and offers many enhancements and refinements, while retaining the same general architecture of the original Struts framework. Sun recently brought out a new addition to the Java platform, called JavaServer Faces (JSF). Aside from the original framework, the Apache Struts project also offers a JSFbased framework called Shale. Other MVC frameworks that are not J2EE based include Ruby on Rails, WebObjects, Django, Catalyst, TurboGears and CakePHP. There's another J2EE framework which is based on Struts called paradiseRAD.

See also
• • •

Model-view-controller Jakarta Java EE

• • • • •

• • •

James Holmes: Struts: The Complete Reference, McGraw-Hill Osborne Media, ISBN 0-07-223131-9 Bill Siggelkow: Jakarta Struts Cookbook, O'Reilly, ISBN 0-596-00771-X James Goodwill, Richard Hightower: Professional Jakarta Struts, Wrox Press, ISBN 0-7645-4437-3 John Carnell and Rob Harrop: Pro Jakarta Struts, Second Edition, Apress, ISBN 1-59059-228-X John Carnell, Jeff Linwood and Maciej Zawadzki: Professional Struts Applications: Building Web Sites with Struts, ObjectRelationalBridge, Lucene, and Velocity, Apress, ISBN 1-59059-255-7 Ted Husted, etc: Struts in Action, Manning Publications Company, ISBN 1930110-50-2 Struts View Assembly and Validation, (PDF format). Stephan Wiesner: Learning Jakarta Struts 1.2, Packt Publishing, 2005 ISBN 1904811-54-X


External links
• • • • • • • •

Apache Struts' home page AppFuse Struts Console : GUI tool for working with Struts Struts and UTF-8 : Handling UTF-8 in Struts with Servlet filters Struts-Layout : Rich TagLib for Struts Free chapter from Learning Jakarta Struts 1.2: A concise and practical tutorial book (PDF format) A step-by-step Tutorial for Struts How to configure Struts for Tiles and modules

What's New In Struts 1.1
For JSP and J2EE developers looking for a way to manage the complexity of large web sites, the platform of choice has been Struts. This framework, based on the Model-View-Controller design pattern and part of the Apache Jakarta project, is in use throughout the industry. Struts combines tools for form validation, workflow control, and separation of business logic from display. After nearly a year of anticipation, Jakarta Struts 1.1 is finally out the door. In reality, however, this release is more like Struts 1.99 or even Struts 2.0 when you get done looking at all the new features and functionality that have been added. And now that 1.1 is complete, the developers and user community are looking toward the 1.x and 2.0 releases, with a plethora of new features and integrations waiting in the wings. With that in mind, let's look at what's new in 1.1, and what's upcoming in the next few months and year.

What's New in Struts 1.1?
To begin with, the release is much more bulletproof than 1.0.2 was, mainly as a result of the thousands of developers who have been using the 1.1 betas and finding bugs. In addition, the same developers were finding the shortcomings of 1.0 as they deployed Struts as their application framework, and have been adding some sorely needed features. The following sections briefly describe a few of the key advances found in the Struts 1.1 release.

Navigation Improvements
In 1.1, you can have an action that forwards directly to a JSP page without needing to define an Action class. The html:link tag can specify an action rather than a URI, allowing you to define links that pass through intermediate Actions.

Dynaforms allow you to define ActionForms directly from the struts-config.xml file, without needing to create an explicit new class. As a result, it's easy to create a new form on the fly, without having to spend a lot of time on bean properties.

Validator Framework


Hand in hand with Dynaforms, the validator framework allows you to define validation logic for forms using XML descriptors rather than having to write a validate() method on an ActionForm. In addition to the predefined validation (which includes length, type, and credit card checks), you can also define your own validations. You can also define field validations that depend on the values of other fields.

Tiles Taglib
Tiles provides a very powerful templating framework that enables you to define common document struts (headers, footers, sidebars) using either JSP documents or XML files. Using Tiles, you can essentially include document content from another file, passing parameters into the document.

Integration with JSTL
As the Java Standard Tag Library becomes more widely adopted, Struts has kept in step by offering tight integration. For example, the struts-el taglib offers versions of most of the Struts tags that can have embedded Expression Language (EL) values in them. For example, the following can generate a computed URL for an HTML link.:

<html-el:link href="${aBean.nextPage}"> Next Page </html-el:link>
In addition, some of the base Struts tag libraries now have a better understanding of JSTL. For example, the "indexed" property of the Struts HTML form tags can now be wrapped inside a JSTL <c:forEach> loop, as well as in a Struts <logic:iterate> loop.

Integration with JSF
Looking ahead to the Java Server Faces framework, which will be released at the end of the year, Struts included a contributed JSF integration tag library, which will enable developers to begin exploring JSF and working out migration and integration strategies for this new Java Community Process Technology.

And All the Rest...
There have also been countless tweaks and minor improvements throughout the Struts framework—all designed to improve the usability and reliability of the framework. The best place to look to see the full picture is the 1.1 release notes. 2. Where Does Struts Go from Here?

Where Does Struts Go from Here?
The first thing people wonder about with Struts is how long it will take for a 1.2 release to appear. If you took the 1.0 to 1.1 timeframe as a guideline, you might be looking out into 2005 for a 1.2 release. However, the Struts development team took a lesson from the 1.1 release (too many changes between releases means that it's hard to lock it down). So the team plans to do many more frequent and focused releases. The expectation is that 1.2 will release sometime in the next few months, with a few minor features that couldn't make the feature freeze cutoff for 1.1, as well as a migration of some of the resource management code to the Jakarta Commons libraries rather than the "proprietary" Struts versions. After that, there will be a release whenever there have been more than a few bug fixes or new features added. So expect Struts to look more like Linux from now on, frequent small releases.


Another thing to watch for is the eventual deprecation of the Struts tag libraries. The Struts taglibs largely date from before JSTL was available, and most of their functionalities can be better served by the equivalent JSTL tags at this point. As JSTL-compatible (Servlet 2.3) containers become the standard (such as Tomcat 4.0), there will be less and less reason to use (and, by extension, to maintain and improve) the Struts tag libraries. Eventually, Struts may find itself out of a job. As the JSF standard evolved over the last year, more and more of the Struts functionality crept into JSF. This is not surprising, considering that Craig McClanahan, the "father" of Struts, is also the JSF Spec Lead. Because JSF is a JCP technology, it will be widely adopted and may eventually push out Struts because there is a lot of overlap between the two. Of course, the Struts development community is already busily discussing how to keep Struts relevant in a post-JSF world, and you can also expect tools to provide cross-compatibility and migration from Struts to JSF after JSF is available for production applications (again, in 2004.) Two potential upgrades to Struts are being discussed. The first is the capability to use JSP pages as Actions, which would let developers dynamically redefine Actions without having to recompile or restart the server (as well as letting page authors create business logic using technologies like JSTL rather than pure Java). The other includes improvements to automated type conversion so that after validation, values are made available to the business logic in their native forms (Date, Integer, and so on) rather than as String. There are also ongoing active discussions of how Struts can become more internationalization-friendly and work as a framework for web services.

With an increasing user community, more frequent releases, and the challenge of making Struts relevant in light of JSF, you can expect a lot of new functionality in the post-1.1 Struts world.

Apache Struts
Apache Struts is a free open-source framework for creating Java web applications. Web applications differ from conventional websites in that web applications can create a dynamic response. Many websites deliver only static pages. A web application can interact with databases and business logic engines to customize a response. Web applications based on JavaServer Pages sometimes commingle database code, page design code, and control flow code. In practice, we find that unless these concerns are separated, larger applications become difficult to maintain. One way to separate concerns in a software application is to use a Model-View-Controller (MVC) architecture. The Model represents the business or database code, the View represents the page design code, and the Controller represents the navigational code. The Struts framework is designed to help developers create web applications that utilize a MVC architecture. The framework provides three key components:

An extensible "front controller" that dispatches requests to an "action"


• •

handler provided by the application developer. A location handler that transfers control to another resource that completes the response. A tag library that helps developers create interactive form-based applications.

The framework's architecture and tags are buzzword compliant. Struts works well with conventional REST applications and with nouveau technologies like SOAP and AJAX.

The Apache Struts Project
The Apache Struts Project is the open source community that creates and maintains the Apache Struts framework. The project consists of a diverse group of volunteers who share common values regarding collaborative, community-based open source development. The Apache Struts Project is proud to share these values with our parent organization: The Apache Software Foundation. The project is called "Struts" because the framework is meant to furnish the "invisible underpinnings" that support professional application development. Struts provides the glue that joins the various elements of the standard Java platform into a coherent whole. Our goal is to leverage existing standards by producing the missing pieces we need to create enterprise-grade applications that are easy to maintain over time. The Apache Sruts Project offers two major versions of the Struts framework. Struts 1 is recognized as the most popular web application framework for Java. The 1.x framework is mature, well-documented, and widely supported. Struts 1 is the best choice for teams who value proven solutions. Struts 2 was originally known as WebWork 2. After working independently for several years, the WebWork and Struts communities joined forces to create Struts 2. The new framework is the best choice for teams who value elegant solutions to difficult problems. Struts 2 is still under construction. An initial release is expected in the third quarter of 2006. For more about the future of Struts, see our Roadmap FAQ, especially "What should I use with my next project?".


Kickstart FAQ General

I'm new to Apache Struts. How do I get started? Why is the project called "Struts"? How is Apache Struts licensed? Can Apache Struts be used in a commercial product? Do I have to credit Apache Struts on my own web site? Are there ISPs that will host my Java web application? Is there a particularly good IDE to use? Product Support Where can I get help with Apache Struts? Is commercial support available? If a product doesn't do what I want, can I request that a feature be added? Mailing Lists Is there a digest for the User list? Is there an Apache Struts newsgroup? Why didn't my posting show up on the user list? Are there separate user lists for Struts 1 and Struts 2? How do I unsubscribe from the mailing list? General


I'm new to Apache Struts. How do I get started? The best available release of Apache Struts is Struts 1.2.9. The Struts 1.2.9 release has its own area of the website. If you are new to Java or web development, as well as Apache Struts, be sure to review the Preface to the User Guide.

The main Struts web site covers the latest, unreleased version of each major release series (Struts 1.3 and Struts 2.0). The documentation for past releases are also available online. See the navigational menu for links to the documentation for each release.

To learn more about using Apache Struts, you can also visit Struts Central, which catalogs all the known resources about Apache Struts for both Struts 1 and Struts 2. To keep up on the latest news about "everything Struts", point your RSS reader at the Planet Struts news site. If you still have questions, you can search the User Mailing List archives, or post your own question to the list. (Plain old Google often works too!) Why is the project called "Struts"? It's a reference to "struts" in the architectural sense, a reminder of the nearly invisible pieces that hold up buildings, houses, and bridges. How is Apache Struts licensed? All Apache Struts products are copyrighted software available under the Apache License, a "free-to-use, business-friendly license".

Can Apache Struts be used in a commercial product? Yes. The only requirements you must meet are those listed in the Apache License.


Do I have to credit Apache Struts on my own web site? You need to credit Apache Struts if you redistribute your own framework based on our products for other people to use. (See the Apache License for details.) But you do not need to credit Apache Struts just because your web application utilizes one of our products. It's the same situation as using the Apache HTTPD server or Tomcat. Not required if its just running your web site. Required if you've used the source code to create your own server that you are redistributing to other people. Are there ISPs that will host my Java web application? For a listing of some Java and Struts ISPs, visit the Struts Community Resources area on SourceForge.

Is there a particularly good IDE to use? The frameworks should work well with any development environment that you would like to use, as well as with any programmers editor. The members of the Apache Struts development group each use their own tools such as Emacs, IDEA, Eclipse, and NetBeans.

For more, see the IDE discussion page in the wiki.

Product Support Where can I get help with Apache Struts? Each release of Struts comes with a User Guide or set of Tutorials to introduce people to the framework and its underlying technologies. Various components also have their own in-depth Developers Guide, to cover more advanced topics. Comprehensive Javadocs are provided for each release, along with the full source code.


The Struts user mailing list is also very active, and welcomes posts from new users. Before posting a new question, be sure to consult the MAILING LIST ARCHIVE and the very excellent How To Ask Questions The Smart Way by Eric Raymond. Please do be sure to turn off HTML in your email client before posting.

Is commercial support available? The Apache Software Foundation does not provide commercial support for any of our software products, including Apache Struts. However, third parties may offer different degrees of support.

If a product doesn't do what I want, can I request that a feature be added? First, it's important to remember that Apache Struts is an all-volunteer project. We don't charge anyone anything to use Apache Struts products. Committers and other developers work on Apache Struts products because they need to use it with their own applications. If others can use it too, that's "icing on the cake". If you submit a patch for a feature that a Committer finds useful, then that Committer may choose to volunteer his or her time to apply the patch. If you just submit an idea without a patch, it is much less likely to be added (since first someone else has to volunteer their time to write the patch).

We are grateful for any patches, and we welcome new ideas, but the best way to see that something gets added to the framework is to do as much of the work as you can, rather than rely on the "kindness of strangers". Worst case, you can apply the patch to your copy of the framework and still use the feature in your own application. (Which is what open source is ~really~ all about.)

Mailing Lists Is there a digest for the User list?


Yes. send a blank message to < user-digestsubscribe@struts.apache.org >.

If you are subscribed to the digest, you can also post to the list. Just be sure to send your post to the user list rather than trying to reply to the digest. Is there an Apache Struts newsgroup? Not a usenet group, but the Apache Struts User list can be accessed with your favorite newsgroup reader from the GMane News Site. Subscribe to groups gmane.comp.jakarta.struts.devel for the developer list, or gmane.comp.jakarta.struts.user for the user list.

Why didn't my posting show up on the user list? You must be subscribed to the user list or user digest before posting (or use the GMane Newsgroups instead).

Are there separate user lists for Struts 1 and Struts 2? No. Each major version has it's own JIRA project, but we share the mailing lists.

To get the best response to an inquiry, please specify which version of Struts is being used, including the milestone ("Struts 1.2.4", for example). You can also include the label [struts1] or [struts2] in the subject line of your post.

How do I unsubscribe from the mailing list? From the email account used to subscribe to the list, send a blank message to < user-unsubscribe@struts.apache.org >.


If you are receiving the digest, you must send a blank email to < userunsubscribe@struts.apache.org > instead.

Getting the Most Out of the Struts Tag Libraries
by Chuck Cavaness, author of Programming Jakarta Struts and the recently released Jakarta Struts Pocket Reference 07/30/2003 The popularity of JSP Custom Tags has been rapidly growing since they were first introduced in the JSP 1.1 specification. The Struts framework, which was introduced in 2000, includes a set of Tag libraries that are instrumental in harvesting the fruits of the Struts framework. This article looks at some of the ways to get more out of those tags and helps make sense out of a few of the more complicated tasks.

Reaping the Benefits of JSP Custom Tags
The problems with straight JSP have been well documented. With JSP for instance, it's very tempting to insert Java code into the JSP. This makes maintenance of the page somewhat more challenging, even if they do have a cool sounding name like "scriptlets." And reuse for JSPs is usually nothing more than copy and paste. We all should know by now that JSP Custom Tags were created to solve these problems and a few others. They are easier to maintain because the logic is programmed in once place and the construction of web applications is made simpler because a non-Java designer can drop them into a JSP and instantly see the results. Most popular JSP editors such as DreamWeaver and WSAD support Custom Tag syntax and the ability to step through them in a debugger. The Tags themselves are built using Java, of course. Inserting a Tag only requires embedding an XML-like fragment into a JSP. When a JSP that contains one or more tags is invoked, the servlet produced from JSP compilation calls out to the appropriate Tag handler instance to perform its logic. All invocations of the same Tag invoke the same code. The better application's containers will create a pool of tag instances and not create a new instance of the Java class for every request. This helps to improve performance in a manner similar to how an EJB container can reuse Stateless Session bean instances.

Overview of the Struts Tag Libraries
The Struts framework provides a set of six built-in Tag libraries that allow you to build the view part of the MVC without embedding Java code directly within your application JSPs. The six Struts libraries are:

• • • • • •

Bean Tags HTML Tags Logic Tags Nested Tags Template Tags Tiles Tags


The Bean Tags
The Tags within the Bean Library are used for creating and accessing JavaBeans and a few other general purpose uses. Although these tags work with any standard JavaBean, they are often used with Objects that extend the Struts ActionForm class. Table 1 lists the tags within the Bean Library. Table 1. Tags within the Struts Bean Tag Library

Tag Name

Description Define a scripting variable based on the value(s) of the specified request cookie. Define a scripting variable based on the value(s) of the specified bean property. Define a scripting variable based on the value(s) of the specified request header. Load the response from a dynamic application request and make it available as a bean. Render an internationalized message string to the response. Expose a specified item from the page context as a bean. Define a scripting variable based on the value(s) of the specified request parameter. Load a web application resource and make it available as a bean. Define a bean containing the number of elements in a Collection or Map. Expose a named Struts internal configuration object as a bean. Render the value of the specified bean property to the current JspWriter.

cookie define header include message page parameter resource size struts write

Two of the most often used Tags from Table 1 are the message and write Tags.

The HTML Tags
The Tags within the Struts HTML Tag Library are used to create input forms for your application. There are also a few other useful Tags used in the creation and rendering of HTML-based user interfaces. The Tags included within the HTML Library are shown in Table 2. Table 2. Tags within the Struts HTML Tag Library

Tag Name

Description Render an HTML <base> Element Render a Button Input Field Render a Cancel Button Render a Checkbox Input Field Conditionally display a set of accumulated error messages

base button cancel checkbox errors


file form frame hidden html image img javascript link messages multibox option options optionsCollection password radio reset rewrite select submit text textarea xhtml

Render a File Select Input Field Define an Input Form Render an HTML frame element Render a Hidden Field Render an HTML <html> Element Render an input tag of type "image" Render an HTML img tag Render JavaScript validation based on the validation rules loaded by the


Render an HTML anchor or hyperlink Conditionally display a set of accumulated messages Render a Checkbox Input Field Render a Select Option Render a Collection of Select Options Render a Collection of Select Options Render a Password Input Field Render a Radio Button Input Field Render a Reset Button Input Field Render an URI Render a Select Element Render a Submit Button Render an Input Field of Type text Render a Textarea Field Render HTML tags as XHTML

Most all of the Tags within the HTML Tag library must be nested within the Struts Form Tag.

The Logic Tags
The Logic Tag Library contains tags that are helpful with iterating through collections, conditional generation of output, and application flow. Table 3 lists the Tags within the Logic Library. Table 3. Tags within the Struts Logic Tag Library


Tag Name

Description Evaluate the nested body content of this tag if the requested variable is either null or an empty string. Evaluate the nested body content of this tag if the requested variable is equal to the specified value. Forward control to the page specified by the specified ActionForward entry. Evaluate the nested body content of this tag if the requested variable is greater than or equal to the specified value. Evaluate the nested body content of this tag if the requested variable is greater than the specified value. Repeat the nested body content of this tag over a specified collection. Evaluate the nested body content of this tag if the requested variable is greater than or equal to the specified value. Evaluate the nested body content of this tag if the requested variable is less than the specified value. Evaluate the nested body content of this tag if the specified value is an appropriate substring of the requested variable. Generate the nested body content of this tag if the specified message is not present in this request. Generate the nested body content of this tag if the specified message is present in this request. Evaluate the nested body content of this tag if the requested variable is neither null, nor an empty string, nor an empty java.util.Collection (tested by the .isEmpty() method on the java.util.Collection interface). Evaluate the nested body content of this tag if the requested variable is not equal to the specified value. Evaluate the nested body content of this tag if the specified value is not an appropriate substring of the requested variable. Generate the nested body content of this tag if the specified value is not present in this request. Generate the nested body content of this tag if the specified value is present in this request. Render an HTTP Redirect.

empty equal forward greaterEqual greaterThan iterate lessEqual lessThan match messagesNotPresent messagesPresent


notEqual notMatch notPresent present redirect

The Nested Tags
The Nested Tags were added to Struts during development of the 1.1 release. They extend the existing Tags functionality by allowing the Tags to relate to each other is a nested fashion. This is most useful when dealing with Object graphs.


The Nested Tags don't add any additional functionality over the Struts standard Tags other than to support the nested approach. For each Tag in the Bean, HTML, and Logic libraries, there is an equivalent nested Tag.

The Template Tags
The Template Tag Library was created to reduce the redundancy found in most web applications. In most web sites, there are sections within multiple pages that are exactly the same. The header, menus, or footers are three obvious examples. Instead of duplicating the content in each page and having to modify all pages when something like the look and feel changes, Templates allow you to have the common content in one place and insert it where necessary. However, since the Tiles framework was introduced, the Template Tags have been deprecated and developers are encouraged to use Tiles.

Tiles Library Tags
As mentioned earlier, the Tiles framework is now integrated into the core Struts framework. Tiles is similar to the Template Tags except that it adds much more functionality and flexibility. For instance, Tiles supports inheritance between Tiles and allows you to define layouts and reuse those layouts within your site. They also support different Tiles and layouts based on I18N and channel. The Tags with the Tiles Library are shown in Table 4. Table 4. Tags within the Struts Tiles Tag Library

Tag Name

Description Add an element to the surrounding list. Equivalent to 'put', but for list element. Create a tile/component/template definition bean. Gets the content from request scope that was put there by a put tag. Render the value of the specified tile/component/template attribute to the current JspWriter. Import Tile's attribute in specified context. Initialize Tile/Component definitions factory. Insert a tiles/component/template. Put an attribute into tile/component/template context. Declare a list that will be pass as attribute to tile. Use attribute value inside page.

add definition get getAsString importAttribute initComponentDefinitions insert put putList useAttribute

Collections, Maps, and the Iterate Tag
There are often times within a Struts application that you must display a collection of objects to the user. The scenario usually goes something like this: for each item in the collection, display one or more attributes from the object. For example, in a page that renders a classic shopping cart, each Object represents one item that has been added to the cart. Figure 1 shows an example of a shopping cart page.


Figure 1. A Shopping Cart page showing multiple items Using JSP without Struts, this functionality would most likely be accomplished with some rather unattractive looking Java Scriptlets using loops. With the Struts Iterate Tag, it becomes somewhat innocuous and much easier to manage over the lifetime of the application. The following example code snippet shows what this might look like using the Iterate Tag from the Logic library. Example 1. Logic Iterate Code for Shopping Cart in Figure 1

<logic:iterate id="cartItem" name="UserContainer" property="cart.items">      <!­This Table Row will be repeated for each item in the user's cart ­­>


  <tr bgcolor="#ffffff">     <!­­Delete checkbox for item ­­>     <td valign="top" align="middle">       <input          type="checkbox"          value="<bean:write name="cartItem" property="id"/>"          name="deleteCartItem">     </td>            <!­­ name and description for item ­­>     <td valign=top align=left>       <html:link          page="/action/viewitemdetail"          paramName="cartItem"          paramId="id"          paramProperty="id">                <!­­ Write out the name of the Item ­­>         <bean:write name="cartItem" property="name"/>       </html:link>          <br>In Stock ­ Usually ships in 2­3 business days<br>              Available for pickup at most stores     </td>                       <!­­ Write out a Quantity Input Field ­­>     <td valign="top" noWrap align="middle">       <input          type="text"


        maxlength="3"          size="3"         name="qty_<bean:write name="cartItem" property="id"/>"         value="<bean:write name="cartItem" property="quantity"/>">     </td>                       <!­­ Unit price for item ­­>     <td valign="top" noWrap align="middle">        $<bean:write name="cartItem" format="#,##0.00" property="unitPrice"/>     </td>                        <!­­ Extended price for item ­­>     <td valign="top" align="right">       <table cellspacing="2" cellpadding="3" width="100%" border="0">        <tbody>          <tr>            <td align="right" bgcolor="#ffcc00">              $<bean:write                  name="cartItem"                  format="#,##0.00"                 property="extendedPrice"/>            </td>          </tr>        </tbody>       </table>     </td>         </tr> </logic:iterate>


Looking at this snippet from a JSP, you might be inclined to comment on how formidable it appears. You should however quickly recognize that with the exception of a few Struts tags, most of the text is HTML; which is how it should be. Using collections is rather straightforward. However, if your data is for some reason stored within an instance of a java.util.Map, getting at the properties in the map is a little trickier, but nonetheless solvable with the Iterate Tag. Using the Iterate Tag, we can iterate through the Map and access each element. Normally each Object that you iterate over is an element of the underlying collection, a ShoppingCartBean, for example. However, when iterating over a Map instance, the Object is of type java.util.Map.Entry. As the Struts documentation describes, Map.Entry has two properties:

• •

Key: The key under which the item is stored in the Map. Value: The value associated with the key.

Let's say you are using a Struts ActionForm (standard or a dynamic) and it contains a property named "usermap" that is declared as a Map and instantiated as a java.util.HashMap. In this Map, the key is a unique user ID and the value is a String representing the users encrypted password. Figure 2 illustrates what this would look like.

Figure 2. Data Structure for Example 2 The following code fragment shows an example of iterating through this Map and writing out the two values. Example 2. Iterate Tag used with a Map

<logic:iterate id="user" name="userForm" property="usermap">   <tr>     <td>User Id: <bean:write name=" user" property="key"/></td>     <td>Password: <bean:write name=" user" property="value"/></td>      </tr>


As you can see from this JSP fragment, using the Iterate Tag with Maps is not much different than with Collections. The primary distinction is that of using the key and value attributes to access the data stored within the Map. Once you have references to this data, you can use it within the Iterate block just as you would with anything else.

Formatting Errors with the Errors Tag
Let's face it, as hard as you try to make GUIs user-friendly and bulletproof, end users make mistakes. One of the most common mistakes is incorrect data entry. Your applications need to be very careful when accepting input from users in order to ensure the integrity of the datastore is maintained; Struts applications are no exception. Data entered from the browser must be validated and if errors are found, information needs to be displayed to the user describing what the problems are and what steps to take next. The Struts framework includes the ability to catch errors and populate an Errors collection that can be accessed from within the JSPs. This is made even easier when using the Validator framework. Whether you're doing programmatic or declarative validation, the Errors collection is inserted into the request and available to a JSP. You can use either the Messages or the Errors Tag (depending on which type of validation problems occurred) to access the messages to display to the users. Both of the Messages and Errors Tags support an "id" attribute that is used to store the current error while looping through the collection of problems. In a very simple approach you can use the following within your JSP:

   <html:messages id="error">       <li><bean:write name="error"/></li>    </html:messages>
This fragment would print out each message from the problem collection inside an HTML list. There's a header and footer entry that you can pull from the resource bundle, which allows you to provide some text to the beginning and end of the errors respectively. The following fragment shows the usage of the errors.header element.

  <bean:message key="errors.header"/>    <ul>    <html:messages id="error">       <li><bean:write name="error"/></li>    </html:messages>    </ul><hr>
The resource bundle would need to contain the errors.header entry:

errors.header=<h3><font color="red">Validation Error</font></h3>You must  correct the following error(s) before proceeding.


If the message collection is empty however, you would still see the header text and therefore it's best to check for the presence of messages before printing anything out:

<logic:messagesPresent>    <bean:message key="errors.header"/>    <ul>    <html:messages id="error">       <li><bean:write name="error"/></li>    </html:messages>    </ul><hr> </logic:messagesPresent>
There are equivalent Tags for the errors collection.

Dealing with Dates and Currencies
The Java language provides within its core libraries, the ability to deal with Dates and Currencies. One of the most sought after requirements is to format a Date or a Currency value based on a Country or Language. Java provides the java.util.Locale class, which encapsulates the country and language and makes it easier for applications to understand and deal with geographical differences. The Struts framework makes use of this by storing the Locale for a user in the HttpSession, which is created the first time a request is received from a user. This functionality can be turned off in Struts, but it's on by default. Once the Locale is stored in the user's session, the framework can make decisions about which localization is performed. For example, the Struts framework will chose a resource bundle that best matches the user's Locale. This makes it somewhat easier to brand a site because localized text can be rendered automatically be the framework. Many of the Tags also support this I18N functionality. For example, the Message Tag within the Bean Library uses the Locale to automatically pull text from the proper bundle. However, there are some additional ways you can format text output. The Write Tag has a format attribute that you can use to format currency in a particular format. Suppose that I wanted to display currency in the following manner:

Which is specific to mostly U.S. markets, then I could use the Write Tag this way:

<bean:write    name="cartItem"    format="#,##0.00"    property="unitPrice"/>
Although this works, there's actually a more portable way of doing this to support multiple formats. There's an attribute called formatKey on the Write Tag that will pull the format from the resource bundle. The Tag entry would look like the following:


<bean:write    name="cartItem"    format="currencyFormat"    property="unitPrice"/>
And the resource bundle would need to have the following entry:

Doing it this way allows each resource bundle to specify a currency format specific to its Locale.

Displaying Checkboxes with the Options Tag
One of the seemingly easiest things to do with HTML may be one of the hardest to do when building dynamic pages. That is rendering of checkboxes for an application; there are a few reasons why this is true First, checkboxes indicate checked/unchecked on/off, or true/false, depending on how you look at it; it's a binary condition. In the Java world, you would think this needs to be represented as a primitive boolean or a class java.lang.Boolean. However, since all values are sent from the browser to the server as Strings, this complicates things a bit. That means that something needs to convert the String "on" or "true" to its Boolean equivalent. The second non-obvious thing is how browsers treat checkboxes when updating the server. If a checkbox is checked, then a value is sent to the server like name="on", where "name" is the name given to the checkbox in the HTML form. Well, guess what is sent to the server when the checkbox isn't checked? You would think name="off" or name="false", right? Actually nothing is sent. Therefore the application can have a tough time understanding what's going on with the checkboxes on the pages. The Struts Checkbox Tag renders an HTML checkbox and will automatically check or uncheck it depending on the state of the Boolean value of the ActionForm associated with the page. You have to remember that if the checkbox is unchecked by the user, no notification will be sent and you must take this into account. (Hint: One of the easiest ways to think about this is if you don't receive a value, then it must be unchecked!) When you have a large number of related checkboxes, you should consider storing the values within an array and use the Multibox Tag.

Switching to the Java Standard Tag Library (JSTL)
The use of the Struts Tag libraries is a great movement forward toward smarter and faster development of web applications. However, Struts isn't cornering the market on the use of JSP Tags. Other frameworks use Tags and you don't have to use Struts to get the benefits of JSP Custom Tags. In order to standardize some of the more useful Tags, Sun and other participants created an initiative and formalized the most commonly used Tags. This initiative was turned into JSR-52, which has now released 1.0. Java Standard Tag Library (or JSTL as it's known), provides a specification for four separate areas of concern:

• • • •

Core XML Processing Internationalization Database


The specification also describes an expression language (EL) that is designed to simplify page development. As you might imagine, there is some overlap with some of the Tags provided by the Struts library. For example, looping, error handling, and conditional logic is provided by JSTL as well as Struts. The question is "which set of tags should I use?" The answer is to use the JSTL Tags where possible. For existing code, it's probably not worth the risk to change. However, for new JSPs you should be looking at the JSTL for Tag functionality. Struts 1.1 even includes a version of the JSTL Tags based on the new Expression Language. Something definitely to keep you eye on in future versions of Struts. Chuck Cavaness is a graduate from Georgia Tech with degrees in computer engineering and computer science. He has built Java-based enterprise systems in the healthcare, banking, and B2B sectors. He is also the author of two O'Reilly books, Programming Jakarta Struts and Jakarta Struts Pocket Reference.


Sign up to vote on this title
UsefulNot useful