|
Getting the Most Out of the Struts Tag Librariesby Chuck Cavaness, author of Programming Jakarta Struts and the recently released Jakarta Struts Pocket Reference07/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 |
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 |
cookie | Define a scripting variable based on the value(s) of the specified request cookie. |
define | Define a scripting variable based on the value(s) of the specified bean property. |
header | Define a scripting variable based on the value(s) of the specified request header. |
include | Load the response from a dynamic application request and make it available as a bean. |
message | Render an internationalized message string to the response. |
page | Expose a specified item from the page context as a bean. |
parameter | Define a scripting variable based on the value(s) of the specified request parameter. |
resource | Load a web application resource and make it available as a bean. |
size | Define a bean containing the number of elements in a Collection or Map. |
struts | Expose a named Struts internal configuration object as a bean. |
write | Render 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 Name | Description |
base | Render an HTML <base> Element |
button | Render a Button Input Field |
cancel | Render a Cancel Button |
checkbox | Render a Checkbox Input Field |
errors | Conditionally display a set of accumulated error messages |
file | Render a File Select Input Field |
form | Define an Input Form |
frame | Render an HTML frame element |
hidden | Render a Hidden Field |
html | Render an HTML <html> Element |
image | Render an input tag of type "image" |
img | Render an HTML img tag |
javascript | Render JavaScript validation based on the validation rules loaded by the
ValidatorPlugIn |
link | Render an HTML anchor or hyperlink |
messages | Conditionally display a set of accumulated messages |
multibox | Render a Checkbox Input Field |
option | Render a Select Option |
options | Render a Collection of Select Options |
optionsCollection | Render a Collection of Select Options |
password | Render a Password Input Field |
radio | Render a Radio Button Input Field |
reset | Render a Reset Button Input Field |
rewrite | Render an URI |
select | Render a Select Element |
submit | Render a Submit Button |
text | Render an Input Field of Type text |
textarea | Render a Textarea Field |
xhtml | 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 |
empty | Evaluate the nested body content of this tag if the requested variable is either null or an empty string. |
equal | Evaluate the nested body content of this tag if the requested variable is equal to the specified value. |
forward | Forward control to the page specified by the specified ActionForward entry. |
greaterEqual | Evaluate the nested body content of this tag if the requested variable is greater than or equal to the specified value. |
greaterThan | Evaluate the nested body content of this tag if the requested variable is greater than the specified value. |
iterate | Repeat the nested body content of this tag over a specified collection. |
lessEqual | Evaluate the nested body content of this tag if the requested variable is greater than or equal to the specified value. |
lessThan | Evaluate the nested body content of this tag if the requested variable is less than the specified value. |
match | Evaluate the nested body content of this tag if the specified value is an appropriate substring of the requested variable. |
messagesNotPresent | Generate 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. |
notEmpty | 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). |
notEqual | Evaluate the nested body content of this tag if the requested variable is not equal to the specified value. |
notMatch | Evaluate the nested body content of this tag if the specified value is not an appropriate substring of the requested variable. |
notPresent | Generate the nested body content of this tag if the specified value is not present in this request. |
present | Generate the nested body content of this tag if the specified value is present in this request. |
redirect | Render 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 Name | Description |
add | Add an element to the surrounding list. Equivalent to 'put', but for list element. |
definition | Create a tile/component/template definition bean. |
get | Gets the content from request scope that was put there by a put tag. |
getAsString | Render the value of the specified tile/component/template attribute to the current JspWriter. |
importAttribute | Import Tile's attribute in specified context. |
initComponentDefinitions | Initialize Tile/Component definitions factory. |
insert | Insert a tiles/component/template. |
put | Put an attribute into tile/component/template context. |
putList | Declare a list that will be pass as attribute to tile. |
useAttribute | Use attribute value inside page. |
Pages: 1, 2 |
