Primefaces 嵌套对话框/'appendToBody' - 支持 bean 操作未触发 [英] Primefaces nested Dialogs / 'appendToBody' - backing bean action not firing

查看:20
本文介绍了Primefaces 嵌套对话框/'appendToBody' - 支持 bean 操作未触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a backing bean method (session scoped) within two nested forms which will not fire.

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

I set out the question with a generic example which exhibits the problem.

如果您能深入了解表单、对话框和 appendToBody 标签如何/为什么会导致问题.

I would appreciate an insight into how/why the forms, dialogs and appendToBody tag are causing the problem.

澄清一下,contentsOfDialogTwo.xhtml - nestedDialogsBean.actionOnClickOfDialogTwoItem() 中的操作不会触发.显示了 javascript 警报,但支持 bean 上操作的断点显示未调用.

To clarify, the action in contentsOfDialogTwo.xhtml - nestedDialogsBean.actionOnClickOfDialogTwoItem() - does not fire. The javascript alert is displayed, but a breakpoint on the action on the backing bean shows that this is not called.

模板视图:

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

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

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

推荐答案

切勿使用嵌套表单

appendToBody : 将对话框作为文档正文的子级附加.

appendToBody : Appends dialog as a child of document body.

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

When using it the rendered content of the dialog is being appended to the body , so if for example in your xhtml the dialog was wrapped by h:form and you using appendToBody="true" in the generated page the dialog wont be wrapped by the form tag

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

This is illegal in HTML and the behaviour is unspecified and dependent on the webbrowser used. The ajax link doesn't submit the form, it just collects the input values by JavaScript and then sends a XMLHttpRequest in the background. why a:commandLink's action attribute works but h:commandLink's does not?

一个 JSF 页面中的多个 h:form

这篇关于Primefaces 嵌套对话框/'appendToBody' - 支持 bean 操作未触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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