2. Using the Tutorial Examples
3. Getting Started with Web Applications
5. JavaServer Pages Technology
7. JavaServer Pages Standard Tag Library
10. JavaServer Faces Technology
11. Using JavaServer Faces Technology in JSP Pages
The Example JavaServer Faces Application
Adding UI Components to a Page Using the HTML Component Tags
The style and styleClass Attributes
The value and binding Attributes
Rendering a Text Field with the inputText Tag
Rendering a Label with the outputLabel Tag
Rendering a Hyperlink with the outputLink Tag
Displaying a Formatted Message with the outputFormat Tag
Rendering a Password Field with the inputSecret Tag
Using Command Components for Performing Actions and Navigation
Rendering a Button with the commandButton Tag
Rendering a Hyperlink with the commandLink Tag
Using Data-Bound Table Components
Adding Graphics and Images with the graphicImage Tag
Laying Out Components with the UIPanel Component
Rendering Components for Selecting One Value
Displaying a Check Box Using the selectBooleanCheckbox Tag
Displaying a Menu Using the selectOneMenu Tag
Rendering Components for Selecting Multiple Values
The UISelectItem, UISelectItems, and UISelectItemGroup Components
Displaying Error Messages with the message and messages Tags
Referencing Localized Static Data
Converting a Component's Value
Registering Listeners on Components
Registering a Value-Change Listener on a Component
Registering an Action Listener on a Component
Validating a Component's Value
Binding Component Values and Instances to External Data Sources
Binding a Component Value to a Property
Binding a Component Value to an Implicit Object
Binding a Component Instance to a Bean Property
Binding Converters, Listeners, and Validators to Backing Bean Properties
Referencing a Backing Bean Method
Referencing a Method That Performs Navigation
Referencing a Method That Handles an Action Event
12. Developing with JavaServer Faces Technology
13. Creating Custom UI Components
14. Configuring JavaServer Faces Applications
15. Internationalizing and Localizing Web Applications
16. Building Web Services with JAX-WS
17. Binding between XML Schema and Java Classes
19. SOAP with Attachments API for Java
21. Getting Started with Enterprise Beans
23. A Message-Driven Bean Example
24. Introduction to the Java Persistence API
25. Persistence in the Web Tier
26. Persistence in the EJB Tier
27. The Java Persistence Query Language
28. Introduction to Security in the Java EE Platform
29. Securing Java EE Applications
31. The Java Message Service API
32. Java EE Examples Using the JMS API
36. The Coffee Break Application
37. The Duke's Bank Application
A component tag has a set of attributes for referencing backing bean methods that can perform certain functions for the component associated with the tag. These attributes are summarized in Table 11-10.
Table 11-10 Component Tag Attributes That Reference Backing Bean Methods
Attribute |
Function |
---|---|
action |
Refers to a backing bean method that performs navigation processing for the component and returns a logical outcome String |
actionListener |
Refers to a backing bean method that handles action events |
validator |
Refers to a backing bean method that performs validation on the component’s value |
valueChangeListener |
Refers to a backing bean method that handles value-change events |
Only components that implement ActionSource can use the action and actionListener attributes. Only components that implement EditableValueHolder can use the validator or valueChangeListener attributes.
The component tag refers to a backing bean method using a method expression as a value of one of the attributes. The method referenced by an attribute must follow a particular signature, which is defined by the tag attribute’s definition in the TLD. For example, the definition of the validator attribute of the inputText tag in html_basic.tld is the following:
void validate(javax.faces.context.FacesContext, javax.faces.component.UIComponent, java.lang.Object)
The following four sections give examples of how to use the four different attributes.
If your page includes a component (such as a button or hyperlink) that causes the application to navigate to another page when the component is activated, the tag corresponding to this component must include an action attribute. This attribute does one of the following
Specifies a logical outcome String that tells the application which page to access next
References a backing bean method that performs some processing and returns a logical outcome String
The bookcashier.jsp page of the Duke’s Bookstore application has a commandButton tag that refers to a backing bean method that calculates the shipping date. If the customer has ordered more than 100ドル (or 100 euros) worth of books, this method also sets the rendered properties of some of the components to true and returns null; otherwise it returns receipt, which causes the bookreceipt.jsp page to display. Here is the commandButton tag from the bookcashier.jsp page:
<h:commandButton value="#{bundle.Submit}" action="#{cashier.submit}" />
The action attribute uses a method expression to refer to the submit method of CashierBean. This method will process the event fired by the component corresponding to this tag.
Writing a Method to Handle Navigation describes how to implement the submit method of CashierBean.
The application architect must configure a navigation rule that determines which page to access given the current page and the logical outcome, which is either returned from the backing bean method or specified in the tag. See Configuring Navigation Rules for information on how to define navigation rules in the application configuration resource file.
If a component on your page generates an action event, and if that event is handled by a backing bean method, you refer to the method by using the component’s actionListener attribute.
The chooselocale.jsp page of the Duke’s Bookstore application includes some components that generate action events. One of them is the NAmerica component:
<h:commandLink id="NAmerica" action="bookstore" actionListener="#{localeBean.chooseLocaleFromLink}">
The actionListener attribute of this component tag references the chooseLocaleFromLink method using a method expression. The chooseLocaleFromLink method handles the event of a user clicking on the hyperlink rendered by this component.
Writing a Method to Handle an Action Event describes how to implement a method that handles an action event.
If the input of one of the components on your page is validated by a backing bean method, you refer to the method from the component’s tag using the validator attribute.
The Coffee Break application includes a method that performs validation of the email input component on the checkoutForm.jsp page. Here is the tag corresponding to this component:
<h:inputText id="email" value="#{checkoutFormBean.email}" size="25" maxlength="125" validator="#{checkoutFormBean.validateEmail}"/>
This tag references the validate method described in Writing a Method to Perform Validation using a method expression.
If you want a component on your page to generate a value-change event and you want that event to be handled by a backing bean method, you refer to the method using the component’s valueChangeListener attribute.
The name component on the bookcashier.jsp page of the Duke’s Bookstore application references a ValueChangeListener implementation that handles the event of a user entering a name in the name input field:
<h:inputText id="name" size="50" value="#{cashier.name}" required="true"> <f:valueChangeListener type="listeners.NameChanged" /> </h:inputText>
For illustration, Writing a Method to Handle a Value-Change Event describes how to implement this listener with a backing bean method instead of a listener implementation class. To refer to this backing bean method, the tag uses the valueChangeListener attribute:
<h:inputText id="name" size="50" value="#{cashier.name}" required="true" valueChangeListener="#{cashier.processValueChange}" /> </h:inputText>
The valueChangeListener attribute of this component tag references the processValueChange method of CashierBean using a method expression. The processValueChange method handles the event of a user entering his name in the input field rendered by this component.
Writing a Method to Handle a Value-Change Event describes how to implement a method that handles a ValueChangeEvent.
Copyright © 2010, Oracle and/or its affiliates. All rights reserved. Legal Notices
Scripting on this page tracks web page traffic, but does not change the content in any way.