jutil Tag Library Documentation

Library Version: 1.0
Library URI: /WEB-INF/jutil.tld
JSP Required: 1.1

A library of JSP utility tags.

Tag Summary
<jutil:attribute> This sets an attribute for an element tag.
<jutil:element> This tag specifies the span of a page element.
<jutil:else> A tag for evaluating conditionals.
<jutil:elseIf> A tag for evaluating conditionals.
<jutil:forEach> A utility tag used for looping.
<jutil:if> A tag for evaluating conditionals.
<jutil:new> A tag to create new object.
<jutil:param> This tag specifies parameters for parametrized tags.
<jutil:print> A tag print object data.
<jutil:printError> This tag prints an error message, if one is present.
<jutil:set> A tag to load object properties of other objects.
<jutil:use> A tag to load page data from a controller.

Library Description

A library of JSP utility tags. It has tags controlling basic page logic.

Tag Details

<jutil:use> Tag

Tag Handler Class: org.chwf.taglib.jutil.UseTag
Tag Body Content: empty
Tag Attributes: var, controller, property

This tag is used to load object data from a controller. These objects are defined as properties of the controller. Consult the documentation for individual controllers to determine the properties available in that controller.

The property attribute of this tag is optional. If omitted, it defaults to the value of the var attribute. The following are equivalent:

<jutil:use var="order" controller="Cart" />
<jutil:use var="order" controller="Cart" property="order" />

The JSP page may need some request parameters to properly retrieve object data from the controller. For example, suppose the following tag appeared in the showItem.jsp page using the Catalog controller from the sample catalog application:

<jutil:use var="item" controller="Catalog" />
public class CatalogController extends Controller {
  public Item getItem(Long itemId) throws Exception {
    return Item.load(itemId);
  }
  
  // Other methods ...
}

To load the item object, the URL would need to pass the item id value of the item being retrieved, e.g. showItem.jsp?itemId=423.

Technical Note: The required parameters will be determined by the initializer/getter method for the controller in question. For more information, see the documentation for the Controller in the Java Developers Guide.

Tag Attributes

  • var (Required): The variable storing the retrieved object.
  • controller (Required): Name of the controller.
  • property (Optional): The object property being retrieved from the controller. Defaults to the var value.

<jutil:new> Tag

Tag Handler Class: org.chwf.taglib.jutil.NewTag
Tag Body Content: empty
Tag Attributes: var, type

This tag is used to create brand new objects. It is useful for creating page helper objects that are not associated with any given controller or new objects for forms.

<jutil:new var="item" type="com.domain.catalog.Item" />

<h1>Create Catalog Item</h1>

<jhtml:form action="Catalog.createItem.cmd">
<table>
  <jhtml:field property="name" />
  <jhtml:field property="stock" />
  <jhtml:field property="price" />
  <jhtml:field property="startDate" />
  <tr>
    <td>&nbsp;</td>
    <td><input type="submit" value="Submit"></td>
  </tr>
</table>
</jhtml:form>

Technical Note: The new business object created by this tag is simply a dummy object, used only for form generation. Its values should not be manipulated by this page.

Tag Attributes

  • var (Required): The variable storing the object.
  • type (Required): The fully-qualified name of the new object's Java class.

<jutil:set> Tag

Tag Handler Class: org.chwf.taglib.jutil.SetTag
Tag Body Content: empty
Tag Attributes: var, object, property, el

This tag gets a subordinate object out of another object and stores the subordinate object in a page variable. A subordinate object is one that is stored in a property of its owner. Consult an object's documentation to determine which subordinate objects it contains.

The example below gets the lineItems property out of the order object and stores it in a variable named lineItems:

<jutil:use var="order" controller="Cart" />
<jutil:set var="lineItems" object="order" />

Alternately, this tag can use a JSTL Expression Language statement to define a value. For example, so store the "preview" request parameter in an "isPreview" variable:

<jutil:set var="isPreview" el="${param.preview}" />

Technical Note: This tag uses the appropriate getter method to retrieve the subordinate object.

Tag Attributes

  • var (Required): The variable storing the retrieved object.
  • object (Optional): Name of the owning object.
  • property (Optional): The object property being retrieved. Defaults to the var value.
  • el (Optional): The JSTL Expression Language statement determining the value.

<jutil:print> Tag

Tag Handler Class: org.chwf.taglib.jutil.PrintTag
Tag Body Content: empty
Tag Attributes: object, property, attribute, el, datatype

This tag prints object information. The example below prints the name property of the item object:

<jutil:use var="item" controller="Catalog" />
<jutil:print object="item" property="name" />

The print tag can be used to print information at three different levels of specificity:

Object value:
<jutil:print object="item" />

Object property value:
<jutil:print object="item" property="price" />

Property attribute value:
<jutil:print object="item" property="price" attribute="label" />

If no property is specified, the value of the object itself is printed instead. If a property attribute is specified, the attribute value is printed. If no object is specified, the default object is used.

Alternately, the print value can be expressed using a JSTL Expression Language statement:

<jutil:print el="{$param.redirect}" />

The information is not necessarily displayed to the user. For example, this tag can be used to insert parameter values into URLs:

