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

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

问题描述

我有一个问题,如标题中所述。



问题的小描述如下:
我有按钮,用于打开对话框。然后,在该对话框内,有一个按钮,在第一个对话框之上打开另一个对话框。点击第二个按钮后,我想从控制器调用方法,但没有任何反应。在 h:outputText 中的值已正确读取,因此我想这不是连接控制器 - >查看的问题。






  • Spring web 3.1.2.RELEASE

  • JSF 2.2.10

  • Primefaces 5.1



代码:



xml

 < bean id =testControllerclass =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 =1cellpadding =5> 
< p:commandButton value =Basictype =buttononclick =PF('dlg1')。show() />
< / h:panelGrid>

< p:dialog widgetVar =dlg1>
< h:outputText value =抵抗PrimeFaces是徒劳的! />
< h:panelGrid columns =1cellpadding =5>
< p:commandButton value =Basictype =buttononclick =PF('dlg2')。show(); />
< / h:panelGrid>

< p:dialog widgetVar =dlg2>
< h:outputText value =#{testController.test}/>
< p:commandButton value =Call methodtype =buttonactionListener =#{testController.testMethod}/>
< / p:dialog>
< / p:dialog>

我尝试:




  • appendToBody =true添加到每个 p:对话框

  • 更改 p:commandButton
  • $ b $ b


但没有任何帮助。



我会感谢任何帮助或建议,不调用给定方法的原因。

解决方案

有3个问题。


  1. 您嵌套< p:dialog> 组件。这没有意义。分隔它们。


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


  3. A < p:commandButton type =button> 不是作为提交按钮。从提交按钮中移除该属性。


总之,这应该是:

 < h:form> 
< h:panelGrid columns =1cellpadding =5>
< p:commandButton value =Basictype =buttononclick =PF('dlg1')。show() />
< / h:panelGrid>
< / h:form>

< p:dialog widgetVar =dlg1>
< h:form>
< h:outputText value =抵抗PrimeFaces是徒劳的! />
< h:panelGrid columns =1cellpadding =5>
< p:commandButton value =Basictype =buttononclick =PF('dlg2')。show(); />
< / h:panelGrid>
< / h:form>
< / p:dialog>

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


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.

解决方案

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>

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

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