Java Reference
In-Depth Information
The state drop-down menu will have an optional attribute called selected that allows a designer to specify a state
abbreviation to be selected when the drop-down menu is displayed. The following XML code identifies the tag, tag
handler, and defines the tag attribute selected.
<tag>
<name>stateDDM</name>
<tagclass>c11.TNTStateDDM</tagclass>
<attribute>
<name>selected</name>
<required>false</required>
</attribute>
</tag>
The required tag is not required because attributes by default are optional. However, to define a required
attribute, the “subelement” required must be specified and its value set to true.
An attribute must be specified in the start tag to make it available to the doStartTag method. The attribute name
is specified after the tag name, separated by at least one space. For example, the following would define GA as the
selected value in the custom tag stateDDM:
<TNT:stateDDM selected="GA"></TNT:stateDDM >
When the server encounters the keyword selected, the setter (in the TNTStateDDM tag handler class) will be
invoked and passed the value GA. The TNTStateDDM class must check the value of the variable selected and generate
the appropriate HTML such that the value is selected in the drop-down menu.
How to Generate a Visual Component
The stateDDM tag will generate a drop-down menu that has only three values to choose from FL, GA, and OK. (The
tag handler c11.TNTStateDDM, of course, is actually doing all the work. In this case, the work entails inserting the
HTML that defines the drop-down menu into the JSP. TNTStateDDM will do this similarly to how servlets manipulate
a page. In a servlet, the programmer had to:
A.
Create a PrintWriter object
B.
Tie the print writer to the response object
C.
Run the print writer's println method and embed the HTML in the response object
The tag handler (TNTStateDDM) will do the same, however, the JSP container (part of the server) makes things
much easier by providing a JspWriter that is already associated with the JSP response object. In other words, the tag
handler does not have to perform steps A and B from above. Rather the JSP writer is retrieved (using the ever-useful
PageContext object) and the write method is executed. The following statement will do this:
pageContext.getOut().write(" HTML to be embedded ");
There is just one small hitch. Notice that the following HTML (which defines a drop-down menu, with the
three state abbreviation options, and FL specified as the selected value) contains double quotes and that the write
command above requires that the HTML be enclosed in double quotes.
<SELECT name="stateDDM">
<OPTION value="FL" selected>FL</OPTION>
<OPTION value="GA">GA</OPTION>
<OPTION value="OK">OK</OPTION>
</SELECT>
 
Search WWH ::




Custom Search