jsf - p :dialog is not calling controller method 内

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

问题简述如下: 我有用于打开对话框的按钮。然后,在该对话框中,有一个按钮可以在第一个对话框的顶部打开另一个对话框。单击第二个按钮后,我希望调用 Controller 中的方法但没有任何反应。 h:outputText 中的值被正确读取,所以我猜这不是连接 Controller -> View 的问题。

我正在使用:

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

代码:

beans.xml

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

测试 Controller .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;
   }
}

测试.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

但没有任何帮助。

对于不调用给定方法的可能原因的任何帮助或建议,我将不胜感激。

最佳答案

有3个问题。

  1. 你在嵌套 <p:dialog>成分。这没有意义。将它们分开。

  2. A <p:dialog>必须有自己的<h:form> ,特别是当您明确使用 appendToBody="true" 时或 appendTo="@(body)" ,否则无法提交任何内容,因为 JavaScript 会将对话框从其在 HTML DOM 树中的位置重新定位到主体的末尾,导致它不再位于表单中。

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

总而言之,它应该是这样的:

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

关于jsf - p :dialog is not calling controller method 内对话框中的按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29053468/

相关文章:

java - 为什么以不同的顺序将三个 double 值相乘会得到不同的答案?

r - 带 Kernlab 的内核 PCA 和结肠癌数据集的分类

html - 绝对定位图像上的 100% 宽度

angularjs - Angular.js 将参数传递给指令

ruby-on-rails - 在哪里使用依赖 : :destroy

html - 使 HTML 中的图像变大且不模糊

.net - Streams 及其在 .Net 中的用途

maven - Jenkins 部署 WAR/EAR 选项不可用

java - 如何从命令提示符或管理控制台停止 Hybris 服务器

c - 为什么 scanf 在按下 Enter 键时将控制权返回给程序?