<f:ajax> 不适用于 PrimeFaces 组件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18329433/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 11:05:12  来源:igfitidea点击:

<f:ajax> doesn't work on PrimeFaces component

ajaxjsfeventsprimefaces

提问by watou

I trying to use the onChangeevent of selectOneMenu, but it doesn't work and the component is not displayed when I add onChangeattribue.

我尝试使用 的onChange事件selectOneMenu,但它不起作用,并且在添加onChange属性时未显示该组件。

Can someone tell me how can I handle the onChangeevent of <p:selectOneMenu>?

有人能告诉我如何处理 的onChange事件 <p:selectOneMenu>吗?

Here is my view:

这是我的观点:

<p:selectOneMenu id="service" filterMatchMode="startsWith">  
    <f:selectItem itemLabel="Selectionner un Service : "  />  
    <f:selectItems value="#{newOpProgramme.listeSevice}" var="service" itemValue="#{service.serviceId}" itemLabel="#{service.serviceNom}"/>
    <f:ajax event="change" execute="@this" listener="#{newOpProgramme.serviceChange()}" render="nomCdp"/>
</p:selectOneMenu>

And here is the <f:ajax listener>method in a request scoped bean:

这是<f:ajax listener>请求范围bean中的方法:

public void serviceChange() {
    System.out.println("change");
}

When I change the menu, however, nothing is been printed.

但是,当我更改菜单时,没有打印任何内容。

How is this caused and how can I solve it?

这是怎么引起的,我该如何解决?

回答by BalusC

First of all, onChangeis the wrong event name. It's change. Secondly, if you intend to call the HTML attribute name, onChangeis also the wrong attribute name. It's onchange.

首先,onChange是错误的事件名称。它是change。其次,如果你打算调用HTML属性名,onChange也是错误的属性名。它是onchange

Coming back to your concrete problem; the standard JSF <f:ajax>is not compatible with PrimeFaces components. You should be using PrimeFaces own <p:ajax>instead.

回到你的具体问题;标准 JSF<f:ajax>与 PrimeFaces 组件不兼容。你应该使用 PrimeFaces own<p:ajax>代替。

<p:selectOneMenu ...>
    ...
    <p:ajax listener="#{newOpProgramme.serviceChange()}" update="nomCdp" />
</p:selectOneMenu>

Note that I omitted the eventand the processattributes. They both have already the right default value of valueChangeand @thisrespectively.

请注意,我省略了eventprocess属性。它们都已经分别具有正确的默认值valueChange@this

See also:

也可以看看:

回答by michalorlik

When I want to update something after change in selectOneMenu, I use <f:ajax>tag inside selectOneMenu like this:

当我想在 selectOneMenu 更改后更新某些内容时,我在 selectOneMenu 中使用<f:ajax>标记,如下所示:

  <h:selectOneMenu value="#{bean.selected}" >
... select items here
     <f:ajax event="change" execute="@this" render="search" />
  </h:selectOneMenu>

Where search is the Idof the object you want to update.

其中 search 是Id您要更新的对象的 。

Other solution is that you should try onchangenot onChange.

其他的解决办法是,你应该试试onchangeonChange

回答by andrexterz

<p:selectOneMenu value="#{someBean.myAttr.id}" valueChangeListener="#someBean.mySelectioMethodListener}">
    <f:selectItems value="#{someBean.listAttrs}" var="item"
        itemLabel="#{item.name}" itemValue="#{item.id}" />
    <p:ajax process="@this" update="someElementId" />
</p:selectOneMenu>

You must put an Id for <f:selectItems />and set your selection on backing bean side by posted ajax itemValue (id).

您必须<f:selectItems />通过发布的 ajax itemValue (id) 在支持 bean 端放置一个 Id并设置您的选择。

Server side method bean without a converter:

没有转换器的服务器端方法 bean:

public void mySelectionMethodListener(ValueChangeEvent event) {
ApplicationContext context = FacesContextUtils.getWebApplicationContext(FacesContext.getCurrentInstance());        
    SomeBeanDao someBeanDao = (SomeBeanDao) context.getBean(SomeBeanDao.class);
    myAttr = someBeanDao.byId((Long) event.getNewValue());
    System.out.println("value changed...");
}