0

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  
}
CesarKuehl
  • 91
  • 1
  • 7
  • I noticed that if I separate the dialog and the commandButton into two different forms the @PostConstruct method is not called on dialog opening but is still called when the cancel button is clicked – CesarKuehl Jun 03 '16 at 03:14
  • So you contineously talk about a bean, but did not post the code. Please create an [mcve] – Kukeltje Jun 03 '16 at 06:33
  • I've also noticed that if I remove the inputText that is inside the dialog, the problem does not occur – CesarKuehl Jun 03 '16 at 14:21
  • I tried to remove the form from outside the dialog and adding one inside the dialog as suggested, but the behavior persists. As I stated before, for some reason, removing the inputText stops the page reload on dialog opening and the cancel button. But I can't explain why – CesarKuehl Jun 04 '16 at 12:43

4 Answers4

2

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.

based
  • 65
  • 1
  • 7
  • Thank you for answering, when I added the type="button" to the Cancel button inside the dialog it stopped submitting the form, but also didn't close the modal. – CesarKuehl Jun 03 '16 at 15:42
  • @CesarKuehl edited my answer so it closes. if it helped please accept answer and upvote :) By the way some answer under here are plain wrong – based Jun 03 '16 at 20:15
  • Almost there! Adding the type="button" and the onclick tag did worked on the Cancel button, but the Save button didn't – CesarKuehl Jun 04 '16 at 11:56
1

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.

CesarKuehl
  • 91
  • 1
  • 7
0

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

Pablo Lammel
  • 187
  • 1
  • 4
  • 13
  • Thank you for the answer, I'll try the attributes as soon as I can. As to the label values recomendation, I'm using a resource bundle. 'messages' is not a bean it's the var name I declared on faces-config.xml as follows: bundle messages – CesarKuehl Jun 03 '16 at 16:53
  • Adding the attributes almost solved it. The cancel button works perfectly now, but the save button still tries to reload the page before firing the addServiceParameterGroup method – CesarKuehl Jun 04 '16 at 12:39
  • Use the same attributes for "save" button: ... partialSubmit="true" process="@this" – Pablo Lammel Jun 06 '16 at 13:41
  • Your page won't reload anymore. After processing, you can try using [messages tag] (http://www.primefaces.org/showcase/ui/message/messages.xhtml) to inform the user the result of the processing. Hope it helps! – Pablo Lammel Jun 06 '16 at 13:48
0

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.

mcastilloy2k
  • 478
  • 7
  • 9