jhtml Tag Library Documentation

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

A library of JSP html-related tags.

Tag Summary
<jhtml:field> A tag to print a label and input field for a property.
<jhtml:fieldPattern> A tag to specify output patterns for fields.
<jhtml:form> A tag to specify the object for a form.
<jhtml:input> A tag to populate an input element with a object property.
<jhtml:label> A tag to print a field label.
<jhtml:link> A tag to create hyperlinks.

Library Description

A library of JSP html-related tags. It has tags for auto-generating HTML code based on object data. Typically, the jhtml tags will be mixed with normal HTML tags (such as table tags) to render data:

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

<h1>Edit Catalog Item</h1>

<jhtml:form action="Catalog.editItem.cmd">
<table>
  <tr>
    <td><jhtml:label property="name" />: </td>
    <td><jhtml:input property="name" /></td>
  </tr>
  <tr>
    <td><jhtml:label property="stock" />: </td>
    <td><jhtml:input property="stock>" /></td>
  </tr>
  <tr>
    <td></td>
    <td><input type="submit" value="Submit"></td>
  </tr>
</table>
</jhtml:form>

Technical Note: This library uses object metadata derived via the BeanFilter API. Consult the documentation for the org.chwf.filter package more information.

In addition, the HTML class attribute used to define CSS style classes is replaced by the styleClass attibute in jhtml tags. This is because the term "class" refers to a different type of information in JSP custom tags (the Java class), and cannot be used.

Tag Details

<jhtml:form> Tag

Tag Handler Class: org.chwf.taglib.jhtml.FormTag
Tag Body Content: JSP
Tag Attributes: action, object, validations, styleClass, id, lang, style, title, accept, enctype, method, name, target

This tag replaces the HTML <form> tag for generating HTML forms. If specified, the object in the <jhtml:form> tag becomes the default object, to make it easier to use other form tags. Other variable declarations will still replace this default object, as usual.

Tag Attributes

  • action (Required): The controller method that processes the form. The action URL should not include the context path.
  • object (Optional): The name of the object which this form will update. If not specified, the default object is used. Otherwise, the specified object becomes the default object.
  • validations (Optional): Whether JavaScript validations are enabled. Defaults to true.
  • styleClass (Optional): Same meaning as "class" in HTML.
  • id (Optional): Same meaning as in HTML.
  • lang (Optional): Same meaning as in HTML.
  • style (Optional): Same meaning as in HTML.
  • title (Optional): Same meaning as in HTML.
  • accept (Optional): Same meaning as in HTML.
  • enctype (Optional): Same meaning as in HTML.
  • method (Optional): Same meaning as in HTML.
  • name (Optional): Same meaning as in HTML.
  • target (Optional): Same meaning as in HTML.

<jhtml:input> Tag

Tag Handler Class: org.chwf.taglib.jhtml.InputTag
Tag Body Content: empty
Tag Attributes: type, object, property, name, validations, options, styleClass, id, lang, style, title, accept, accesskey, disabled, maxlength, size, cols, rows, wrap

This tag replaces the HTML <input> tag for input elements. Rather than having the name and value attributes, it specifies a object property and uses that property for both the field name and to derive the field's default value.

The type attribute supports many of the same types as the HTML <input> element, plus three additional types:

  • text: Prints a text field.
  • password: Prints a password field.
  • hidden: Prints a hidden form field.
  • checkbox: Prints a checkbox.
  • textarea: Prints a textarea.
  • select: Prints a (non-multivalue) select list. This type only works for properties that have option lists.
  • fixed: Prints a fixed value, and stores the same value in a hidden form field. This is useful for read-only values like object ids.

If the type attribute is omitted, the type is derived from the object property's characteristics:

  • Read-only properties are printed as fixed fields.
  • Properties with options are printed as select lists.
  • Boolean properties are printed as checkboxes.
  • Properties with maxlength > 255 are printed as textareas.
  • Otherwise, the property is printed as a text field.

This tag also generates JavaScript validation code for the field. For the generated validations to work, the page must import and initialize the FormValidation.js library with code like the following:

<script src="/catalog/javascript/FormValidation.js"></script>
<body onload="initFormValidation()">

It is best to put this JavaScript initialization login in the web application's template pages, since it will be used globally.

Tag Attributes

  • type (Optional): The input element type to be created. If omitted, the type is derived from the object property's characteristics.
  • object (Optional): The name of the object. In most cases, this attribute can be omitted, because the form's object becomes the default object.
  • property (Required): The object property that this field will edit. The field is pre-populated with the property's current value.
  • name (Optional): Same meaning as in HTML. Defaults to property name if absent.
  • validations (Optional): Whether JavaScript validations are enabled. Defaults to the value of form's validation attribute, or true if not specified anywhere.
  • options (Optional): The name of the options list for a select list. This attribute should only be specified for input elements of type "select". If not specified, the object's options attribute will be used instead. If neither is specified, an error will be generated.
  • styleClass (Optional): Same meaning as "class" in HTML.
  • id (Optional): Same meaning as in HTML.
  • lang (Optional): Same meaning as in HTML.
  • style (Optional): Same meaning as in HTML.
  • title (Optional): Same meaning as in HTML.
  • accept (Optional): Same meaning as in HTML.
  • accesskey (Optional): Same meaning as in HTML.
  • disabled (Optional): Same meaning as in HTML.
  • maxlength (Optional): Same meaning as in HTML.
  • size (Optional): Same meaning as in HTML.
  • cols (Optional): Same meaning as the identical <textarea> attribute in HTML. Defaults to size if absent.
  • rows (Optional): Same meaning as the identical <textarea> attribute in HTML.
  • wrap (Optional): Same meaning as the identical <textarea> attribute in HTML.

