8.4 Struts HTML TagsThe Struts HTML tag library contains tags used to create HTML input forms, as well as other tags generally useful in the creation of HTML-based user interfaces. For example, instead of using a regular HTML text-input field, you can use the text tag from this library. These tags are designed to work very closely with the other components of the Struts framework, including ActionForms. You always should attempt to use one of these tags first, rather than using standard HTML. These tags' special knowledge of the rest of the Struts components makes it worthwhile to use them. Most of the tags within this library must be nested inside of a Struts form tag. There are a few tags included in this library that address issues not necessarily related to forms or form widgets. Those tags also will be discussed briefly in this section. Although not every HTML tag will be discussed, Table 8-1 provides a complete list of the tags that are available in the HTML tag library.
As stated earlier, this chapter will not discuss every tag within the Struts framework; to do so would be redundant, as that material is covered thoroughly in the Struts JavaDocs. Instead, certain tags will be selected and discussed based on their importance, on how confusing they are to new developers, and on whether there are practical strategies for using the tags. If you need a reference to the complete set of available tags, the Jakarta Struts Pocket Reference (O'Reilly) includes information and examples on all of the Struts tags. 8.4.1 Thel TagThe Starting with Struts 1.2, the locale attribute
will be deprecated and replaced with the lang
attribute. In Struts, the locale is tied to a user's
HttpSession object. In 1.1 and earlier, using the
locale attribute requires this session object to
be created; however, not all applications use sessions, and it is
undesirable for those that don't to use them. The
lang attribute will achieve the same functionality
without the need for a session. Struts 1.1 added a new attribute to the This tag renders an HTML base
element with an href attribute pointing to the
absolute location of the enclosing JSP page. This tag is useful
because it allows you to use relative URL references that are
calculated based on the URL of the page itself, rather than the URL
to which the most recent submit took place (which is what the browser
normally would resolve relative references against). This tag must be
placed within the HTML head element. The following is an example of
using the base tag: The base tag example here would produce the
following output when executed in the main page of the Storefront
application: This tag is very important when using relative URLs for images in a
JSP page. There are two attributes for the base tag:
target and server. You can use
the target attribute in order to specify a window target for this
base reference. The server attribute is used to specify the server
name, and overrides the value that comes from
request.getServerName(). The Struts form tag
is one of the most important tags in the HTML tag library. Its
purpose is to render a standard HTML form tag and to link the HTML
form with an ActionForm configured for the
application. Each field in the HTML form should correspond to a property of the
ActionForm. When an HTML field name and a property
name match, the property from the ActionForm is
used to populate the HTML field. When the HTML form is submitted, the
framework will store the user's input into the
ActionForm, again matching up the HTML field names
to the property names. All of the HTML custom tags that render HTML controls must be nested
within the The form tag controls many important aspects of
the page. Its attributes are shown in Table 8-2. Name Description The URL to which this form will be submitted. The content encoding to be used when submitting this form. The field name to which initial focus will be assigned for this page. If the focus field is a field array, such as a radio button group,
you can specify the index in the array to receive focus. The HTTP method that will be used to submit this request. The name of the ActionForm whose properties will
be used to populate the input field values. The JavaScript event handler executed if the form is reset. The JavaScript event handler executed if the form is submitted. The scope of the ActionForm for this form. The CSS styles to be applied to this HTML element. The CSS stylesheet class to be applied to this HTML element. The identifier to be assigned to this HTML element. The frame target to which this form is submitted. The fully qualified class name of the ActionForm
for this page. The value for the action
attribute is used to select the ActionMapping the
page is assumed to be processing, from which we can identify the
appropriate ActionForm and scope. If extension mapping is being used (*.do), the
action value should be equal to the value of the
path attribute of the corresponding action
element, optionally followed by the correct extension suffix. An
example of this is: If path mapping is used instead, the action attribute value should be
exactly equal to the value of the path attribute of the corresponding
action element: Typically, you won't need to set the
enctype attribute. However, if
your form is performing file uploads, you should set the
enctype attribute to
multipart/form-data. You also must make sure the
method attribute is set to POST, which is the
default method if none is specified. The name attribute specifies the
name of the request- or session-scope ActionForm
whose properties will be used to populate the input field values. If
no such bean is found, a new bean will be created and added to the
appropriate scope, using the Java class name specified by the type
attribute. If no value is specified for the name attribute,
it will be calculated by using the value of the action attribute to
look up the corresponding ActionMapping element,
from which the form bean name will be determined. In other words, if
no name attribute is specified for the
form tag, the tag will use the value from the
name attribute in the action
element from the configuration file. This is a very important point
that confuses many new Struts developers. Let's look
at an example. Suppose there is an action element
configured like the following: Now say you have a form tag that looks like this declared in a JSP
page: Because the name attribute is not specified in the
form tag, the tag will look up the
signin action from the configuration file. It
will retrieve the value of the name attribute from
the action element and use that to check for an
ActionForm in either the request or session scope.
In this case, the loginForm will be selected
because it's the value for the
name attribute in the action
element. The scope attribute defines where
the tag should look for the ActionForm. Its value
must be either request or
session. If the scope attribute
is not specified, it will be calculated by using the value of the
action attribute to look up the corresponding
ActionMapping element, from which we will select
the specified form bean scope. This is similar to how the
name attribute is determined if
it's not specified in the form
tag. The type attribute specifies the
fully qualified class name of the ActionForm to be
created if no such bean is found in the specified scope. If this
attribute is not specified, it will be calculated by using the value
of the action attribute to look up the corresponding
ActionMapping element, from which we will select
the specified form bean type. As with standard HTML, you can include
more than one form tag within a JSP page.
Obviously, only one form can be submitted at a time, but that
doesn't stop you from declaring multiple
form tags. For example, you might have a
form tag for a search area of the page. When the
user presses the search button, that form is submitted along with the
search criteria fields. In that same page, you might also have
another form tag that performs a different
function. When a button within that form is pressed, that form and
its corresponding fields are submitted. Example 8-1
provides an example of what that might look like. These two tags render HTML
input elements of type
button, populated from the
specified value or from the content of the tag body. These tags are
valid only when nested inside a form tag body. Stylesheets also can be used with these tags by supplying a value for
the styleClass attribute. By default, the button
label is set to the value "Click"
and the cancel label is set to the value
"Cancel". You can override this
using the value attribute. The button produced by the cancel tag has a special characteristic
that causes the validate() method to be skipped
when it's pressed. The
RequestProcessor just calls the execute(
) method, without going through the validation routine. This tag renders an HTML
input element of type checkbox,
populated from the specified value or the specified property of the
bean associated with the current form. This tag is valid only when
nested inside a form tag body. The underlying property value associated with this field should be of
type boolean, and any value you specify should
correspond to one of the strings that indicate a true value
("true",
"yes", or
"on"). These
two tags are responsible for
displaying a set of general-purpose messages or errors to the user.
Messages correspond to ActionMessages, and errors
to ActionErrors. The messages/errors are created
either in the validate() method or by the
exception-handling framework. If no messages or errors are present,
nothing will be rendered. When using the errors tag, the message bundle must
include message keys for the following values: Text that will be rendered before the error messages list. Typically,
this message text will end with <ul> to
start the error messages list. Text that will be rendered after the error messages list. Typically,
this message text will begin with </ul> to
end the error messages list. For example, we might set the header and footer values to: When the errors are written out to the HTML page,
they'll appear inside a bulleted list. Now that the
Struts framework supports multiple
MessageResources within the configuration file,
you can specify which one should be used using the
bundle attribute. Many of the HTML tags support JavaScript event handlers through the
use of their attributes. For example, to configure an
onClick handler for a supported tag, you need to
include the function name in the onClick attribute
for the tag. Table 8-3 lists the attributes for
the supported event handlers. Attribute Description Executed when this element loses input focus. Executed when this element loses input focus and its value has
changed. Executed when this element receives a mouse click. Executed when this element receives a mouse double-click. Executed when this element receives input focus. Executed when this element has focus and a key is depressed. Executed when this element has focus and a key is depressed and
released. Executed when this element has focus and a key is released. Executed when this element is under the mouse pointer and a mouse
button is depressed. Executed when this element is under the mouse pointer and the pointer
is moved. Executed when this element is under the mouse pointer but the pointer
is moved outside the element. Executed when this element is not under the mouse pointer but the
pointer is moved inside the element. Executed when this element is under the mouse pointer and a mouse
button is released. Executed if the parent form is reset. Executed if the parent form is submitted. Many of the tags also support navigation using only the keyboard.
This is done using the attributes listed in Table 8-4. Attribute Description The keyboard character used to move focus immediately to this element. The tab order (ascending positive integers) for this element.
|