JSF Action/Event/ValueChange Listeners

11 篇文章 0 订阅

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值