CesarKuehl
CesarKuehl

Reputation: 101

p:commandButton is reloading the page to open dialog

So I have this code:

<h:form id="serviceCustomFormForm">
        <p:dialog id="parameterGroupAddDialog" widgetVar="parameterGroupAddDialog" header="#{messages.addParameterGroup}" modal="true" resizable="false">       
            <p:inputText value="#{serviceCustomFormBean.serviceParameterGroup.name}" styleClass="Wid90" />
            <br />
            <br />

            <p:commandButton value="#{messages.save}" styleClass="Fright BlueButton" update="serviceCustomFormForm" actionListener="#{serviceCustomFormBean.addServiceParameterGroup}" oncomplete="PF('parameterGroupAddDialog').hide()" />
            <p:commandButton value="#{messages.cancel}" styleClass="Fright RedButton" oncomplete="PF('parameterGroupAddDialog').hide()"/>
        </p:dialog>

        <div class="Container100">
            <div class="ContainerIndent">
                <p:commandButton value="#{messages.addParameterGroup}" icon="fa fa-plus-circle" styleClass="Fright CyanButton FloatNoneOnMobile" oncomplete="PF('parameterGroupAddDialog').show()" />
                <div class="EmptyBox10 ShowOnMobile"></div>             
            </div>
        </div>
</h:form>

When the page is first loaded the @PostConstruct method is called. When I click the commandButton to open the dialog it's called again. And when I press the Cancel button inside the dialog it's called again.

This behavior does not occur in other parts of the application, and I can't see what I am missing here.

Update: As requested, the Bean code is here:

@ManagedBean
@ViewScoped
public final class ServiceCustomFormBean implements Serializable {

   private ServiceParameterGroup serviceParameterGroup = new ServiceParameterGroup();

   // Other attributes

   @PostConstruct
   private void init() {
       // Reads attributes sent from previous page  
   }

   public void addServiceParameterGroup() {
      // Saves the serviceParameterGroup to database
   }

   // Getters and Setters  
}

Upvotes: 0

Views: 2217

Answers (4)

CesarKuehl
CesarKuehl

Reputation: 101

After a long time dealing with this problem, I finally found the reason. The annotation ViewScoped that I was importing in the backing bean was from the package javax.faces.view. The correct one is javax.faces.bean.

Thanks for everyone that spend some time trying to help.

Upvotes: 1

based
based

Reputation: 65

It's because the Commandbutton submits the form. You can change to this:

<p:commandButton type="button" ...onclick="PF('parameterGroupAddDialog').hide()" 

Type button tells primefaces not to submit the form. If the form isn't submitted oncomplete is never called. So it's onclick.

Upvotes: 2

mcastilloy2k
mcastilloy2k

Reputation: 465

If I remember correctly, you should put your Dialog outside your main form, at the end of your body or use the appendTo="@(body)" param, and then, have another form inside the dialog.

Upvotes: 0

Pablo Lammel
Pablo Lammel

Reputation: 307

Try setting the following attributes to your 'Add Service' and 'Cancel' commandButton elements: partialSubmit="true" process="@this". Code like this:

<commandButton value="#{messages.addParameterGroup}" ... partialSubmit="true" process="@this" ... />

By default, pf commandButtons try to submit the whole form, while in those two cases you just want to call the invoked method without doing a submit. With this, you are saying to primefaces that you don't want to submit it all (partialSubmit=true), and that you just want to process the invocation of the button itself (process=@this). Maybe that is your problem.

As an additional comment, i don't think getting the label values for the buttons from the bean is a good idea (unless you want to intentionally change the labels dynamically), because you will end up doing excessive requests to the bean. Better try using a messages properties file, as described in here http://www.mkyong.com/jsf2/jsf-2-0-and-resource-bundles-example/.

Upvotes: 0

Related Questions