0

我在两个不会触发的嵌套表单中有一个支持 bean 方法(会话范围)。

我用一个展示问题的通用示例提出了这个问题。

我希望能深入了解表单、对话框和appendToBody标签是如何/为什么导致问题的。

澄清一下,contentsOfDialogTwo.xhtml - nestedDialogsBean.actionOnClickOfDialogTwoItem()- 中的动作不会触发。将显示 javascript 警报,但在支持 bean 上的操作上的断点显示它没有被调用。

模板视图:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:p="http://primefaces.org/ui">
<ui:composition>
    <ui:insert name="content"/>
</ui:composition>
</html>

父视图:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:p="http://primefaces.org/ui">
<ui:composition template="../templates/layout.xhtml">
    <ui:define name="content">
        <h:form>
            <p:panel>
                <p:tabView id="exampleTabView" dynamic="false">
                    <p:tab id="nestedDialogsTab" title="Nested Dialogs Tab">
                        <ui:insert>
                            <ui:include src="childView.xhtml"></ui:include>
                        </ui:insert>
                    </p:tab>
               </p:tabView>
            </p:panel>
         </h:form>
    </ui:define>
</ui:composition>
</html>

子视图:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml"
        xmlns:f="http://java.sun.com/jsf/core"
        xmlns:h="http://java.sun.com/jsf/html"
        xmlns:ui="http://java.sun.com/jsf/facelets"
        xmlns:p="http://primefaces.org/ui">
    <ui:composition>
    <p:commandButton 
    id="openDialog1" 
    value="Open Dialog 1"
    action="#{nestedDialogsBean.actionOnOpenOfDialog1()}"
    oncomplete="dialogOneVar.show()">
     </p:commandButton>
    <p:dialog id="dialogOne" header="Panel One" widgetVar="dialogOneVar"
            resizable="false" modal="true" closable="true" dynamic="true"
            hideEffect="fold" draggable="false" height="700" width="1000">
            <ui:insert>
                <ui:include src="contentsOfDialogOne.xhtml"></ui:include>
            </ui:insert>
        </p:dialog>
    </ui:composition>
    </html>

contentOfDialogOne.xhtml:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:p="http://primefaces.org/ui">
    <p:commandButton 
    id="openDialog2" 
    value="Open Dialog 2"
    action="#{nestedDialogsBean.actionOnOpenOfDialog2()}"
    oncomplete="dialogTwoVar.show()">
     </p:commandButton>
    <p:dialog id="dialogTwo"
        header="Dialog Two"
        widgetVar="dialogTwoVar" 
        closable="true"
        dynamic="true" 
            resizable="false" draggable="false" height="700"
        width="1000" hideEffect="fold" appendToBody="true" >
        <ui:insert>
            <ui:include src="contentsOfDialogTwo.xhtml"></ui:include>
        </ui:insert>
    </p:dialog>
</ui:composition>
</html>

contentOfDialogTwo.xhtml

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:p="http://primefaces.org/ui">
<ui:composition>
    <h:form id="innerWrapperFormForDialogTwoDueToAppendToBody">
         <p:commandButton id="actionInsideDialogTwouButton"
         action="#{nestedDialogsBean.actionOnClickOfDialogTwoItem()}"
                onclick="alert('Button clicked')"
                value="Invoke Backing Bean Action">
            </p:commandButton>
</h:form>
</ui:composition>
</html>
4

1 回答 1

2

永远不要使用嵌套表单

appendToBody :将对话框作为文档正文的子项附加。

使用它时,对话框的渲染内容被附加到正文中,因此,例如,如果在您的 xhtml 中,对话框被包装h:form并且您appendToBody="true"在生成的页面中使用,则对话框不会被form标签包装


这在 HTML 中是非法的,并且行为未指定并且取决于所使用的网络浏览器。ajax 链接不提交表单,它只是通过 JavaScript 收集输入值,然后在后台发送 XMLHttpRequest。 为什么 a:commandLink 的 action 属性有效但 h:commandLink 的无效?

JSF 页面中的多个 h:form

于 2012-11-13T11:56:12.643 回答