background image

Action Event Handler

<< The NavigationHandler | Value-Change Event Handler >>
<< The NavigationHandler | Value-Change Event Handler >>

Action Event Handler

When an action method returns an outcome, it uses the dot notation to reference the outcome
from the Enum class:
public Object submit(){
...
return Navigation.accountHist;
}
"Referencing a Method That Performs Navigation" on page 371
explains how a component tag
references this method.
"Binding a Component Instance to a Bean Property" on page 368
discusses how the page author can bind these components to bean properties.
"Writing
Properties Bound to Component Instances" on page 388
discusses how to write the bean
properties to which the components are bound.
"Configuring Navigation Rules" on page 451
provides more information on configuring navigation rules.
Writing a Method to Handle an Action Event
A backing bean method that handles an action event must be a public method that accepts an
action event and returns void. This method is referenced using the component tag's
actionListener
attribute. Only components that implement ActionSource can refer to this
method.
The following backing bean method from LocaleBean of the Duke's Bookstore application
processes the event of a user clicking one of the hyperlinks on the chooselocale.jsp page:
public void chooseLocaleFromLink(ActionEvent event) {
String current = event.getComponent().getId();
FacesContext context = FacesContext.getCurrentInstance();
context.getViewRoot().setLocale((Locale)
locales.get(current));
}
This method gets the component that generated the event from the event object. Then it gets the
component's ID. The ID indicates a region of the world. The method matches the ID against a
HashMap
object that contains the locales available for the application. Finally, it sets the locale
using the selected value from the HashMap object.
"Referencing a Method That Handles an Action Event" on page 371
explains how a component
tag references this method.
Writing a Method to Perform Validation
Rather than implement the Validator interface to perform validation for a component, you
can include a method in a backing bean to take care of validating input for the component.
Writing Backing Bean Methods
The Java EE 5 Tutorial · September 2007
406