问题描述
使用PF 3.0-RC1快照(11/22/2011)
Using PF 3.0-RC1 Snapshot (11/22/2011)
我在复合组件中有一个.我想在做出选择时调用valueChangeListener,但它似乎没有在调用侦听器.
I have a in a composite component.I want to call the valueChangeListener when a selection is made, but it does not appear to be calling the listener.
以下是该组件的代码:
<p:selectOneMenu style="width: 220px;"
value="#{customerProfileSessionBean.selectedAccount}"
valueChangeListener="#{customerProfileSessionBean.accountValueChange}" >
<f:selectItems value="#{sessionBean1.custAccountList}"/>
</p:selectOneMenu>
支持Bean中的侦听器具有未被调用的打印语句(至少我在服务器日志中看不到它).
The listener in the backing bean has a print statement that is not being called (at least I do not see it in the server log).
在值更改时,还需要执行其他操作来使valueChangeListener调用吗?我需要使用吗?
Is there something else that I need to do to get the valueChangeListener to be called when the value changes?Do I need to use ?
此外,在侦听器中,是否传递了ValueChangeEvent?
Also, in the listener, is there a ValueChangeEvent that is passed?
谢谢.
推荐答案
您似乎期望在客户端发生更改事件时立即调用服务器端的valueChangeListener
方法.这是不正确的.仅当将表单提交到服务器并且新值不equals()
旧值
You seem to be expecting that the valueChangeListener
method in the server side is called immediately when a change event occurs on the client side. This is not correct. It will only be invoked when the form is submitted to the server and the new value does not equals()
the old value.
至少有两种方法可以满足您的功能要求:
There are at least two ways to achieve your functional requirement:
-
添加
onchange="submit()"
,以便每当您更改值时JavaScript都将提交表单:
Add
onchange="submit()"
so that JavaScript will submit the form whenever you change the value:
<p:selectOneMenu style="width: 220px;"
value="#{customerProfileSessionBean.selectedAccount}"
valueChangeListener="#{customerProfileSessionBean.accountValueChange}"
onchange="submit()">
<f:selectItems value="#{sessionBean1.custAccountList}"/>
</p:selectOneMenu>
然而,这非常类似于JSF-1.x,用户体验很差.它还会提交(并转换/验证!)所有其他您可能不需要的输入字段.
This is however very JSF-1.x-ish and poor for user experience. It will also submit (and convert/validate!) all other input fields which may not be what you want.
请改用Ajax侦听器,以确保您对实际值更改不感兴趣(即,旧值对您而言不感兴趣),但实际上您对更改事件本身感兴趣.您可以使用<f:ajax>
或在PrimeFaces组件中使用<p:ajax>
:
Make use of an ajax listener instead, for sure if you are not interested in the actual value change (i.e. the old value is not interesting for you), but you're actually interested in the change event itself. You can do this using <f:ajax>
or in PrimeFaces components using <p:ajax>
:
<p:selectOneMenu style="width: 220px;"
value="#{customerProfileSessionBean.selectedAccount}">
<p:ajax listener="#{customerProfileSessionBean.accountValueChange}" />
<f:selectItems value="#{sessionBean1.custAccountList}"/>
</p:selectOneMenu>
并将ValueChangeEvent
参数替换为AjaxBehaviorEvent
参数.
这篇关于我如何获得PrimeFaces< p:selectOneMenu>打电话给valueChangeListener?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!