Button in dialog box inside dialog p: does not call controller method

I have a problem as described in the title.

The small description of the problem is as follows: I have a button that is used to open a dialog. Then, a button appears inside that dialog that opens another dialog on top of the first. After clicking the second button, I want the method from the controller to be called, but nothing happens. The value in h: outputText reads correctly, so I think this is not a problem with the connection controller -> view.

I use:

  • Spring web 3.1.2.RELEASE
  • JSF 2.2.10
  • Main interfaces 5.1

code:

beans.xml

<bean id="testController" class="test.TestController" />

      

TestController.java

public class TestController implements Serializable
{
   private static final long serialVersionUID = 7028608421091861830L;

   private String test;

   public TestController()
   {
      test = "abc";
   }

   public void testMethod()
   {
      test = "cba";
   }

   public String getTest()
   {
      return test;
   }
}

      

test.xhtml

<h:panelGrid columns="1" cellpadding="5">
     <p:commandButton value="Basic" type="button" onclick="PF('dlg1').show();" />
  </h:panelGrid>

  <p:dialog widgetVar="dlg1">
     <h:outputText value="Resistance to PrimeFaces is futile!" />
     <h:panelGrid columns="1" cellpadding="5">
        <p:commandButton value="Basic" type="button" onclick="PF('dlg2').show();" />
     </h:panelGrid>

     <p:dialog widgetVar="dlg2">
        <h:outputText value="#{testController.test}" />
        <p:commandButton value="Call method" type="button" actionListener="#{testController.testMethod}" />
     </p:dialog>
  </p:dialog>

      

What I have tried:

  • adding appendToBody = "true" to every dialog p:
  • change from p: commandButton to p: button
  • transition from actionListener to action

but nothing helps.

I would appreciate any help or advice that might be the reason for the refusal to call this method.

0


source to share


2 answers


There are 3 problems.

  • Nested components <p:dialog>

    . This makes no sense. Separate them.

  • A <p:dialog>

    must have its own <h:form>

    , especially if you are explicitly using appendToBody="true"

    or appendTo="@(body)"

    , otherwise nothing can be submitted, because JavaScript will move the dialog from its position in the HTML DOM tree to the end of the body, causing it to no longer sit in the form.

  • A <p:commandButton type="button">

    acts like a "click" button, not a submit button. Remove this attribute from submit buttons.



In general, this is how it should look:

<h:form>
    <h:panelGrid columns="1" cellpadding="5">
        <p:commandButton value="Basic" type="button" onclick="PF('dlg1').show();" />
    </h:panelGrid>
</h:form>

<p:dialog widgetVar="dlg1">
    <h:form>
        <h:outputText value="Resistance to PrimeFaces is futile!" />
        <h:panelGrid columns="1" cellpadding="5">
            <p:commandButton value="Basic" type="button" onclick="PF('dlg2').show();" />
        </h:panelGrid>
    </h:form>
</p:dialog>

<p:dialog widgetVar="dlg2">
    <h:form>
        <h:outputText value="#{testController.test}" />
        <p:commandButton value="Call method" actionListener="#{testController.testMethod}" />
    </h:form>
</p:dialog>

      

+3


source


OK. I think I found a way to fix this problem.

It seems the problem was:



type="button"

      

I removed it from the list of attributes of each button and now it works even without h: form. Thanks for the help.

+3


source







All Articles