p:dialog 内对话框中的按钮未调用控制器方法 [英] Button in dialog inside p:dialog is not calling controller method

查看:31
本文介绍了p:dialog 内对话框中的按钮未调用控制器方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了标题中描述的问题.

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

问题的小描述如下:我有用于打开对话框的按钮.然后,在该对话框中,有一个按钮可以在第一个对话框之上打开另一个对话框.单击第二个按钮后,我希望调用控制器中的方法,但没有任何反应.h:outputText 中的值被正确读取,所以我猜这不是连接控制器-> 视图的问题.

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.

我正在使用:

  • Spring web 3.1.2.RELEASE
  • JSF 2.2.10
  • Primefaces 5.1

代码:

beans.xml

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

TestController.java

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>

我尝试了什么:

  • appendToBody="true" 添加到每个 p:dialog
  • p:commandButton 更改为 p:button
  • actionListener 更改为 action
  • adding appendToBody="true" to each p:dialog
  • changing from p:commandButton to p:button
  • changing from actionListener to action

但没有任何帮助.

我将不胜感激任何关于不调用给定方法的原因的帮助或建议.

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

推荐答案

有3个问题.

  1. 您正在嵌套 <p:dialog> 组件.这没有意义.将它们分开.

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

<p:dialog> 必须有自己的 <h:form>,尤其是当您明确使用 appendToBody="true"appendTo="@(body)",否则无法提交任何内容,因为 JavaScript 会将对话框从其在 HTML DOM 树中的位置重新定位到正文的末尾,从而导致它不要再坐在表格里了.

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.

<p:commandButton type="button"> 充当单击"按钮,而不是提交按钮.从提交按钮中删除该属性.

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>

这篇关于p:dialog 内对话框中的按钮未调用控制器方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