The template tag library contains four tag handlers (DefinitionTag, ScreenTag, ParameterTag, and InsertTag) that demonstrate the use of cooperating tags. DefinitionTag, ScreenTag, and ParameterTag constitute a set of nested tag handlers that share private objects. DefinitionTag creates a public object named bookstore that is used by InsertTag.
In doTag, tut-install/javaeetutorial5/examples/web/bookstore3/src/java/com/sun/bookstore3/template/DefinitionTag.java creates a private object named screens that contains a hash table of screen definitions. A screen definition consists of a screen identifier and a set of parameters associated with the screen. These parameters are loaded when the body of the definition tag, which contains nested screen and parameter tags, is invoked. DefinitionTag creates a public object of class tut-install/javaeetutorial5/examples/web/bookstore3/src/java/com/sun/bookstore3/template/Definition.java, selects a screen definition from the screens object based on the URL passed in the request, and uses this screen definition to initialize a public Definition object.
public int doTag() { try { screens = new HashMap(); getJspBody().invoke(null); Definition definition = new Definition(); PageContext context = (PageContext)getJspContext(); ArrayList params = (ArrayList) screens.get(screenId); Iterator ir = null; if (params != null) { ir = params.iterator(); while (ir.hasNext()) definition.setParam((Parameter)ir.next()); // put the definition in the page context context.setAttribute(definitionName, definition, context.APPLICATION_SCOPE); } }
The table of screen definitions is filled in by ScreenTag and ParameterTag from text provided as attributes to these tags. Table 8–14 shows the contents of the screen definitions hash table for the Duke’s Bookstore application.
Table 8–14 Screen Definitions
Screen ID |
Title |
Banner |
Body |
---|---|---|---|
/bookstore |
Duke’s Bookstore |
/banner.jsp |
/bookstore.jsp |
/bookcatalog |
Book Catalog |
/banner.jsp |
/bookcatalog.jsp |
/bookdetails |
Book Description |
/banner.jsp |
/bookdetails.jsp |
/bookshowcart |
Shopping Cart |
/banner.jsp |
/bookshowcart.jsp |
/bookcashier |
Cashier |
/banner.jsp |
/bookcashier.jsp |
/bookreceipt |
Receipt |
/banner.jsp |
/bookreceipt.jsp |
If the URL passed in the request is /bookstore, the Definition object contains the items from the first row of Table 8–14 (see Table 8–15).
Table 8–15 Definition Object Contents for URL /bookstore
Title |
Banner |
Body |
---|---|---|
Duke’s Bookstore |
/banner.jsp |
/bookstore.jsp |
The parameters for the URL /bookstore are shown in Table 8–16. The parameters specify that the value of the title parameter, Duke’s Bookstore, should be inserted directly into the output stream, but the values of banner and body should be included dynamically.
Table 8–16 Parameters for the URL /bookstore
Parameter Name |
Parameter Value |
isDirect |
---|---|---|
title |
Duke’s Bookstore |
true |
banner |
/banner.jsp |
false |
body |
/bookstore.jsp |
false |
tut-install/javaeetutorial5/examples/web/bookstore3/src/java/com/sun/bookstore3/template/InsertTag.java inserts parameters of the screen definition into the response. The doTag method retrieves the definition object from the page context and then inserts the parameter value. If the parameter is direct, it is directly inserted into the response; otherwise, the request is sent to the parameter, and the response is dynamically included into the overall response.
public void doTag() throws JspTagException { Definition definition = null; Parameter parameter = null; boolean directInclude = false; PageContext context = (PageContext)getJspContext(); // get the definition from the page context definition = (Definition)context.getAttribute( definitionName, context.APPLICATION_SCOPE); // get the parameter if (parameterName != null && definition != null) parameter = (Parameter) definition.getParam(parameterName); if (parameter != null) directInclude = parameter.isDirect(); try { // if parameter is direct, print to out if (directInclude && parameter != null) context.getOut().print(parameter.getValue()); // if parameter is indirect, include results of dispatching to page else { if ((parameter != null) && (parameter.getValue() != null)) context.include(parameter.getValue()); } } catch (Exception ex) { throw new JspTagException(ex.getMessage()); } }