<jutil:use var="item" controller="Catalog" />

<a href="showItem.jsp?itemId=<jutil:print property='itemId' />">
  Show Item
</a>

If a datatype is specified, the tag attempts to print the value as the specified datatype rather than its normal datatype:

<jutil:print object="item" property="startDate" datatype="LongDate" />

Technical Note: This tag uses the appropriate getter method to retrieve the property value. If no property is specified, it uses the object's toString() method to display information. Also, this tag searches all web application scopes to retrieve the object value, so this tag can be used to print values from the request, session and application scopes.

Tag Attributes

  • object (Optional): The object being printed. Uses default object if omitted.
  • property (Optional): The property being printed. The object value itself is displayed if no property is specified.
  • attribute (Optional): The property attribute being printed. If specified, the tag will print the attribute value instead of the property value.
  • el (Optional): The JSTL Expression Language statement determining the value.
  • datatype (Optional): The converter datatype used to print the value. If not specified, the converter is derived from the object or property type. If a logical datatype is used, it must be compatible with the value's Java datatype. This attribute is meaningless when printing property attributes instead of property values.

<jutil:forEach> Tag

Tag Handler Class: org.chwf.taglib.jutil.ForEachTag
Tag Body Content: JSP
Tag Attributes: var, collection, el

This tag is used for looping through a collection of objects, so their values can be printed one-by-one. It is often used to generate HTML tables from object data.

<jutil:use var="itemList" controller="Catalog" />

<table>
<jutil:forEach var="item" collection="itemList">
  <tr>
    <td><jutil:print property="itemId" /></td>
    <td><jutil:print property="name" /></td>
    <td><jutil:print property="stock" /></td>
  </tr>
</jutil:forEach>
</table>

For example, if the itemList had five objects, the code above would print five table rows. Each row would have different values for the object properties. This is because each time the row was printed, the item variable would hold a different object value.

The collection attribute must be the name of a previously initialized object. Furthermore, it cannot be just any object; it must be a collection. The documentation for the application's objects and controllers should specify which properties are collections as opposed to individual objects.

During iteration, the current iteration count is stored in a variable named counter. The counter value starts at 1 for the first iteration, and increases by one for each additional iteration. The counter value is only valid inside the loop; outside the loop it has no value.

Technical notes: Legitimate collections include arrays of objects, Collection classes and Map classes. For maps, the map values [map.values()] are used for the collection.

Also note that this tag has no facilities for looping through a subset of a collection (as is the case in JSTL). This is because this kind of logic is better handled in the controller layer. To display a subset of a collection, define a controller method that derives the correct subset and returns a collection containing that subset to the JSP via a <jutil:use> tag.

Tag Attributes

  • var (Required): The variable storing each object within the loop.
  • collection (Optional): The name of the collection. It behaves like a object attribute.
  • el (Optional): The JSTL Expression Language statement determining the collection.

<jutil:printError> Tag

Tag Handler Class: org.chwf.taglib.jutil.PrintErrorTag
Tag Body Content: empty
Tag Attributes: parameter

This tag checks for the existence of an error (or exception) and prints out its message. If there is no error, this tag prints nothing.

<%@ page isErrorPage="true" %>

<p><b><font color="red"><jutil:printError /></font></b></p>

Optionally, you may specify a parameter attribute to print the error associated with a particular input parameter. This attribute is only meaningful if the original error was a MultiParameterException. If the error is any other kind of exception, or there is no error message associated with the given parameter, nothing is printed.

<table>
...
  <tr colspan="2"><td>
    <font color="red"><jutil:printError parameter="name" /></font>
  </td></tr>
  <tr>
    <td><jhtml:label property="name" /></td>
    <td><jhtml:input property="name" /></td>
  </tr>
...
</table>

Technical Note: This tag searches the standard request attribute for the exception, which is compatible with the JSP standard for error pages. This attribute's name is defined in the constant javax.servlet.jsp.PageContext.EXCEPTION. The parameter attribute only functions correctly if the exception is a org.chwf.servlet.MultiParameterException.

Tag Attributes

  • parameter (Optional): The name of a parameter with which the error is associated.

<jutil:if> Tag

Tag Handler Class: org.chwf.taglib.jutil.IfTag
Tag Body Content: JSP
Tag Attributes: object, property, el

This tag evaluates an object property as a conditional (boolean) value:

<jutil:use var="items" controller="Cart" />

<jutil:if object="items" property="empty">
  <p><font color="red">No items in cart.</font></p>
</jutil:if>

If the property evaluates to a value of true, the tag body is executed. Otherwise, it is not.

If the property is omitted, the object itself is evaluated as a boolean condition value.

Alternately, you can define the condition using a JSTL expression language value:

<jutil:if el="{$param.preview}">
  Show preview ...
</jutil:if>

Tag Attributes

  • object (Optional): The object being tested. Uses default object if omitted.
  • property (Optional): The property being evaluated.
  • el (Optional): A test in JSTL Expression Language.

<jutil:else> Tag

Tag Handler Class: org.chwf.taglib.jutil.ElseTag
Tag Body Content: JSP

