10.12.2012 Views

The Java EE 5 Tutorial (PDF) - Oracle Software Downloads

The Java EE 5 Tutorial (PDF) - Oracle Software Downloads

The Java EE 5 Tutorial (PDF) - Oracle Software Downloads

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Programming SimpleTag Handlers<br />

268<br />

}<br />

}<br />

Tag Handlers<br />

HttpServletResponse response) {<br />

request.setAttribute("selectedScreen",<br />

request.getServletPath());<br />

try {<br />

request.<br />

getRequestDispatcher(<br />

"/template/template.jsp").<br />

forward(request, response);<br />

} catch(Exception ex) {<br />

ex.printStackTrace();<br />

}<br />

<strong>The</strong> template tag library contains four tag handlers (DefinitionTag, ScreenTag,<br />

ParameterTag, and InsertTag) that demonstrate the use of cooperating tags. DefinitionTag,<br />

ScreenTag, and ParameterTag constitute a set of nested tag handlers that share private objects.<br />

DefinitionTag creates a public object named bookstore that is used by InsertTag.<br />

In doTag,<br />

tut-install/javaeetutorial5/examples/web/bookstore3/src/java/com/sun/bookstore3/template/Defi<br />

creates a private object named screens that contains a hash table of screen definitions. A screen<br />

definition consists of a screen identifier and a set of parameters associated with the screen.<br />

<strong>The</strong>se parameters are loaded when the body of the definition tag, which contains nested screen<br />

and parameter tags, is invoked. DefinitionTag creates a public object of class<br />

tut-install/javaeetutorial5/examples/web/bookstore3/src/java/com/sun/bookstore3/template/Defi<br />

selects a screen definition from the screens object based on the URL passed in the request, and<br />

uses this screen definition to initialize a public Definition object.<br />

public int doTag() {<br />

try {<br />

screens = new HashMap();<br />

getJspBody().invoke(null);<br />

Definition definition = new Definition();<br />

PageContext context = (PageContext)getJspContext();<br />

ArrayList params = (ArrayList) screens.get(screenId);<br />

Iterator ir = null;<br />

if (params != null) {<br />

ir = params.iterator();<br />

while (ir.hasNext())<br />

definition.setParam((Parameter)ir.next());<br />

// put the definition in the page context<br />

context.setAttribute(definitionName, definition,<br />

context.APPLICATION_SCOPE);<br />

}<br />

}<br />

<strong>The</strong> <strong>Java</strong> <strong>EE</strong> 5<strong>Tutorial</strong> • June 2010

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!