Home > Articles > Programming > Java

  • Print
  • + Share This
This chapter is from the book

This chapter is from the book

An Overview of the JSF HTML Tags

Table 4–4 lists all HTML tags. We can group these tags in the following categories:

  • Inputs (input...)
  • Outputs (output..., graphicImage)
  • Commands (commandButton and commandLink)
  • GET Requests (button, link, outputLink)
  • Selections (checkbox, listbox, menu, radio)
  • HTML pages (head, body, form, outputStylesheet, outputScript)
  • Layouts (panelGrid, panelGroup)
  • Data table (dataTable and column); see Chapter 6
  • Errors and messages (message, messages)

The JSF HTML tags share common attributes, HTML pass-through attributes, and attributes that support dynamic HTML.

Table 4–4. JSF HTML Tags

Tag

Description

head jsf-2-0.jpg

Renders the head of the page

body jsf-2-0.jpg

Renders the body of the page

form

Renders a HTML form

outputStylesheet jsf-2-0.jpg

Adds a stylesheet to the page

outputScript jsf-2-0.jpg

Adds a script to the page

inputText

Single-line text input control

inputTextarea

Multiline text input control

inputSecret

Password input control

inputHidden

Hidden field

outputLabel

Label for another component for accessibility

outputLink

Link to another web site

outputFormat

Like outputText, but formats compound messages

outputText

Single-line text output

commandButton

Button: submit, reset, or pushbutton

commandLink

Link that acts like a pushbutton

button jsf-2-0.jpg

Button for issuing a GET request

link jsf-2-0.jpg

Link for issuing a GET request

message

Displays the most recent message for a component

messages

Displays all messages

graphicImage

Displays an image

selectOneListbox

Single-select listbox

selectOneMenu

Single-select menu

selectOneRadio

Set of radio buttons

selectBooleanCheckbox

Checkbox

selectManyCheckbox

Set of checkboxes

selectManyListbox

Multiselect listbox

selectManyMenu

Multiselect menu

panelGrid

Tabular layout

panelGroup

Two or more components that are laid out as one

dataTable

A feature-rich table control (see Chapter 6)

column

Column in a dataTable (see Chapter 6)

Common Attributes

Three types of tag attributes are shared among multiple HTML component tags:

  • Basic
  • HTML 4.0
  • DHTML events

Next, we look at each type.

Basic Attributes

As you can see from Table 4–5, basic attributes are shared by the majority of JSF HTML tags.

Table 4–5. Basic HTML Tag Attributesa

Attribute

Component Types

Description

id

A (31)

Identifier for a component

binding

A (31)

Links this component with a backing bean property

rendered

A (31)

A Boolean; false suppresses rendering

value

I, O, C (21)

A component's value, typically a value expression

valueChangeListener

I (11)

A method expression to a method that responds to value changes

converter

I, O (15)

Converter class name

validator

I (11)

Class name of a validator that is created and attached to a component

required

I (11)

A Boolean; if true, requires a value to be entered in the associated field

converterMessage, validatorMessage, requiredMessage jsf-1-2.jpg

I (11)

A custom message to be displayed when a conversion or validation error occurs, or when required input is missing

All components can have id, binding, and rendered attributes, which we discuss in the following sections.

The value and converter attributes let you specify a component value and a means to convert it from a string to an object, or vice versa.

The validator, required, and valueChangeListener attributes are available for input components so that you can validate values and react to changes to those values. See Chapter 7 for more information about validators and converters.

IDs and Bindings

The versatile id attribute lets you do the following:

  • Access JSF components from other JSF tags
  • Obtain component references in Java code
  • Access HTML elements with scripts

In this section, we discuss the first two tasks listed above. See "Form Elements and JavaScript" on page 120 for more about the last task.

The id attribute lets page authors reference a component from another tag. For example, an error message for a component can be displayed like this:

<h:inputText id="name" .../>
<h:message for="name"/>

You can also use component identifiers to get a component reference in your Java code. For example, you could access the name component in a listener like this:

UIComponent component = event.getComponent().findComponent("name");

The preceding call to findComponent has a caveat: The component that generated the event and the name component must be in the same form. There is another way to access a component in your Java code. Define the component as an instance field of a class. Provide property getters and setters for the component. Then use the binding attribute, which you specify in a JSF page, like this:

