我如何获得 PrimeFaces <p:selectOneMenu>调用 valueChangeListener? [英] How do I get PrimeFaces <p:selectOneMenu> to call valueChangeListener?

查看:16
本文介绍了我如何获得 PrimeFaces <p:selectOneMenu>调用 valueChangeListener?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 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>

backing 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:

  1. 添加 onchange="submit()" 以便 JavaScript 会在您更改值时提交表单:

  1. 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-ish 和糟糕的用户体验.它还将提交(并转换/验证!)所有其他可能不是您想要的输入字段.

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 侦听器,但您实际上对更改事件本身感兴趣.您可以使用 或在 PrimeFaces 组件中使用 :

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 参数.

And replace the ValueChangeEvent argument by the AjaxBehaviorEvent argument.

这篇关于我如何获得 PrimeFaces &lt;p:selectOneMenu&gt;调用 valueChangeListener?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