Docstoc

JSFEventModel

Document Sample
JSFEventModel Powered By Docstoc
					Handling Events
1

JSF Event & Listener Model
●

Similar to JavaBeans event model
– –

–

–

Listener and Event classes that an application can use to handle events generated by UI components An Event object identifies the component that generated the event and stores information about the event To be notified of an event, an application must provide an implementation of the Listener class and register it on the component that generates the event When the user activates a component, such as by clicking a button, an event is fired
2

Handling Events: Types of Events
3

Three Types of Events
● ● ●

Action events (ActionEvent) Value change events (ValueChangeEvent) Phase events (PhaseEvent)

4

Action Events
●

Occur when the user activates a component that implements ActionSource
–

ex) buttons and hyperlinks

●

●

●

Represented by the javax.faces.event.ActionEvent class Either an implementation of the javax.faces.event.ActionListener or a method that takes an ActionEvent parameter can handle action events Processed during the “Apply Request Values” phase or the “Invoke Application” phase
5

Value Change Events
●

Result in a change to the local value of a component represented by UIInput or one of its subclasses
–

ex) a value-change event generated by entering a value in a text field

●

●

●

Represented by the javax.faces.event.ValueChangeEvent class Either an implementation of the javax.faces.event.ValueChangeListener or a method that takes a ValueChangeEvent parameter can handle value-change events processed during the “Process Validations” phase
6

Handling Events: Two Different Ways to Handle Events
7

Two Different Ways to Handle Events
●

Option1:
– –

Implement a method in a backing bean to handle the event Page author refers to the method with a JSF EL expression from the appropriate attribute of the component Implement an event listener to handle the event ● Value change event listener ● Action event listener Page author registers a listener on a component by nesting a listener tag inside the UI component tag
8

●

Option2:
–

–

Option1: Write a Backing Bean Method for ActionEvent
9

Step 1: Write a Event handler method in a Backing Bean
●

Component tags reference the methods on a backing bean through special attributes using JSF EL expressions

10

Example: Method that handles ActionEvent in carstore.java (cardemo)
public void chooseLocaleFromLink(ActionEvent event) { String current = event.getComponent().getId(); FacesContext context = FacesContext.getCurrentInstance(); context.getViewRoot(). setLocale((Locale) locales.get(current)); resetMaps(); }

11

Step 2: Refer to the method with a JSF EL Expression
●

Use the tag's actionListener attribute
–

Can only be used with the tags of components that are UICommand components or that implement ActionSource

12

Example: Refer to the ActionEvent Handler Method in chooseLocale.jsp (cardemo)
<h:command_link id="NAmerica" action="storeFront" actionListener="#{carstore.chooseLocaleFromLink}">

13

Option1: Write a Backing Bean Method for ValueChangeEvent
14

Example: Refer to the ValueChangeEvent Handler Method in FirstNameChanged.java
<h:inputText id="firstName" value="#{customer.firstName}" required="true" valueChangeListener="#{carstore.FirstNameChanged}" />

15

Option2: Implementing a Value-Change Listener
16

Step1: Implement ValueChangeListener interface
●

processValueChange(ValueChangeEvent) method is invoked by the JavaServer Faces implementation when a ValueChangeEvent occurs
–

ValueChangeEvent instance stores the old and the new values of the component that fired the event

17

Example: FirstNameChanged listener (cardemo)
public class FirstNameChanged extends Object implements ValueChangeListener { public void processValueChange(ValueChangeEvent event) throws AbortProcessingException { if (null != event.getNewValue()) { FacesContext.getCurrentInstance().getExternalContext() .getSessionMap() .put("firstName", event.getNewValue()); } } public PhaseId getPhaseId() { return PhaseId.ANY_PHASE; } }
18

Step2: Register Value-change listener to a Component
●

Register a ValueChangeListener to a UIInput component
–

By nesting a <f:valueChangeListener> tag within the component's tag on the page

–

“type” attribute specifies the fully-qualified class name of the ValueChangeListener implementation

19

Example: customerInfo.jsp (cardemo)
<h:inputText id="firstName" value="#{customer.firstName}" required="true"> <f:valueChangeListener type="carstore.FirstNameChanged" /> </h:inputText>
●

carstore.FirstNameChanged event listener object is now registered to the inputText UI component – addValueChangeListener() method of the UI component is used for registration

20

What Happens Behind the Scene
●

●

After this component tag is processed and local values have been validated, the component instance represented by this tag will automatically queue the ValueChangeEvent associated with the specified ValueChangeListener to the component The listener processes the event after the phase specified by the getPhaseID method of the listener implementation
21

Option2: Implementing a Action Listener
22

Step1: Implement ActionListener interface
●

processAction(ActionEvent) method is invoked by the JavaServer Faces implementation when an ActionEvent occurs

23

Example: LocaleChange Listener (cardemo)
... public class LocaleChange extends Object implements ActionListener { public void processAction(ActionEvent event) throws AbortProcessingException { String current = event.getComponent().getId(); FacesContext context = FacesContext.getCurrentInstance(); context.getViewRoot().setLocale((Locale) locales.get(current)); resetMaps(); } }

24

Step 2: Register ActionListener to a Component
●

Register an ActionListener on a UICommand component by nesting an <f:actionListener> tag within the component's tag on the page

25

Example: chooseLocale.jsp if it uses <f:actionListener>
<h:command_link id="NAmerica" action="storeFront"> <f:actionListener type="carstore.LocaleChange" /> </h:command_link>

26

What Happens Behind the Scene
●

●

When the component associated with this tag is activated, the component's decode method (or its associated Renderer) automatically queues the ActionEvent associated with the specified ActionListener to the component This listener processes the event after the phase specified by the getPhaseID method of the listener implementation

27

Thank You!
28