This tag works in conjunction with the <jutil:if> tag.

<jutil:use var="items" controller="Cart" />

<jutil:if object="items" property="empty">
  <p><font color="red">No items in cart.</font></p>
</jutil:if>
<jutil:else>
  [Print out cart contents]
</jutil:else>

If the preceding <jutil:if> tag evaluates to false, the contents of the <jutil:else> tag will execute. The <jutil:else> must follow the <jutil:if> tag. Nesting the <jutil:else> is incorrect:

<jutil:use var="items" controller="Cart" />

<jutil:if object="items" property="empty">
  <p><font color="red">No items in cart.</font></p>
  <jutil:else>
    [THIS WILL NOT WORK]
  </jutil:else>
</jutil:if>

<jutil:elseIf> Tag

Tag Handler Class: org.chwf.taglib.jutil.ElseIfTag
Tag Body Content: JSP
Tag Attributes: object, property, el

This tag is effectively a combination of an "else" and an "if" tag. Its contents will be executed provided that (a) the previous if tag evaluated to false and (b) the elseif evaluates to true.

Tag Attributes

  • object (Optional): The object being tested. Uses default object if omitted.
  • property (Optional): The property being evaluated.
  • el (Optional): A test in JSTL Expression Language.

<jutil:param> Tag

Tag Handler Class: org.chwf.taglib.jutil.ParamTag
Tag Body Content: JSP
Tag Attributes: name, object, property

This tag specifies parameter values for parametrized tags. The <jutil:param> tag is only valid when it is the immediate child of a parametrized parent tag. The value for the param tag can be set in several ways:

Option Source Code
Value in tag body <jutil:param name="id">
<jutil:print object="item" property="itemId"/>
</jutil:param>
The parameter value can be specified in the tag body. All whitespace is trimmed.
Value in object property <jutil:param name="itemId" object="item" property="itemId"/>
The parameter value can be derived from an object property. If no object is specified, the default object is used. If no name is specified, it defaults to the name of the property.
No value specified <jutil:param name="itemId"/>
If no value is specified, the parameter value is retrieved from the request parameters or from the string value of the object with the same name.

Tag Attributes

  • name (Optional): The parameter name. Parameter value is retrieved from web context.
  • object (Optional): The object name. If no object is specified, the default object.
  • property (Optional): The property name. The parameter value is retrieved from the object property.

<jutil:element> Tag

Tag Handler Class: org.chwf.taglib.jutil.ElementTag
Tag Body Content: JSP
Tag Attributes: name

This tag specifies the span of a page element. It is used in conjunction with the <jutil:attribute> tag to create a page element with attributes. The use of these two tags is similar to the XSLT tags of the same names; they can create JSP pages that are well-formed XML. A typical JSP construction might be something like:

<a href="showItem?id=<jutil:print object="item" property="itemId"/>">
  Go to item <jutil:print object="item" property="itemId"/>
</a>

This can be replaced by the verbose but well-formed:

<jutil:element name="a">
  <jutil:attribute name="href">
    showItem?id=<jutil:print object="item" property="itemId"/>
  </jutil:attribute>
  Go to item <jutil:print object="item" property="itemId"/>
</jutil:element>

The output is:

<a href="showItem?id=###">Go to item ###</a>

Here "###" is the value of the item's id value.

Tag Attributes

  • name (Required): Specifies the name of the generated element.

<jutil:attribute> Tag

Tag Handler Class: org.chwf.taglib.jutil.AttributeTag
Tag Body Content: JSP
Tag Attributes: name, object, property

This tag generates an attribute for a <jutil:element> tag. Its syntax is identical to the <jutil:param> tag. The value for the attribute tag can be set in several ways:

Option Source Code
Value in tag body <jutil:attribute name="id">
<jutil:print object="item" property="itemId"/>
</jutil:attribute>
The attribute value can be specified in the tag body. All whitespace is trimmed.
Value in object property <jutil:attribute name="itemId" object="item" property="itemId"/>
The attribute value can be derived from an object property. If no object is specified, the default object is used. If no name is specified, it defaults to the name of the property.
No value specified <jutil:attribute name="itemId"/>
If no value is specified, the attribute value is retrieved from the request parameters or from the string value of the object with the same name.

Unlike the <jutil:param> tag, this tag does not need to be an immediate child of the <jutil:element> tag, but can be more deeply nested (e.g. inside conditional tags).

<jutil:element name="input">
  <jutil:if object="question" property="multipleCorrectAnswers">
    <jutil:attribute name="type">checkbox</jutil:attribute>
  </jutil:if>
  <jutil:else>
    <jutil:attribute name="type">radio</jutil:attribute>
  </jutil:else>
  <jutil:if object="answer" property="selected">
    <jutil:attribute name="checked">checked</jutil:attribute>
  </jutil:if>
  <jutil:attribute name="name">answerId</jutil:attribute>
  <jutil:attribute name="value" object="answer" property="id" />
</jutil:element>

Tag Attributes

  • name (Optional): The attribute name.
  • object (Optional): The object name. If no object is specified, the default object.
  • property (Optional): The property name. The attribute value is retrieved from the object property.