2011-12-22 43 views
1

我一直在努力與此一段時間,我希望有人能幫助我。從頁面多個提交動態使用primefaces阿賈克斯

我有此代碼使用JSF-2(從this question由BalusC給定的溶液中取出)工作:

<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爲inluded頁面,特別是當我更換<h:commandButton...有:

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

結果是,形式submited多次,甚至包括來自其他輸入字段(不呈現)頁面被處理(總亂七八糟)。

我正在使用:
JSF 2.1.1 Mojarra實現。
PrimeFaces 3.0-RC2。
的Tomcat 7
(Tomcat和JSF是來與NetBeans 7.0.1的那些)

預先感謝您。

回答

0

我不得不在每個相應的頁面上替換binding="#{save}",如binding="#{savePage1}"binding="#{savePage2}"binding="#{savePage2}"

我的另一個錯誤是在<p:ajax...,這是造成奇怪的行爲。我有這樣的:

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

,應該是這樣的:

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

似乎"@this"<f:ajax execute="..

1

p:commandButton爲什麼要使用bindingactionListener屬性?順便問一句,#{save}是什麼?你的意思是#{itemsBeans.save}

無論你在託管bean的save方法中設置actionListener,你都不應該綁定它。刪除綁定屬性,看看是否阻止多次回發。

+2

'binding =#{save}'是一種解決方法,讓驗證依賴於按下的按鈕,它與'#{itemsBean.save}'看不到[this question](http:// stackoverflow。 com/a/8371726/1079323)以獲取更多信息。 但是你的回答是正確的,因爲'binding =#{save}'導致了問題: 我在3中的'binding =#{save}'上使用了相同的名字('save')包含的頁面,即使其他頁面沒有渲染,也是問題的原因。所以現在解決了。 感謝您的幫助。 – Roteke 2011-12-23 15:13:28

0

默認primefaces都支持AJAX的強制性。所以你不需要指定

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

2)「@ this」是必須的。它必須處理commandButton點擊。 3)在prependId =「false」中使用。這將使您能夠將流程屬性中的控件名稱指定爲實際控件名稱,即name,desc,否則您必須在控件的前面指定表單名稱,如 contentFrom:name,contentForm:desc

Update = @form意味着它將在執行後呈現完整的表單。

希望這能解決問題。