ONJava.com -- The Independent Source for Enterprise Java
oreilly.comSafari Books Online.Conferences.

advertisement

AddThis Social Bookmark Button
Programming Jakarta Struts

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.

Related Reading

Jakarta Struts Pocket Reference
By Chuck Cavaness, Brian Keeton

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 NameDescription
cookieDefine a scripting variable based on the value(s) of the specified request cookie.
defineDefine a scripting variable based on the value(s) of the specified bean property.
headerDefine a scripting variable based on the value(s) of the specified request header.
includeLoad the response from a dynamic application request and make it available as a bean.
messageRender an internationalized message string to the response.
pageExpose a specified item from the page context as a bean.
parameterDefine a scripting variable based on the value(s) of the specified request parameter.
resourceLoad a web application resource and make it available as a bean.
sizeDefine a bean containing the number of elements in a Collection or Map.
strutsExpose a named Struts internal configuration object as a bean.
writeRender the value of the specified bean property to the current JspWriter.

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 NameDescription
baseRender an HTML <base> Element
buttonRender a Button Input Field
cancelRender a Cancel Button
checkboxRender a Checkbox Input Field
errorsConditionally display a set of accumulated error messages
fileRender a File Select Input Field
formDefine an Input Form
frameRender an HTML frame element
hiddenRender a Hidden Field
htmlRender an HTML <html> Element
imageRender an input tag of type "image"
imgRender an HTML img tag
javascriptRender JavaScript validation based on the validation rules loaded by the ValidatorPlugIn
linkRender an HTML anchor or hyperlink
messagesConditionally display a set of accumulated messages
multiboxRender a Checkbox Input Field
optionRender a Select Option
optionsRender a Collection of Select Options
optionsCollectionRender a Collection of Select Options
passwordRender a Password Input Field
radioRender a Radio Button Input Field
resetRender a Reset Button Input Field
rewriteRender an URI
selectRender a Select Element
submitRender a Submit Button
textRender an Input Field of Type text
textareaRender a Textarea Field
xhtmlRender 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 NameDescription
emptyEvaluate the nested body content of this tag if the requested variable is either null or an empty string.
equalEvaluate the nested body content of this tag if the requested variable is equal to the specified value.
forwardForward control to the page specified by the specified ActionForward entry.
greaterEqualEvaluate the nested body content of this tag if the requested variable is greater than or equal to the specified value.
greaterThanEvaluate the nested body content of this tag if the requested variable is greater than the specified value.
iterateRepeat the nested body content of this tag over a specified collection.
lessEqualEvaluate the nested body content of this tag if the requested variable is greater than or equal to the specified value.
lessThanEvaluate the nested body content of this tag if the requested variable is less than the specified value.
matchEvaluate the nested body content of this tag if the specified value is an appropriate substring of the requested variable.
messagesNotPresentGenerate the nested body content of this tag if the specified message is not present in this request.
messagesPresent Generate the nested body content of this tag if the specified message is present in this request.
notEmptyEvaluate 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).
notEqualEvaluate the nested body content of this tag if the requested variable is not equal to the specified value.
notMatchEvaluate the nested body content of this tag if the specified value is not an appropriate substring of the requested variable.
notPresentGenerate the nested body content of this tag if the specified value is not present in this request.
presentGenerate the nested body content of this tag if the specified value is present in this request.
redirectRender an HTTP 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 NameDescription
addAdd an element to the surrounding list. Equivalent to 'put', but for list element.
definitionCreate a tile/component/template definition bean.
getGets the content from request scope that was put there by a put tag.
getAsStringRender the value of the specified tile/component/template attribute to the current JspWriter.
importAttributeImport Tile's attribute in specified context.
initComponentDefinitionsInitialize Tile/Component definitions factory.
insertInsert a tiles/component/template.
putPut an attribute into tile/component/template context.
putListDeclare a list that will be pass as attribute to tile.
useAttributeUse attribute value inside page.

Pages: 1, 2

Next Pagearrow