我遇到了标题中描述的问题。
问题简述如下: 我有用于打开对话框的按钮。然后,在该对话框中,有一个按钮可以在第一个对话框的顶部打开另一个对话框。单击第二个按钮后,我希望调用 Controller 中的方法但没有任何反应。 h:outputText 中的值被正确读取,所以我猜这不是连接 Controller -> View 的问题。
我正在使用:
代码:
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>
我尝试过的:
但没有任何帮助。
对于不调用给定方法的可能原因的任何帮助或建议,我将不胜感激。
最佳答案
有3个问题。
你在嵌套 <p:dialog>
成分。这没有意义。将它们分开。
A <p:dialog>
必须有自己的<h:form>
,特别是当您明确使用 appendToBody="true"
时或 appendTo="@(body)"
,否则无法提交任何内容,因为 JavaScript 会将对话框从其在 HTML DOM 树中的位置重新定位到主体的末尾,导致它不再位于表单中。
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/