<h:inputText binding="#{form.nameField}" .../>

The binding attribute is specified with a value expression. That expression refers to a read-write bean property, such as this one:

private UIComponent nameField = new UIInput();
public UIComponent getNameField() { return nameField; }
public void setNameField(UIComponent newValue) { nameField = newValue; }

See "Backing Beans" on page 38 of Chapter 2 for more information about the binding attribute. The JSF implementation sets the property to the component, so you can programatically manipulate components.

Values, Converters, and Validators

Inputs, outputs, commands, and data tables all have values. Associated tags in the HTML library, such as h:inputText and h:dataTable, come with a value attribute. You can specify values with a string, like this:

<h:commandButton value="Logout" .../>

Most of the time you will use a value expression—for example:

<h:inputText value="#{customer.name}"/>

The converter attribute, shared by inputs and outputs, lets you attach a converter to a component. Input tags also have a validator attribute that you can use to attach a validator to a component. Converters and validators are discussed at length in Chapter 7.

Conditional Rendering

You use the rendered attribute to include or exclude a component, depending on a condition. For example, you may want to render a "Logout" button only if the user is currently logged in:

<h:commandButton ... rendered="#{user.loggedIn}"/>

To conditionally include a group of components, include them in an h:panelGrid with a rendered attribute. See "Panels" on page 115 for more information.

HTML 4.0 Attributes

JSF HTML tags have appropriate HTML 4.0 pass-through attributes. Those attribute values are passed through to the generated HTML element. For example, <h:inputText value="#{form.name.last}" size="25".../> generates this HTML: <input type="text" size="25".../>. Notice that the size attribute is passed through to HTML.

The HTML 4.0 attributes are listed in Table 4–6.

Table 4–6. HTML 4.0 Pass-Through Attributesa

Attribute

Description

accesskey (16)

A key, typically combined with a system-defined metakey, that gives focus to an element.

accept (1)

Comma-separated list of content types for a form.

acceptcharset (1)

Comma- or space-separated list of character encodings for a form. The HTML accept-charset attribute is specified with the JSF attribute named acceptcharset.

alt (5)

Alternative text for nontextual elements such as images or applets.

border (4)

Pixel value for an element's border width.

charset (3)

Character encoding for a linked resource.

coords (3)

Coordinates for an element whose shape is a rectangle, circle, or polygon.

dir (26)

Direction for text. Valid values are "ltr" (left to right) and "rtl" (right to left).

disabled (14)

Disabled state of an input element or button.

hreflang (3)

Base language of a resource specified with the href attribute; hreflang may only be used with href.

lang (26)

Base language of an element's attributes and text.

maxlength (2)

Maximum number of characters for text fields.

readonly (11)

Read-only state of an input field; text can be selected in a read-only field but not edited.

rel (3)

Relationship between the current document and a link specified with the href attribute.

rev (3)

Reverse link from the anchor specified with href to the current document. The value of the attribute is a space-separated list of link types.

rows (1)

Number of visible rows in a text area. h:dataTable has a rows attribute, but it is not an HTML pass-through attribute.

shape (3)

Shape of a region. Valid values: default, rect, circle, poly (default signifies the entire region).

size (4)

Size of an input field.

style (26)

Inline style information.

styleClass (26)

Style class; rendered as HTML class attribute.

tabindex (16)

Numerical value specifying a tab index.

target (5)

The name of a frame in which a document is opened.

title (25)

A title, used for accessibility, that describes an element. Visual browsers typically create tooltips for the title's value.

type (4)

Type of a link—for example, "stylesheet".

width (3)

Width of an element.

The attributes listed in Table 4–6 are defined in the HTML specification, which you can access online at http://www.w3.org/TR/REC-html40. That web site is an excellent resource for deep digging into HTML.

Styles

You can use CSS styles, either inline (style) or classes (styleClass), to influence how components are rendered:

<h:outputText value="#{customer.name}" styleClass="emphasis"/>
<h:outputText value="#{customer.id}" style="border: thin solid blue"/>

CSS style attributes can be value expressions—that gives you programmatic control over styles.

Resources jsf-2-0.jpg

You can include a stylesheet in the usual way, with an HTML link tag. But that is tedious if your pages are at varying directory nesting levels—you would always need to update the stylesheet directory when you move a page. More importantly, if you assemble pages from different pieces—as described in Chapter 5—you don't even know where your pieces end up.

