10.12.2012 Views

The Java EE 5 Tutorial (PDF) - Oracle Software Downloads

The Java EE 5 Tutorial (PDF) - Oracle Software Downloads

The Java EE 5 Tutorial (PDF) - Oracle Software Downloads

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Writing Backing Bean Methods<br />

404<br />

When an action method returns an outcome, it uses the dot notation to reference the outcome<br />

from the Enum class:<br />

public Object submit(){<br />

...<br />

return Navigation.accountHist;<br />

}<br />

“Referencing a Method That Performs Navigation” on page 369 explains how a component tag<br />

references this method. “Binding a Component Instance to a Bean Property” on page 366<br />

discusses how the page author can bind these components to bean properties. “Writing<br />

Properties Bound to Component Instances” on page 386 discusses how to write the bean<br />

properties to which the components are bound. “Configuring Navigation Rules” on page 449<br />

provides more information on configuring navigation rules.<br />

Writing a Method to Handle an Action Event<br />

A backing bean method that handles an action event must be a public method that accepts an<br />

action event and returns void. This method is referenced using the component tag’s<br />

actionListener attribute. Only components that implement ActionSource can refer to this<br />

method.<br />

<strong>The</strong> following backing bean method from LocaleBean of the Duke’s Bookstore application<br />

processes the event of a user clicking one of the hyperlinks on the chooselocale.jsp page:<br />

public void chooseLocaleFromLink(ActionEvent event) {<br />

String current = event.getComponent().getId();<br />

FacesContext context = FacesContext.getCurrentInstance();<br />

context.getViewRoot().setLocale((Locale)<br />

locales.get(current));<br />

}<br />

This method gets the component that generated the event from the event object. <strong>The</strong>n it gets the<br />

component’s ID. <strong>The</strong> ID indicates a region of the world. <strong>The</strong> method matches the ID against a<br />

HashMap object that contains the locales available for the application. Finally, it sets the locale<br />

using the selected value from the HashMap object.<br />

“Referencing a Method That Handles an Action Event” on page 369 explains how a component<br />

tag references this method.<br />

Writing a Method to PerformValidation<br />

Rather than implement the Validator interface to perform validation for a component, you<br />

can include a method in a backing bean to take care of validating input for the component.<br />

<strong>The</strong> <strong>Java</strong> <strong>EE</strong> 5<strong>Tutorial</strong> • June 2010

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!