|
Download
FAQ History |
|
API
Search Feedback |
Referencing a Backing Bean Method
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 18-10.
Only components that implement
ActionSourcecan use theactionandactionListenerattributes. OnlyUIInputcomponents or components that extendUIInputcan use thevalidatororvalueChangeListenerattribute.The component tag refers to backing bean methods using a method-binding expression with the attributes. The following four sections give examples of how to use the four different attributes.
Referencing a Method That Performs Navigation
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
actionattribute. This attribute does one of the followingThe
bookcashier.jsppage of the Duke's Bookstore application has acommandButtontag 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 therenderedproperties of some of the components totrueand returnsnull; otherwise it returnsreceipt, which causes thebookreceipt.jsppage to display. Here is thecommandButtontag from thebookcasheir.jsppage:The
actionattribute uses a method-binding expression to refer to thesubmitmethod ofCashierBean. 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
submitmethod of theCashierBean.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.
Referencing a Method That Handles an ActionEvent
If a component on your page generates an
ActionEvent, and if that event is handled by a backing bean method, you must refer to the method by using the component'sactionListenerattribute.The
chooselocale.jsppage of the Duke's Bookstore application includes some components that are handled by action events. One of them is theNAmericacomponent:<h:commandLink id="NAmerica" action="bookstore" actionListener="#{localeBean.chooseLocaleFromLink}">This
actionListenerattribute of this component tag references thechooseLocaleFromLinkmethod using a method-binding expression. ThechooseLocaleFromLinkmethod handles the event of a user clicking on the hyperlink rendered by this component.The
actionListenerattribute can be used only with the tags of components that implementActionSource. These includeUICommandcomponents.Writing a Method to Handle an ActionEvent describes how to implement a method that handles an action event.
Referencing a Method That Performs Validation
If the input of one of the components on your page is validated by a backing bean method, you need to refer to the method from the component's tag using the
validatorattribute.The Coffee Break application includes a method that performs validation of the
checkoutForm.jsppage. 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
validatemethod described in Writing a Method to Perform Validation using a method-binding expression.The
validatorattribute can be used only withUIInputcomponents or those components whose classes extendUIInput.Writing a Method to Perform Validation describes how to implement a method that performs validation.
Referencing a Method That Handles a ValueChangeEvent
If you want a component on your page to generate a
ValueChangeEventand you want that event to be handled by a backing bean method, you refer to the method using the component'svalueChangeListenerattribute.The
namecomponent on thebookcashier.jsppage of the Duke's Bookstore application references aValueChangeListenerthat handles the event of a user entering a name in thenameinput 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
valueChangeListenerattribute:<h:inputText id="name" size="50" value="#{cashier.name}" required="true" valueChangeListener="cashier.processValueChangeEvent" /> </h:inputText>The
valueChangeListenerattribute of this component tag references theprocessValueChangeEventmethod of theCashierBeanusing a method-binding expression. TheprocessValueChangeEventmethod handles the event of a user entering his name in the input field rendered by this component.The
valueChangeListenerattribute can be used only with the tags ofUIInputcomponents and components whose classes extendUIInput.Writing a Method to Handle a Value-Change Event describes how to implement a method that handles a
ValueChangeEvent.
|
Download
FAQ History |
|
API
Search Feedback |
All of the material in The J2EE(TM) 1.4 Tutorial is copyright-protected and may not be published in other works without express written permission from Sun Microsystems.