<jhtml:label> Tag

Tag Handler Class: org.chwf.taglib.jhtml.LabelTag
Tag Body Content: empty
Tag Attributes: object, property, for, styleClass, id, lang, style, title, accesskey

This tag prints the label for a property using the property's "label" attribute. It is analogous to the HTML <label> tag, except that it is empty and uses the property label defined in the object's metadata.

Tag Attributes

  • object (Optional): The name of the object. This attribute can normally be omitted, but is useful for printing property labels outside of forms.
  • property (Required): The object property which this label is for.
  • for (Optional): Same meaning as in HTML. Defaults to property if absent.
  • styleClass (Optional): Same meaning as "class" in HTML.
  • id (Optional): Same meaning as in HTML.
  • lang (Optional): Same meaning as in HTML.
  • style (Optional): Same meaning as in HTML.
  • title (Optional): Same meaning as in HTML.
  • accesskey (Optional): Same meaning as in HTML.

<jhtml:field> Tag

Tag Handler Class: org.chwf.taglib.jhtml.FieldTag
Tag Body Content: empty
Tag Attributes: type, object, property, name, validations, options, styleClass, id, lang, style, title, accept, accesskey, disabled, maxlength, size, cols, rows, wrap

This tag is effectively a combination of the <jhtml:label>, <jhtml:input> and <jutil:printError> tags. It prints both a label and input field, as well as errors associated with the field (if any). It combines this output based on the appropriate field pattern: one for errors, and one for normal output.

Tag Attributes

  • type (Optional): The input element type to be created. If omitted, the type is derived from the object property's characteristics.
  • object (Optional): The name of the object. In most cases, this attribute can be omitted, because the form's object becomes the default object.
  • property (Required): The object property that this field will edit. The field is pre-populated with the property's current value.
  • name (Optional): Same meaning as in HTML. Defaults to property name if absent.
  • validations (Optional): Whether JavaScript validations are enabled. Defaults to value of form's validation attribute, or true if not specified anywhere.
  • options (Optional): The name of the options list for a select list. This attribute should only be specified for input elements of type "select". If not specified, the object's options attribute will be used instead. If neither is specified, an error will be generated.
  • styleClass (Optional): Same meaning as "class" in HTML.
  • id (Optional): Same meaning as in HTML.
  • lang (Optional): Same meaning as in HTML.
  • style (Optional): Same meaning as in HTML.
  • title (Optional): Same meaning as in HTML.
  • accept (Optional): Same meaning as in HTML.
  • accesskey (Optional): Same meaning as in HTML.
  • disabled (Optional): Same meaning as in HTML.
  • maxlength (Optional): Same meaning as in HTML.
  • size (Optional): Same meaning as in HTML.
  • cols (Optional): Same meaning as the identical <textarea> attribute in HTML. Defaults to size if absent.
  • rows (Optional): Same meaning as the identical <textarea> attribute in HTML.
  • wrap (Optional): Same meaning as the identical <textarea> attribute in HTML.

<jhtml:fieldPattern> Tag

Tag Handler Class: org.chwf.taglib.jhtml.FieldPatternTag
Tag Body Content: empty
Tag Attributes: normal, error

Specifies the output pattern for <jhtml:field> tags. There are two patterns: one for normal output and one used if there are validation errors associated with the field:

<jhtml:fieldPattern
       normal="<b>{label}</b> {input}"
       error="<font color='red'>{error}</font><br><b>{label}</b> {input}" />

The patterns have a specific set of insertion points:

  • {label}: Insertion point for the field label.
  • {input}: Insertion point for the input element.
  • {error}: Insertion point for the error message.

The specified patterns are used for all field tags that follow the <jhtml:fieldPattern> tag. If this tag is not used to specify the field pattern, the default patterns are used. These patterns insert fields as table rows in a two-column table:

[Normal pattern]
<tr><td valign='top'><b>{label}</b>&nbsp;</td><td>{input}</td></tr>

[Error pattern]
<tr><td colspan='2'><font color='red'>{error}</b></font></td></tr>
<tr><td valign='top'><b>{label}</b>&nbsp;</td><td>{input}</td></tr>

Tag Attributes

  • normal (Required): The normal pattern.
  • error (Optional): The pattern for errors.

<jhtml:link> Tag

Tag Handler Class: org.chwf.taglib.jhtml.LinkTag
Tag Body Content: JSP
Tag Attributes: href, styleClass, id, lang, style, title, accesskey, name, target

A tag to create hyperlinks. This tag is equivalent to the HTML <a> tag, except that it may contain <jutil:param> tags to specify request parameters.

<jhtml:link href="/showItem.jsp">
  <jutil:param object="item" property="itemId"/>
  Show the <jutil:print object="item" property="name"/> item
</jhtml:link>

If the hyperlink is site-relative (beginning with a "/"), the href should not include the context-path. The <jhtml:link> tag will insert the context path automatically. This tag will also perform URL-rewriting for session maintenance.

The above logic will generate the following link, with "{#}" replaced by the item object's item ID, {name} replace with its name and {context} replace with the web application's context path:

<a href="/{context}/showItem.jsp?itemId={#}>Show the {name} item</a>

Tag Attributes

  • href (Required): The hyperlink.
  • styleClass (Optional): Same meaning as "class" in HTML.
  • id (Optional): Same meaning as in HTML.
  • lang (Optional): Same meaning as in HTML.
  • style (Optional): Same meaning as in HTML.
  • title (Optional): Same meaning as in HTML.
  • accesskey (Optional): Same meaning as in HTML.
  • name (Optional): Same meaning as in HTML.
  • target (Optional): Same meaning as in HTML.