• 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 οnclick="submit()" or οnchange="submit()" to submit the form
- Test carefully on all expected browsers
jsf page:
<h:selectBooleanCheckBox valueChangeListener="#{bean.doSomething}" οnclick="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

本文详细介绍了JSF页面中使用ActionListeners、EventListeners和ValueChangeListeners进行事件处理的方法,包括它们的应用场景、语法实现及注意事项。通过具体示例展示了如何在页面组件触发事件时调用后台Bean的方法,实现业务逻辑的执行。
2779

被折叠的 条评论
为什么被折叠?



