JSF: Handling Events: For Live JSF Training, Please See JSP/servlet/Struts/JSF Training Courses at
JSF: Handling Events: For Live JSF Training, Please See JSP/servlet/Struts/JSF Training Courses at
Motivation
• There are two varieties of user interface
events
– Events that start back-end processing
– Events that affect only the format of the user interface
• JSF categorizes code that handles these as
action controllers and event listeners
– Action controllers handle main form submission
• Fire after bean has been populated (see last section)
• Fire after validation logic (see upcoming section)
• Return strings that directly affect page navigation
– Event listeners handle UI events
• Often fire before bean has been populated
• Often bypass validation logic
• Never directly affect page navigation
5 J2EE training and tutorials: http://www.coreservlets.com
Types of Event Listeners
• ActionListener
– Fired by submit buttons, image maps, and hypertext links
with attached JavaScript
• <h:commandButton value="..." .../>
• <h:commandButton image="..." .../>
• <h:commandLink .../>
– Automatically submit the form
• ValueChangeListener
– Fired by combo boxes, checkboxes, radio buttons,
textfields, and others
• <h:selectOneMenu .../>
• <h:selectBooleanCheckbox.../>
• <h:selectOneRadio .../>
• <h:inputText .../>
– Do not automatically submit the form
6 J2EE training and tutorials: http://www.coreservlets.com
Action Listeners
http://courses.coreservlets.com/ -- Hands-on, customized training for Java, servlets, JSP, Struts, and JSF.
Using ActionListener in JSP
• Some buttons submit the form and start
backend processing
– Use <h:commandButton action="..." ...>
• Other buttons affect only the UI
– Use <h:commandButton actionListener="..." .../>
– You usually want this process to occur before beans are
populated and especially before validation occurs
• Since forms are often incomplete when the UI is adjusted
– Use "immediate" to designate that listener fires before
beans are populated or validation is performed
<h:commandButton actionListener="..."
immediate="true" .../>
12
public SelectItem[] getAvailableColors() {...}
J2EE training and tutorials: http://www.coreservlets.com
<h:form>
...
<h:commandButton
value="#{resumeBean.colorSupportLabel}"
actionListener="#{resumeBean.toggleColorSupport}"
immediate="true"/>
...
</h:form>
Value Change
Listeners
http://courses.coreservlets.com/ -- Hands-on, customized training for Java, servlets, JSP, Struts, and JSF.
Using ValueChangeListener in JSP
• ActionListener was attached to button
– Form was automatically submitted when clicked
• ValueChangeListener is attached to
combobox, listbox, radio button, checkbox,
textfield, etc.
– Form not automatically submitted
– Need to add JavaScript to submit the form
onclick="submit()" or onchange="submit()"
– Event incompatibility between Netscape and IE
• Netscape, Mozilla, and Opera fire the onchange events
when the combobox selection changes, radio button is
selected, or checkbox is checked/unchecked
• Internet Explorer fires event after selection changes, but
only when another GUI control receives the input focus
– Older IE versions behave differently! Test on multiple browsers!
30 J2EE training and tutorials: http://www.coreservlets.com
Implementing
ValueChangeListener in Java Code
• Listener is usually in the form bean class
– As discussed previously
• Takes a ValueChangeEvent as an argument
– Useful ValueChangeEvent methods
• getComponent (as mentioned for ActionEvent)
• getOldValue (previous value of GUI element)
• getNewValue (current value of GUI element)
– Needed since bean has probably not been populated
– Value for checkbox is of type Boolean
– Value for radio button or textfield corresponds to request parameter
– Sample code
public void someMethod(ValueChangeEvent event) {
boolean flag =
((Boolean)event.getNewValue()).booleanValue();
takeActionBasedOn(flag);
}
31 J2EE training and tutorials: http://www.coreservlets.com
Example: Changing the Colors
Listed for FG and BG Colors
• h:selectOneMenu uses f:selectItems to get
list of combobox entries
• Use checkbox to invoke
ValueChangeListener
• Have listener toggle the definition of the
lists
– Color names
– RGB values
Steps 5-7
• Unchanged from previous slides
Combining Action
Listeners and Action
Controllers for Same
Button
http://courses.coreservlets.com/ -- Hands-on, customized training for Java, servlets, JSP, Struts, and JSF.
Motivation
• Usually, action listeners and action
controllers are attached to different buttons
• Sometimes, you are forced to use both
– Action listeners have access to low-level details of GUI
object
• Renderer, client ID, etc.
• Most common example:
server-side image maps
– h:commandButton with image instead of value results in
image map
– Actual incoming request parameter names are clientID.x
and clientID.y
• Only listener can discover the client ID
...
• Code
<h:commandButton
image="images/GrayBar.gif"
actionListener="#{colorBean.selectGrayLevel}"
action="#{colorBean.showPreview}"/>
Questions?