Java Reference
In-Depth Information
<td>Zip: </td>
<td><input type="text"
name="${addressType}_zip"
id="${addressType}.zip"
value="${addressBean.zip}" />
</td>
</tr> </
table>
Notice that a tag file is not much different from a JSP, it can use JSTL and other
tag libraries, it has access to the same implicit objects that a JSP has access to. In
our example we use the
<jsp:useBean>
tag to access a JavaBean of type
net.
ensode.netBeansbook.AddressBean
, this is a simple JavaBean containing a default
argument constructor and a few properties.
Since the
AddressBean
is so simple, its code is not shown, however it is
available as part of this topic's code download.
Our tag file also uses the JSTL core tag library to implement some conditional logic.
As we mentioned earlier, a tag file can contain one or more attributes. Our tag file
contains a single simple attribute, named
addressType
, this attribute is a
String
we
use to append to the names of all input fields in the tag file. The reason we do this is
to allow a single JSP to use multiple instances of our tag, allowing fields generated
by each tag in the page to have a unique name. Tags can be optional or required, to
make a tag required the
required
attribute of the attribute directive is used, setting
its value to
true
, since not passing the
addressType
attribute to our tag would
potentially generate duplicate input field names and IDs in a single page, we made
this attribute required in our custom address tag.
Notice the comment generated by NetBeans states that normal or fragment attributes
can be defined. Fragment attributes allow the page using our tag to send snippets of
HTML code to our tag, fragment attributes are defined
<%@attribute name="myattribute" fragment="true"%>
Setting the
fragment
attribute to
true
indicates that this attribute is a fragment
attribute.
To render the fragment attribute in our tag, the
<jsp:invoke>
standard action:
<jsp:invoke fragment="myattribute"/>