【问题标题】:How to use <a4j:ajax> with <f:selectItems>如何将 <a4j:ajax> 与 <f:selectItems> 一起使用
【发布时间】:2015-08-10 07:09:56
【问题描述】:

我想编写 ajax 函数并将选择下拉菜单中的选定值传递给 bean。 这就是我所拥有的:

<rich:select id="ctpMeasurementUnits"
    value="#{associationBean.selectedMruId}"
    defaultLabel="#{msg['classifier.select']}"
    requiredMessage="#{msg['classifier.measurementUnitRequire']}"
    validatorMessage="#{msg['common.selectIllegalValue']}">
        <f:selectItems
            value="#{measurementUnitsListBean.allMeasurementUnits}"
            var="mru" itemLabel="#{mru.mruName}" itemValue="#{mru.mruId}">
            <a4j:ajax event="change" execute="@this" listener="#{associationBean.onSelectChangeMru(mru)}" />
        </f:selectItems>
    <f:validateRequired />
    <rich:validator />
</rich:select>

但是,通过使用这段代码,我得到了一个错误:

<a4j:ajax> Unable to attach <a4j:ajax> to non-ClientBehaviorHolder parent

问题:如何使用f:selectItems下拉菜单中的ajax功能?

【问题讨论】:

    标签: jsf richfaces ajax4jsf


    【解决方案1】:

    您必须将 a4j:ajax 附加到 rich:select 而不是 f:selectItems

    <rich:select id="ctpMeasurementUnits"
    value="#{associationBean.selectedMruId}"
    defaultLabel="#{msg['classifier.select']}"
    requiredMessage="#{msg['classifier.measurementUnitRequire']}"
    validatorMessage="#{msg['common.selectIllegalValue']}">
        <f:selectItems
            value="#{measurementUnitsListBean.allMeasurementUnits}"
            var="mru" itemLabel="#{mru.mruName}" itemValue="#{mru.mruId}">        
        </f:selectItems>
      <f:validateRequired />
      <a4j:ajax event="change" execute="@this" listener="#{associationBean.onSelectChangeMru(mru)}" />
      <rich:validator />
    </rich:select>
    

    【讨论】:

    • 如果我将 a4j:ajax 附加到rich:select,那么它就不起作用(永远不会调用 onSelectChangeMru 方法)
    • 然后有一个完全不同的问题导致 @WheelPot 。就目前而言,此答案直接解决了您报告的问题。任何数量的事情都可能导致您的 ajax 操作无法触发。查看您的 javascript 控制台并在此处发布侦听器方法,
    • @WheelPot 不是应该阻止执行而是摆脱参数,当侦听器执行时,bean 中的值已经被更改。 (反正参数为空)。
    【解决方案2】:

    改成

    <f:ajax> 
    

    如果你想使用

    <a4j:ajax> declare an <a4j:region> and write it down within it.
    

    【讨论】:

    • 问题代码 sn-p 中使用的标签和属性名称表明 OP 使用的是 RichFaces 4.x,因此不是 3.x,正如您的回答所预期的那样。
    • 即使他使用的是RichFaces 4 并且想要迁移到f:ajax,这也没有说明,这也不能解决问题。事实上,即使放置正确,它也可能根本不起作用。
    猜你喜欢
    • 2013-03-14
    • 2021-01-02
    • 1970-01-01
    • 2023-03-13
    • 2014-09-25
    • 2016-01-30
    • 2015-12-13
    • 2020-09-15
    • 2020-06-03
    相关资源
    最近更新 更多