Home > Articles > Programming > Java

Standard JSF Tags

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.

InformIT Promotional Mailings & Special Offers

I would like to receive exclusive offers and hear about products from InformIT and its family of brands. I can unsubscribe at any time.

Overview


Pearson Education, Inc., 221 River Street, Hoboken, New Jersey 07030, (Pearson) presents this site to provide information about products and services that can be purchased through this site.

This privacy notice provides an overview of our commitment to privacy and describes how we collect, protect, use and share personal information collected through this site. Please note that other Pearson websites and online products and services have their own separate privacy policies.

Collection and Use of Information


To conduct business and deliver products and services, Pearson collects and uses personal information in several ways in connection with this site, including:

Questions and Inquiries

For inquiries and questions, we collect the inquiry or question, together with name, contact details (email address, phone number and mailing address) and any other additional information voluntarily submitted to us through a Contact Us form or an email. We use this information to address the inquiry and respond to the question.

Online Store

For orders and purchases placed through our online store on this site, we collect order details, name, institution name and address (if applicable), email address, phone number, shipping and billing addresses, credit/debit card information, shipping options and any instructions. We use this information to complete transactions, fulfill orders, communicate with individuals placing orders or visiting the online store, and for related purposes.

Surveys

Pearson may offer opportunities to provide feedback or participate in surveys, including surveys evaluating Pearson products, services or sites. Participation is voluntary. Pearson collects information requested in the survey questions and uses the information to evaluate, support, maintain and improve products, services or sites, develop new products and services, conduct educational research and for other purposes specified in the survey.

Contests and Drawings

Occasionally, we may sponsor a contest or drawing. Participation is optional. Pearson collects name, contact information and other information specified on the entry form for the contest or drawing to conduct the contest or drawing. Pearson may collect additional personal information from the winners of a contest or drawing in order to award the prize and for tax reporting purposes, as required by law.

Newsletters

If you have elected to receive email newsletters or promotional mailings and special offers but want to unsubscribe, simply email information@informit.com.

Service Announcements

On rare occasions it is necessary to send out a strictly service related announcement. For instance, if our service is temporarily suspended for maintenance we might send users an email. Generally, users may not opt-out of these communications, though they can deactivate their account information. However, these communications are not promotional in nature.

Customer Service

We communicate with users on a regular basis to provide requested services and in regard to issues relating to their account we reply via email or phone in accordance with the users' wishes when a user submits their information through our Contact Us form.

Other Collection and Use of Information


Application and System Logs

Pearson automatically collects log data to help ensure the delivery, availability and security of this site. Log data may include technical information about how a user or visitor connected to this site, such as browser type, type of computer/device, operating system, internet service provider and IP address. We use this information for support purposes and to monitor the health of the site, identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents and appropriately scale computing resources.

Web Analytics

Pearson may use third party web trend analytical services, including Google Analytics, to collect visitor information, such as IP addresses, browser types, referring pages, pages visited and time spent on a particular site. While these analytical services collect and report information on an anonymous basis, they may use cookies to gather web trend information. The information gathered may enable Pearson (but not the third party web trend services) to link information with application and system log data. Pearson uses this information for system administration and to identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents, appropriately scale computing resources and otherwise support and deliver this site and its services.

Cookies and Related Technologies

This site uses cookies and similar technologies to personalize content, measure traffic patterns, control security, track use and access of information on this site, and provide interest-based messages and advertising. Users can manage and block the use of cookies through their browser. Disabling or blocking certain cookies may limit the functionality of this site.

Do Not Track

This site currently does not respond to Do Not Track signals.

Security


Pearson uses appropriate physical, administrative and technical security measures to protect personal information from unauthorized access, use and disclosure.

Children


This site is not directed to children under the age of 13.

Marketing


Pearson may send or direct marketing communications to users, provided that

  • Pearson will not use personal information collected or processed as a K-12 school service provider for the purpose of directed or targeted advertising.
  • Such marketing is consistent with applicable law and Pearson's legal obligations.
  • Pearson will not knowingly direct or send marketing communications to an individual who has expressed a preference not to receive marketing.
  • Where required by applicable law, express or implied consent to marketing exists and has not been withdrawn.

Pearson may provide personal information to a third party service provider on a restricted basis to provide marketing solely on behalf of Pearson or an affiliate or customer for whom Pearson is a service provider. Marketing preferences may be changed at any time.

Correcting/Updating Personal Information


If a user's personally identifiable information changes (such as your postal address or email address), we provide a way to correct or update that user's personal data provided to us. This can be done on the Account page. If a user no longer desires our service and desires to delete his or her account, please contact us at customer-service@informit.com and we will process the deletion of a user's account.

Choice/Opt-out


Users can always make an informed choice as to whether they should proceed with certain services offered by InformIT. If you choose to remove yourself from our mailing list(s) simply visit the following page and uncheck any communication you no longer want to receive: www.informit.com/u.aspx.

Sale of Personal Information


Pearson does not rent or sell personal information in exchange for any payment of money.

While Pearson does not sell personal information, as defined in Nevada law, Nevada residents may email a request for no sale of their personal information to NevadaDesignatedRequest@pearson.com.

Supplemental Privacy Statement for California Residents


California residents should read our Supplemental privacy statement for California residents in conjunction with this Privacy Notice. The Supplemental privacy statement for California residents explains Pearson's commitment to comply with California law and applies to personal information of California residents collected in connection with this site and the Services.

Sharing and Disclosure


Pearson may disclose personal information, as follows:

  • As required by law.
  • With the consent of the individual (or their parent, if the individual is a minor)
  • In response to a subpoena, court order or legal process, to the extent permitted or required by law
  • To protect the security and safety of individuals, data, assets and systems, consistent with applicable law
  • In connection the sale, joint venture or other transfer of some or all of its company or assets, subject to the provisions of this Privacy Notice
  • To investigate or address actual or suspected fraud or other illegal activities
  • To exercise its legal rights, including enforcement of the Terms of Use for this site or another contract
  • To affiliated Pearson companies and other companies and organizations who perform work for Pearson and are obligated to protect the privacy of personal information consistent with this Privacy Notice
  • To a school, organization, company or government agency, where Pearson collects or processes the personal information in a school setting or on behalf of such organization, company or government agency.

Links


This web site contains links to other sites. Please be aware that we are not responsible for the privacy practices of such other sites. We encourage our users to be aware when they leave our site and to read the privacy statements of each and every web site that collects Personal Information. This privacy statement applies solely to information collected by this web site.

Requests and Contact


Please contact us about this Privacy Notice or if you have any requests or questions relating to the privacy of your personal information.

Changes to this Privacy Notice


We may revise this Privacy Notice through an updated posting. We will identify the effective date of the revision in the posting. Often, updates are made to provide greater clarity or to comply with changes in regulatory requirements. If the updates involve material changes to the collection, protection, use or disclosure of Personal Information, Pearson will provide notice of the change through a conspicuous notice on this site or other appropriate way. Continued use of the site after the effective date of a posted revision evidences acceptance. Please contact us if you have questions or concerns about the Privacy Notice or any objection to any revisions.

Last Update: November 17, 2020