Professional Documents
Culture Documents
Web pages (Facelets pages, in most cases) represent the presentation layer for web applications. The process of creating web pages for a JavaServer Faces application includes using component tags to add components to the page and wire them to backing beans, validators, listeners, converters, and other server-side objects that are associated with the page. This chapter explains how to create web pages using various types of component and core tags. In the next chapter, you will learn about adding converters, validators, and listeners to component tags to provide additional functionality to components. Many of the examples in this chapter are taken from Chapter 57, "Duke's Bookstore Case Study Example." The following topics are addressed here: Setting Up a Page Adding Components to a Page Using HTML Tag Library Tags Using Core Tags
To use any of the JavaServer Faces tags, you need to include appropriate directives at the top of each page specifying the tag libraries. For Facelets applications, the XML namespace directives uniquely identify the tag library URI and the tag prefix. For example, when you create a Facelets XHTML page, include namespace directives as follows:
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://xmlns.jcp.org/jsf/html" xmlns:f="http://xmlns.jcp.org/jsf/core">
The XML namespace URI identifies the tag library location, and the prefix value is used to distinguish the tags belonging to that specific tag library. You can also use other prefixes instead of the standard h or f. However, when including the tag in the page, you must use the prefix that you have chosen for the tag library. For example, in the following web page, the form tag must be referenced using the h prefix because the preceding tag library directive uses the h prefix to distinguish the tags defined in HTML tag library:
<h:form ...>
The sections Adding Components to a Page Using HTML Tag Library Tags and Using
Core Tags describe how to use the component tags from the JavaServer Faces standard HTML tag library and the core tags from the JavaServer Faces core tag library.
in a page An HTML <input type="hidden"> element No appearance h:inputSecret Allows a user to input a string without the actual string appearing in the field An HTML <input type="password"> element A field, which displays a row of characters instead of the actual string entered h:inputText Allows a user to input a string An HTML <input type="text"> element A field h:inputTextarea Allows a user to enter a multiline string An HTML <textarea> element A multi-row field h:message Displays a localized message An HTML <span> tag if styles are used A text string h:messages Displays localized messages A set of HTML <span> tags if styles are used A text string h:outputFormat Displays a formatted message Plain text Plain text h:outputLabel Displays a nested component as a label for a specified input field An HTML <label> element Plain text h:outputLink Links to another page or location on a page without generating an action event An HTML <a> element A link h:outputText Displays a line of text Plain text Plain text h:panelGrid Displays a table An HTML <table> element with <tr> and <td> elements A table h:panelGroup Groups a set of
components under one parent A HTML <div> or <span> element A row in a table h:selectBooleanCheck box Allows a user to change the value of a Boolean choice An HTML <input type="checkbox"> element. A check box h:selectManyCheckbox Displays a set of check boxes from which the user can select multiple values A set of HTML <input> elements of type checkbox A group of check boxes h:selectManyListbox Allows a user to select multiple items from a set of items, all displayed at once An HTML <select> element A box h:selectManyMenu Allows a user to select multiple items from a set of items An HTML <select> element A menu Table 101 (Cont.) The Component Tags Tag Functions Rendered As Appearance
The tags correspond to components in the javax.faces.component package. The components are discussed in more detail in Chapter 12, "Developing with JavaServer Faces Technology." The next section explains the important attributes that are common to most component tags. For each of the components discussed in the following sections, Writing Bean Properties explains how to write a bean property bound to a particular component or its value. For reference information about the tags and their attributes, see the API documentation for the Facelets tag library at http://docs.oracle.com/javaee/7/javaserverfaces/2.2/vdldocs/face lets/.
items, all displayed at once An HTML <select> element A box h:selectOneMenu Allows a user to select one item from a set of items An HTML <select> element A menu h:selectOneRadio Allows a user to select one item from a set of items An HTML <input type="radio"> element A group of options Table 102 Common Component Tag Attributes Attribute Description binding Identifies a bean property and binds the component instance to it. id Uniquely identifies the component. immediate If set to true, indicates that any events, validation, and conversion associated with the component should happen when request parameter values are applied, rendered Specifies a condition under which the component should be rendered. If the condition is not satisfied, the component is not rendered. style Specifies a Cascading Style Sheet (CSS) style for the tag.