Since JSF 2.0, there is a better way. You can place stylesheets, JavaScript files, images, and other files into a resources directory in the root of your web application. Subdirectories of this directory are called libraries. You can create any libraries that you like. In this book, we often use libraries css, images, and javascript.

To include a stylesheet, use the tag:

<h:outputStylesheet library="css" name="styles.css"/>

The tag adds a link of the form

<link href="/context-root/faces/javax.faces.resource/styles.css?ln=css"
   rel="stylesheet" type="text/css"/>

to the header of the page.

To include a script resource, use the outputScript tag instead:

<h:outputScript name="jsf.js" library="javascript" target="head" />

If the target attribute is head or body, the script is appended to the "head" or "body" facet of the root component, which means that it appears at the end of the head or body in the generated HTML. If there is no target element, the script is inserted in the current location.

To include an image from a library, you use the graphicImage tag:

<h:graphicImage name="logo.png" library="images"/>

There is a versioning mechanism for resource libraries and individual resources. You can add subdirectories to the library directory and place newer versions of files into them. The subdirectory names are simply the version numbers. For example, suppose you have the following directories:

resources/css/1_0_2
resources/css/1_1

Then the latest version (resources/css/1_1) will be used. Note that you can add new versions of a library in a running application.

Similarly, you can add new versions of an individual resource, but the naming scheme is a bit odd. You replace the resource with a directory of the same name, then use the version name as the file name. You can add an extension if you like. For example:

resources/css/styles.css/1_0_2.css
resources/css/styles.css/1_1.css

The version numbers must consist of decimal numbers, separated by underscores. They are compared in the usual way, first comparing the major version numbers and using the minor numbers to break ties.

There is also a mechanism for supplying localized versions of resources. Unfortunately, that mechanism is unintuitive and not very useful. Localized resources have a prefix, such as resources/de_DE/images, but the prefix is not treated in the same way as a bundle suffix. There is no fallback mechanism. That is, if an image is not found in resources/de_DE/images, then resources/de/images and resources/images are not consulted.

Moreover, the locale prefix is not simply the current locale. Instead, it is obtained by a curious lookup, which you enable by following these steps:

  1. Add the line
    <message-bundle>name of a resource bundle used in your application</message-bundle>
    inside the application element of faces-config.xml
  2. Inside each localized version of that resource bundle, place a name/value pair
    javax.faces.resource.localePrefix=prefix
          
  3. Place the matching resources into resources/ prefix/library/...

For example, if you use the message bundle com.corejsf.messages, and the file com.corejsf.messages_de contains the entry

javax.faces.resource.localePrefix=german

then you place the German resources into resources/german. (The prefix need not use the standard language and country codes, and in fact it is a good idea not to use them so that you don't raise false hopes.)

DHTML Events

Client-side scripting is useful for all sorts of tasks, such as syntax validation or rollover images, and it is easy to use with JSF. HTML attributes that support scripting, such as onclick and onchange are called dynamic HTML (DHTML) event attributes. JSF supports DHTML event attributes for nearly all of the JSF HTML tags. Those attributes are listed in Table 4–7.

Table 4–7. DHTML Event Attributesa

Attribute

Description

onblur (16)

Element loses focus

onchange (11)

Element's value changes

onclick (17)

Mouse button is clicked over the element

ondblclick (21)

Mouse button is double-clicked over the element

onfocus (16)

Element receives focus

onkeydown (21)

Key is pressed

onkeypress (21)

Key is pressed and subsequently released

onkeyup (21)

Key is released

onload (1)

Page is loaded

onmousedown (21)

Mouse button is pressed over the element

onmousemove (21)

Mouse moves over the element

onmouseout (21)

Mouse leaves the element's area

onmouseover (21)

Mouse moves onto an element

onmouseup (21)

Mouse button is released

onreset (1)

Form is reset

onselect (11)

Text is selected in an input field

onsubmit (1)

Form is submitted

onunload (1)

Page is unloaded

The DHTML event attributes listed in Table 4–7 let you associate client-side scripts with events. Typically, JavaScript is used as a scripting language, but you can use any scripting language you like. See the HTML specification for more details.

  • + Share This
  • 🔖 Save To Your Account