Chapter 2 discussed the use of action forms for validating user input. And, you can reap more benefits of Struts if you use action forms in conjunction with the tags in the HTML Tag Library. The following are what you can get from the library.
Note
The HTML Tag Library is one of the three core tag libraries in Struts. The other two are the Bean Tag Library and the Logic Tag Library (discussed in Chapters 8 and 9, respectively).
This chapter deals with each of the HTML Tag Library’s tags. Mostly for your smoother learning experience, the tags are not presented in alphabetical order, but easier tags are given first. Also, some tags are similar and teaching them together saves your time. As such, similar tags are discussed in the same section. Additionally, the ease of mastering a tag sometimes depends on familiarity with another tag. In this case, the tag that should be taught first will appear before the other tag, regardless the names of the tags. Needless to say, this chapter is best read from the first paragraph to the last.
The HTML Tag Library tags are listed in Table 3.1.
| Tag | Description |
|---|---|
| base | Renders an HTML base element. |
| button | Represents an HTML button. |
| cancel | Represents a submit button for canceling a Struts action. |
| checkbox | Represents a check box. |
| errors | Displays input validation error messages. |
| file | Displays an HTML file element for selecting a file to upload. |
| form | Represents an HTML form. |
| frame | Represents a frame. |
| hidden | Represents a hidden element. |
| html | Renders an html element. |
| image | Represents an image that acts similarly to a submit button. |
| img | Represents a clickable image. |
| javascript | Renders JavaScript functions that support client-side validation in the Validator plugin. |
| link | Represents a hyperlink. |
| messages | Displays messages prepared from inside an action class. |
| multibox | Represents a set of check boxes from which multiple options can be selected. |
| option | Represents an option in a select box |
| options | Represents multiple options in a select box |
| optionsCollection | Represents multiple options in a select box. |
| password | Represents a password element. |
| radio | Represents a radio box. |
| reset | Represents a reset button. |
| rewrite | Renders a URI without creating a hyperlink. |
| select | Represents a select box. |
| submit | Represents a submit button. |
| text | Represents a text field. |
| textarea | Represents a text area. |
| xhtml | Indicates that the HTML tags in the same page must be rendered as XHTML elements, rather than HTML elements. |
Table 3.1: The tags in the HTML library
Note
The javascript tag is discussed in Chapter 5, messages in Chapter 11, and file in Chapter 17.
Note
To use the HTML Tag Library and reference its tags indirectly from JSPs, you must add this taglib tag to your web.xml file:
<taglib>
<taglib-uri>/tags/struts-html</taglib-uri>
<taglib-location>/WEB-INF/struts-html.tld</taglib-location>
</taglib>
On top of that, you must copy the struts-html.tld file (in the lib directory of Struts’ deployment directory) to your WEB-INF directory.
Note
A list of attributes are given for each tag. All attributes are optional except those indicated by an asterisk (*). Default values are underlined.
Attributes of a typical tag falls into two classes: Struts related attributes and HTML attributes. The latter specify JavaScript event handlers and are simply passed to the rendered code by the tag handler. For example, the form tag has the action attribute, which has a special purpose in Struts and will be processed by the tag handler. The form tag also has the onsubmit attribute, which is not processed and will simply be rendered as the onsubmit attribute of the resulting HTML form element. The onsubmit attribute, and many other attributes that do not get processed by Struts, will not be included in the tables of attributes. The purpose of doing so is twofold. First, we save space. Second, you can focus more on the fewer attributes that are Struts-related.