Value Change Events

Chapter 7. Event Handling

Topics in This Chapter

  • "Life Cycle Events" on page 268

  • "Value Change Events" on page 269

  • "Action Events" on page 275

  • "Event Listener Tags" on page 285

  • "Immediate Components" on page 287

  • "Passing Data from the UI to the Server" on page 291

  • "Phase Events" on page 295

  • "Putting It All Together" on page 303

Web applications often need to respond to user events, such as selecting items from a menu or clicking a button. For example, you might want to respond to the selection of a country in an address form by changing the locale and reloading the current page to better accommodate your users.

Typically, you register event handlers with components for example, you might register a value change listener with a menu in a JSF page, like this:

  <h:selectOneMenu valueChangeListener="#{form.countryChanged}"...>      ...   </h:selectOneMenu>

In the preceding code, the method binding #{form.countryChanged} references the countryChanged method of a bean named form. That method is invoked by the JSF implementation after the user makes a selection from the menu. Exactly when that method is invoked is one topic of discussion in this chapter.

JSF supports three kinds of events:

  • Value change events

  • Action events

  • Phase events

Value change events are fired by input components such as h:inputText, h:selectOneRadio, and h:selectManyMenu when the component's value changes.

Action events are fired by command components for example, h:commandButton and h:commandLink, when the button or link is activated. Phase events are routinely fired by the JSF life cycle. If you want to handle events, you need to have a basic understanding of that life cycle. Next, we see how the life cycle works.

Life Cycle Events

Requests in JSF applications are processed by the JSF implementation with a controller servlet, which in turn executes the JSF life cycle. The JSF life cycle is shown in Figure 7-1.

Figure 7-1. The JSF life cycle


The JSF life cycle consists of the following phases:

  • Restore View

  • Apply Request Values

  • Process Validations

  • Update Model Values

  • Invoke Application

  • Render Response

Starting with the Apply Request Values phase, the JSF implementation may create events and add them to an event queue during each life cycle phase. After those phases, the JSF implementation broadcasts queued events to registered listeners. Those events and their associated listeners are the focus of this chapter.

Note

Event listeners can affect the JSF life cycle in one of three ways:

  1. Let the life cycle proceed normally.

  2. Call FacesContext.renderResponse() to skip the rest of the life cycle up to Render Response.

  3. Call FacesContext.responseComplete() to skip the rest of the life cycle entirely.

See "Immediate Components" on page 287 for an example of using FacesContext.renderResponse().




Core JavaServerT Faces
Core JavaServer(TM) Faces (2nd Edition)
ISBN: 0131738860
EAN: 2147483647
Year: 2004
Pages: 84

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net