• Action listeners
– Attached to buttons, hypertext links, or image maps
– Automatically submit the form
- when to use: business method/handler
jsf page:
<h:commandButton action="#{bean.doSomething}"/>
jsf2 backing bean:
public String doSomething() {
...
return "somePage.xhtml";
}
• Event listeners
- are used to handle events that affect only the user interface
- Typically fire before beans are populated and validation is performed
- Use immediate="true" to designate this behavior
- Form is redisplayed after listeners fire
- No navigation rules apply
- All exceptions/errors swallowed
- When to use: Use actionListener
if you want have a hook before the real business action get executed, e.g. to log it, and/or to set an additional property (by <f:setPropertyActionListener>
), and/or to have access to the component which invoked the action (which is available by the ActionEvent
argument).
jsf page:
<h:commandButton actionListener="#{bean.doSomething}" immediate="true"/>
jsf2 backing bean: takes an ActionEvent parameter and return void
public void doSomething(ActionEvent event) {
...
}
Another example from StackOverflow BalusC:
<h:commandLink value="submit" actionListener="#{bean.listener1}" action="#{bean.submit}"> <f:actionListener type="com.example.SomeActionListener" /> <f:setPropertyActionListener target="#{bean.property}" value="some" /> </h:commandLink>
The above example would invoke in this order:
Bean#listener1()
, SomeActionListener#processAction()
, Bean#setProperty()
and Bean#submit()
• Value change listeners
- Attached to radio buttons, comboboxes, list boxes, checkboxes, textfields, etc.
- Require onclick="submit()" or onchange="submit()" to submit the form
- Test carefully on all expected browsers
jsf page:
<h:selectBooleanCheckBox valueChangeListener="#{bean.doSomething}" onclick="submit()"/>
jsf2 backing bean:
public void doSomething(ValueChangeEvent event) {
Boolean flag = (Boolean) event.getNewValue();
...
}
Reference: http://courses.coreservlets.com/Course-Materials/pdf/jsf/jsf2/JSF2-Event-Handling.pdf