【问题标题】:Multiple submits from page included dynamically using primefaces ajax使用primefaces ajax动态包含来自页面的多个提交
【发布时间】:2011-12-22 21:43:54
【问题描述】:

我已经为此苦苦挣扎了一段时间,希望有人可以帮助我。

我的这段代码使用 JSF-2 工作(取自 BalusC 在this question 中给出的解决方案):

<h:panelGroup id="content" layout="block">
    <h:form id="contentform">
        <h:panelGroup rendered="#{bean.page == 'include1'}">
            <ui:include src="include1.xhtml" />
        </h:panelGroup>
        <h:panelGroup rendered="#{bean.page == 'include2'}">
            <ui:include src="include2.xhtml" />
        </h:panelGroup>
        <h:panelGroup rendered="#{bean.page == 'include3'}">
            <ui:include src="include3.xhtml" />
        </h:panelGroup>
    </h:form>
</h:panelGroup>  

然后,在包含的每个页面中,我都有这样的东西(也在工作):

<h:outputText value="Name: "/>
<h:inputText value="#{itemsBean.item.name}" id="name" required="#{not empty param[save.clientId]}"/>                    

<h:outputText value="Desc: "/>
<h:inputText value="#{itemsBean.item.description}" id="desc" required="#{not empty param[save.clientId]}"/>

<h:commandButton binding="#{save}" label="Save" actionListener="#{itemsBean.save}">
    <f:ajax render=":contentForm" execute="name desc"
</h:commandButton>

<h:dataTable value="#{itemsBean.itemsList}" var="item">
    <h:column>
        <h:outputText value="#{item.name}" />
    </h:column>
    <h:column>
        <h:outputText value="#{item.description}" />
    </h:column>
</h:dataTable>  

现在的问题
当我尝试将 PrimeFaces 用于包含的页面时,它就开始了,特别是当我将 &lt;h:commandButton... 替换为:

<p:commandButton binding="#{save}" value="Save" actionListener="#{itemsBean.save}">
    <p:ajax update=":contentForm" process="name desc" />
</p:commandButton>  

结果是表单被多次提交,甚至来自其他包含(未呈现)页面的输入字段也被处理(完全混乱)。

我正在使用:
JSF 2.1.1 Mojarra 实现。
PrimeFaces 3.0-RC2.
雄猫 7.
(Tomcat 和 JSF 是 NetBeans 7.0.1 自带的)

提前谢谢你。

【问题讨论】:

    标签: jsf-2 primefaces


    【解决方案1】:

    p:commandButton 上,您为什么同时使用bindingactionListener 属性?顺便问一下#{save} 是什么?你的意思是#{itemsBeans.save}

    无论您是否在托管 bean 的保存方法中设置了 actionListener,那么您也不应该绑定它。删除绑定属性,看看是否可以防止多次回发。

    【讨论】:

    • binding=#{save} 是一种让验证依赖于按下的按钮的解决方法,它与#{itemsBean.save} 无关,请参阅this question 了解更多信息。但是您的答案是正确的,因为 binding=#{save} 导致了问题:我在包含的 3 个页面中的 binding=#{save} 上使用了相同的名称(“保存”),即使其他页面未呈现,这是问题的原因。所以现在解决了。感谢您的帮助。
    【解决方案2】:

    我必须在每个相应页面中将binding="#{save}" 替换为binding="#{savePage1}"binding="#{savePage2}"binding="#{savePage2}"

    我遇到的另一个错误是在 &lt;p:ajax... 中,这导致了奇怪的行为。我有这个:

    <p:ajax update=":contentForm" process="name desc" />
    

    应该是这样的:

    <p:ajax update=":contentForm" process="@this name desc" />
    

    看来"@this"&lt;f:ajax execute="..中不是强制的

    【讨论】:

      【解决方案3】:

      默认情况下,primefaces 启用 ajax。所以你不需要指定

      <p:commandButton value="Save" 
        update="@form"  
        process="@this,name,desc"
           actionListener="#{itemBean.save}" /> 
      

      2)“@this”是必须的。它必须处理命令按钮单击。 3) 在 .这将使您能够将 process 属性中的控件名称指定为实际的控件名称,即 name, desc 否则您必须在控件前面指定表单名称,例如 contentFrom:name,contentForm:desc

      Update = @form 表示执行后会呈现完整的表单。

      希望这能解决问题。

      【讨论】:

        猜你喜欢
        • 2012-07-21
        • 1970-01-01
        • 2012-03-14
        • 2014-07-22
        • 2019-05-01
        • 2011-01-30
        • 2016-10-26
        • 2011-11-25
        • 2012-05-17
        相关资源
        最近更新 更多