Thursday, November 4, 2010

Thursday, 04 nov

chapter 9 - The JSF Event Model
---------------------------
There are two broad categories of events in JSF, application events and lifecycle events.

James Blunt - Stay the night


if you want to address like this something
#{something} u can put it in the requestMap

requestMap.put("something", message);

ActionSource2 components -> action event -> action listener
ValueHolder components -> value change event -> value change listener

EventObject --> FacesEvent --> ActionEvent or ValueChangeEvent

There are also: phase events and phase listeners

system events (validation about to occur on this component or this component
is about to be rendered)

Application Events - action event, value change
Lifecycle events - phase events, system events

• The Lifecycle instance may have zero or more PhaseListeners attached to it.
• A UIViewRoot instance may have from zero to two PhaseListeners attached to it.
• Every UIComponent instance may have zero or more SystemEventListeners
attached to it.
• Every instance of a component in the javax.faces.component.html package may
have zero or more ClientBehaviors attached to it, and to each of those may be
attached zero or more BehaviorListener interfaces. (The behavior system will be
explained completely in Chapter 12.)
• Every UIInput instance may have zero or more ValueChangeListeners attached to it.
• Every UICommand may have zero or more ActionListeners attached to it.



ActionEvent - At the completion of the Invoke Application
phase unless the immediate flag is true;
then it is processed at the end of the Apply
Request Value phase.

ValueChangeEvent - At the completion of the Process
Validations phase unless the immediate
flag is true; then it is processed at the end
of the Apply Request Value phase.

Pressing A Button
To record the button click event, the Faces lifecycle instantiates an ActionEvent
object and passes it as an argument to the UICommand’s queueEvent( ) method.

For action events, it is also possible to just
write either an action method or an action listener method.

The key point to remember with an action method is
that it relies on the built-in default ActionListener to invoke it and then pass this value to the NavigationHandler in order to determine if a navigation is needed.



public void addConfirmedUserListenerAction(ActionEvent ae) {
// This method would call a database or other service
// and add the confirmed user information.
System.out.println("Adding new user…");
}


For example, you may want to provide a Cancel button that calls a
method before validating the field values. To short-circuit the processing of the action event, one simply sets the UI component’s immediate attribute to true.

Changing The Value In An Input And Submitting
unlike the action event in which the event is processed during the
Invoke Application phase, value change events are processed in the Process Validations phase.

Writing Custom Action and Value Change Listeners


type="com.jsfcompref.MyActionListener">



type="com.jsfcompref.MyValueChangeListener"/>








Avoid having both value-bound and component-bound properties in a single
managed bean class.

By placing immediate="true" on every component that should participate in the partial validation, and by not having this attribute on the rest of the components, validation is bypassed for those components that do not have the attribute.

@ListenerFor(systemEventClass=PreValidateEvent.class)
public class MyInput extends UIInput {
...
public void processEvent(ComponentSystemEvent event)
throws AbortProcessingException {
super.processEvent(event);
// do any pre-validate stuff here
}
}

@ListenersFor({
@ListenerFor(systemEventClass=PostAddToViewEvent.class)
@ListenerFor(systemEventClass=PostConstructViewMapEvent.class)
})
public class MyInput extends UIInput {
...

listener="an EL expression that points to a method that
returns void and takes a ComponentSystemEvent" />

No comments: