Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
996 views
in Technique[技术] by (71.8m points)

jsf - Button in dialog inside p:dialog is not calling controller method

I've got a problem as described in the title.

Small description of the problem is as following: I have button which is used to open dialog. Then, inside that dialog, there is button which opens another dialog on top of the first one. After clicking second button I want method from controller to be called but nothing happens. Value in h:outputText is read properly, so I guess it is not a problem with connection controller->view.

I'm using:

  • Spring web 3.1.2.RELEASE
  • JSF 2.2.10
  • Primefaces 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 tried:

  • adding appendToBody="true" to each p:dialog
  • changing from p:commandButton to p:button
  • changing from actionListener to action

but nothing helps.

I would be grateful for any help or advice of what can be the reason of not calling given method.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

There are 3 problems.

  1. You're nesting <p:dialog> components. This doesn't make sense. Separate them.

  2. A <p:dialog> must have its own <h:form>, particularly when you explicitly use appendToBody="true" or appendTo="@(body)", otherwise nothing can be submitted because JavaScript would relocate the dialog out of its position in the HTML DOM tree to the end of body, causing it to not be sitting in a form anymore.

  3. A <p:commandButton type="button"> acts as a "click" button, not as a submit button. Remove that attribute from submit buttons.

All in all, this is how it should look like:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...