Roteke
Roteke

Reputation: 663

Multiple submits from page included dynamically using primefaces ajax

I've been struggling with this for a while, I hope someone can help me.

I have this code working using JSF-2 (taken from a solution given by BalusC in 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>  

Then, inside each page included, I have something like this (also working):

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

Now the problem.
It starts when I try to use PrimeFaces for inluded pages, specifically when I replace the <h:commandButton... with:

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

The result is that the form is submited multiple times and even the input fields from the other included (not rendered) pages are processed (total mess).

I'm using:
JSF 2.1.1 Mojarra implementation.
PrimeFaces 3.0-RC2.
Tomcat 7.
(Tomcat and JSF are the ones that come with NetBeans 7.0.1)

Thank you in advance.

Upvotes: 1

Views: 4192

Answers (3)

ps2_play
ps2_play

Reputation: 1

By Default primefaces are ajax enabled. So you need not specify

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

2)"@this" is must. It has to process the commandButton click. 3) use prependId="false" in . This will enable you to specify the control names in the process attribute as actual control names i.e name, desc otherwise you have to specify the form name in-front of the controls like contentFrom:name,contentForm:desc

Update = @form means it will render the complete form after execution.

Hope this will solve the problem.

Upvotes: 0

Roteke
Roteke

Reputation: 663

I had to replace binding="#{save}" for something like binding="#{savePage1}", binding="#{savePage2}" and binding="#{savePage2}" in every corresponding page.

Another error I had was in the <p:ajax..., which was causing strange behavior. I had this:

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

and should be like this:

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

It seems that "@this" is not mandatory in <f:ajax execute="..

Upvotes: 0

maple_shaft
maple_shaft

Reputation: 10463

On the p:commandButton why are you using both the binding and actionListener attributes? What is #{save} by the way? Do you mean #{itemsBeans.save}?

Regardless if you are setting an actionListener in the save method of your managed bean then you should not bind it as well. Remove the binding attribute and see if that prevents multiple postbacks.

Upvotes: 1

Related Questions