jsf - Primefaces 对话框渲染两次

标签 jsf jsf-2 dialog primefaces modal-dialog

我创建了一个 ui:component 来像弹出窗口一样使用,因此我可以使用此模板的标准创建很多弹出窗口。 该组件只是一个带有两个按钮(取消和提交)的弹出窗口和一个可以覆盖的内容,如下所示:

<!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:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:c="http://java.sun.com/jsp/jstl/core"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:p="http://primefaces.prime.com.tr/ui">
<ui:component>
<p:dialog widgetVar="#{idPopup}" id="#{idPopup}" modal="#{popup.modal}"
    draggable="#{popup.modal}"
    rendered="#{popup.visivel}" visible="#{popup.visivel}"
    closeOnEscape="false" closable="false" header="#{titulo}"
    resizable="false" styleClass="autoWidthDialog" showEffect="fade"
    hideEffect="fade">
    <h:panelGroup style="width:100%">
        <p:focus />
        <ui:insert name="conteudo">Nenhum conteúdo definido!</ui:insert>
        <h:panelGrid id="#{idPopup}PainelMensagens" style="width:100%">
            <p:messages />
        </h:panelGrid>
        <ui:insert name="barraDeBotoes">
            <h:panelGroup layout="block" style="width:100%">
                <p:commandButton value="CANCELAR" immediate="true" update="@form"
                    style="float:right" action="#{controladorPopup.fechar}"
                    onclick="#{idPopup}.hide();" />
                <p:commandButton value="OK" style="float:right"
                    update="@form formAlerta"
                    action="#{controladorPopup.submit}"
                    process="@form" />
            </h:panelGroup>
        </ui:insert>
    </h:panelGroup>
</p:dialog>
</ui:component>
</html>

当我尝试提交表单而不填写必填字段时,就会出现问题。正确的行为只是再次显示带有消息的弹出窗口,但对话框会呈现两次,一次带有消息,另一次没有消息。 您可以在此处看到此行为:

twice rendering

这是该模板的一种用途:

<ui:composition template="../templates/popupSubmit.xhtml">
<ui:param name="titulo" value="Buscar pessoa" />
<ui:param name="popup" value="#{modeloPopupBuscaPessoa}" />
<ui:param name="controladorPopup"
    value="#{controladorPopupBuscaPessoa}" />
<ui:define name="conteudo">
    <h:panelGroup>
        <h:panelGrid columns="2">
            <h:outputLabel value="Tipo de cadastro:" style="float:none" />
            <h:selectOneMenu value="#{controladorSugestaoPessoa.tipoCadastro}"
                immediate="true">
                <f:selectItems value="#{carregadorTipoCadastro.itens}" />
                <f:ajax event="change" immediate="true" />
            </h:selectOneMenu>
        </h:panelGrid>
        <h:outputText value="Buscar por:" />
        <h:selectOneRadio value="#{controladorSugestaoPessoa.tipoBusca}"
            immediate="true">
            <f:selectItems value="#{carregadorTipoBuscaPessoa.itens}" />
            <f:ajax event="change" immediate="true" />
        </h:selectOneRadio>
        <p:autoComplete value="#{modeloPopupBuscaPessoa.itemSelecionado}"
            forceSelection="true" maxResults="10" queryDelay="500" 
            completeMethod="#{controladorSugestaoPessoa.atualizarSugestoes}"
            var="pessoa" itemLabel="#{pessoa.label}" itemValue="#{pessoa}"
            converter="#{conversorSelectItem}" />
    </h:panelGroup>
</ui:define>
</ui:composition>

这些是一些用途:

<h:form id="cadastroPessoa">
    <ui:include
        src="resources/components/popups/modulo_cadastro/popupNovoCadastroPessoa.xhtml">
        <ui:param name="idPopup" value="popupNovoCadastroPessoa" />
    </ui:include>
    <ui:include
        src="resources/components/popups/modulo_cadastro/popupCadastroPessoa.xhtml">
        <ui:param name="idPopup" value="popupEdicaoCadastroPessoa" />
    </ui:include>
    <ui:include
        src="resources/components/popups/modulo_cadastro/popupBuscaPessoa.xhtml">
        <ui:param name="idPopup" value="popupBuscaCadastroPessoa" />
    </ui:include>
</h:form>

<h:form id="cadastroProduto">
    <ui:include
        src="resources/components/popups/modulo_cadastro/popupCadastroProduto.xhtml">
        <ui:param name="idPopup" value="popupNovoCadastroProduto" />
    </ui:include>
</h:form>

有人可以告诉我为什么会发生这种情况吗?

最佳答案

我在 primefaces 论坛上发布了同样的问题(就像 Tommy Chan 所说的那样),有人回答了这个问题:

You are probably placing your dialog in the form you are updating which is a nono. Never update the dialog only the stuff in the dialog

我一直尝试这样做,直到我看到所有对话框都有来自服务器的“渲染”属性(只需查看第一个 xml),我在这个应用程序中有很多对话框,其中一些对话框与其他对话框有关系(在服务器上),最后这些采用相同的形式。

我做了一些不同的事情,我只创建了这个 JavaScript 代码:

function removerDialogo(id) {
   setTimeout(function() {
       removerDialogoAposIntervalo(id);
   }, 100);  
}

function removerDialogoAposIntervalo(id) { 
   id = id.replace(':', '\\:');
   jQuery('div.ui-dialog')
       .find('#' + id)
       .parent().eq(1)
       .remove(); 
}

并在对话框“onShow”属性上调用此属性:

<p:dialog widgetVar="#{idPopup}" id="#{idPopup}" modal="#{popup.modal}"
    draggable="#{popup.modal}" rendered="#{popup.visivel}"
    visible="#{popup.visivel}" closeOnEscape="false" closable="false"
    header="#{titulo}" resizable="false" styleClass="autoWidthDialog"
    showEffect="fade" hideEffect="fade" onShow="removerDialogo(this.id)">

我不喜欢做这样的事情,但我找不到更好的方法来解决这个问题...... 如果有人给我更好的解决方案,我将不胜感激

关于jsf - Primefaces 对话框渲染两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7880539/

相关文章:

jquery - 如何使用 jquery UI 对话框

Qt 颜色选择器小部件?

jsf - c:for每个项目都没有从支持bean更新

jsf-2 - Websphere 8.5 - JSF webapp 上下文初始化错误 : cleanupInitMaps

JSF 大于零验证器

java - JSF 字符获得双 UTF-8 编码

jquery - 我如何使用 Font Awesome(或其他字体图标)而不是 Primefaces DataTable 中的 jQuery Sprite ?

c# - 使用 ASP.NET 按钮显示的 jQuery UI 对话框

ajax - 如何保存 2 个相关的 selectOneMenu 值

jsf - CRUD 应用程序推荐的 JSF 2.0 范围是什么?