JSF Dialog无法打开

时间:2012-09-01 15:59:19

标签: jsf-2 primefaces

好的伙计们,我在这里有一个问题:我的面板和ajax行为突然停止工作。他们跑得很好,但突然停下来工作了。我的JSF页面我有一个对话框面板( confirmPanel,newPanel ),必须在commandButtons的单击事件( btnNew,btnExclude )上打开。这是JSF页面的代码。               

    <ui:define name="content">
        <f:view>
            <h:form id="frmPublico">
                <p:panelGrid columns="2" styleClass="ui-main-div">
                    <f:facet name="header">
                        <p:panelGrid columns="2">
                            <p:commandButton id="btnNew" value="Novo" onclick="wvNew.show();" type="button"/>
                        </p:panelGrid>
                    </f:facet>
                    <p:growl id="gGlobalMsgs" globalOnly="true" redisplay="false"/>
                    <p:dataTable id="dtPublico" value="#{publicoBean.lstDataTablePublico}" 
                             var="publico" paginator="true" rows="#{publicoBean.maximoLinhasTablePublico}" 
                             paginatorTemplate="{FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}"
                             rowsPerPageTemplate="5,10" >

                        <f:facet name="header">
                            Públicos
                        </f:facet>
                        <p:column>
                            <p:commandButton type="button" id="btnEdit" image="ui-icon-pencil" /> 
                        </p:column>
                        <p:column>
                            <p:commandButton type="button" id="btnExclude" image="ui-icon-circle-close" onclick="wvConfirm.show()" /> 
                        </p:column>
                        <p:column>
                            <f:facet name="header">
                                Nome
                            </f:facet>
                            <h:outputText value="#{publico.nmePublico}" />
                        </p:column>
                        <p:column>
                            <f:facet name="header">
                                Tipo de público
                            </f:facet>
                            <h:outputText value="#{publico.tdTipoPublico}"  />
                        </p:column>

                        <f:facet name="footer">
                            Públicos
                        </f:facet>
                    </p:dataTable>

                    <f:facet name="footer">

                    </f:facet>
                </p:panelGrid>

                <!-- Caixas de diálogos -->
                <p:dialog id="dialogNovo" header="Novo público" widgetVar="wvNew" modal="true" appendToBody="false" >
                    <p:panelGrid columns="1">

                        <p:panelGrid columns="2" id="pnlNovoPublico">
                            <!-- Cabeçalho do grid -->
                            <f:facet name="header">
                                <!-- <p:messages id="info"/> -->
                                Cadastrar público
                            </f:facet>

                            <!-- Linha 1 -->
                            <h:outputLabel for="txtNomePublico" value="*Nome:"/>
                            <p:inputText id="txtNomePublico" value="#{publicoBean.nome}" required="true" requiredMessage="Digite o nome do público." />
                            <p:message for="txtNomePublico" redisplay="false" id="msgNomePublico" />

                            <!-- Linha 2 -->
                            <h:outputLabel for="ddlTipoPublico" value="*Tipo de público:"/>
                            <p:selectOneMenu id="ddlTipoPublico" value="#{publicoBean.tipoPublicoSelecionado}" effect="fade" 
                                         converter="#{publicoBean.conversor}" 
                                         required="true" requiredMessage="Selecione um tipo de público."> 
                                <f:selectItem itemLabel="Selecione um item..." itemValue=""/>
                                <f:selectItems value="#{publicoBean.lstMenuTipoPublico}" var="pt" itemLabel="#{pt.label}" itemValue="#{pt}"></f:selectItems>
                        </p:selectOneMenu>
                            <p:message for="ddlTipoPublico" redisplay="false" id="msgDdlTipoPublico" />

                        <!-- Rodapé do grid -->
                            <f:facet name="footer">
                                <p:commandButton actionListener="#{publicoBean.cadastrarPublico}" value="Cadastrar" icon="ui-icon-check" ajax="true" update="pnlNovoPublico, msgGlobal"/>
                            </f:facet>
                        </p:panelGrid>
                    </p:panelGrid>
                </p:dialog>

                <!-- Caixa de diálogo de confirmação -->
                <p:confirmDialog id="dialogConfirmExclusao" message="Tem certeza que deseja excluir este item?"
                             header="Confirmar exclusão" severity="alert" widgetVar="wvConfirm">
                    <p:commandButton id="btnConfirm" value="Continuar" oncomplete="wvConfirm.hide()"
                                 actionListener="#{publicoBean.excluirPublico}" />
                    <p:commandButton id="btnCancel" value="Cancelar" onclick="wvConfirm.hide()" type="button" /> 

                </p:confirmDialog>
            </h:form>
        </f:view>
    </ui:define>
</ui:composition>

ID WidVar 属性不同。当我打开Javascript控制台并点击 wvNew wvConfirm 时,它们是未定义的。可能发生什么?

2 个答案:

答案 0 :(得分:0)

来自PrimeFaces文档:

  

小心使用appendToBody作为页面定义和html dom   是不同的,例如,如果对话框在h:form组件中   appendToBody已启用,浏览器对话框将在外面   形成并可能导致意外结果。在这种情况下,嵌套一个表单   在对话框内。

答案 1 :(得分:0)

我改变了我的xhtml,现在它遵循建议:

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE composition PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<ui:composition xmlns:ui="http://java.sun.com/jsf/facelets"
            xmlns:h="http://java.sun.com/jsf/html"
            xmlns:f="http://java.sun.com/jsf/core"
            xmlns:p="http://primefaces.org/ui"
            template="./../../resources/templates/baseTemplate.xhtml">

<ui:define name="content">
    <f:view>
        <h:form id="frmPublic">
            <!-- Show general msgs -->
            <p:growl id="gMessages" globalOnly="true" sticky="false" />

            <!-- Show public list -->
            <p:dataTable id="dtPublics" value="#{publicBean.publics}"
                         paginator="true" rows="5" rowsPerPageTemplate="5,10"
                         paginatorTemplate="{FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}"
                         var="actual">
                <f:facet name="header">
                    <p:commandButton id="cbViewNew" value="New" type="button" styleClass="cbViewNew" onclick="dlgNewPublic.show();" />
                </f:facet>
                <!-- Edit and delete columns -->
                <p:column>
                    <p:commandButton id="cbViewEdit"  image="ui-icon-pencil" 
                                     oncomplete="dlgEditPublic.show()"  title="Edit">
                        <f:setPropertyActionListener value="#{actual}" target="#{publicBean.selectedPublic}" />
                    </p:commandButton>
                </p:column>
                <p:column>
                    <p:commandButton id="cbViewDelete" type="button" image="ui-icon-circle-close" onclick="dlgConfirm.show()" title="Delete" /> 
                </p:column>
                <p:column>
                    <f:facet name="header">
                        Name
                    </f:facet>
                    <h:outputLabel id="olViewPublicName" value="#{actual.publicName}"/>
                </p:column>
                <p:column>
                    <f:facet name="header">
                        Public type
                    </f:facet>
                    <h:outputLabel id="olViewPublicType" value="#{actual.publicType}"/>
                </p:column>
            </p:dataTable>
        </h:form>
        <!-- Caixa de diálogo de inclusão -->
        <p:dialog id="newPublicDialog" widgetVar="dlgNewPublic" modal="true" header="New"
                  resizable="false">
            <h:form id="frmNew">
                <p:panelGrid columns="2">
                    <p:outputLabel id="olNewPublicName" value="Name:" for="itNewPublicName"/>
                    <p:inputText id="itNewPublicName" value="#{publicBean.name}" required="true"
                                 requiredMessage="Enter the name"/>
                    <p:outputLabel id="olNewPublicType" for="somNewPublicType" value="Public type:"/>
                    <p:selectOneMenu id="somNewPublicType" value="#{publicBean.selectedPublicType}" effect="fade" 
                                     converter="#{publicBean.converter}" 
                                     required="true" requiredMessage="Select a public type"> 
                        <f:selectItem itemLabel="-- Select --" itemValue=""/>
                        <f:selectItems value="#{publicoBean.lstPublicType}" var="actual" itemLabel="#{actual.label}" itemValue="#{actual}"></f:selectItems>
                    </p:selectOneMenu>
                    <p:commandButton id="cbNewSave" value="Save" update=":dlgNewPublic" actionListener="#{publicBean.save}" icon="ui-icon-check" 
                                      oncomplete="dlgNewPublic.hide();" />
                    <p:commandButton value="Cancel" immediate="true" onclick="dlgNewPublic.hide()"/>
                    <f:facet name="footer">
                        <p:message id="mNewPublicName" for="itNewPublicName" redisplay="false" />
                        <p:message id="mNewPublicType" for="somNewPublicType" redisplay="false"  /> 
                    </f:facet>
                </p:panelGrid>
            </h:form>
        </p:dialog>
    </f:view>
</ui:define>

我的bean保存方法如下:

...

private String name;
private PublicType publicType;
private PublicDao publicDao;
private List<Public> publics;
// Getters and Setters

...

// Actions e listeners
public void save(ActionEvent ex) {
    FacesContext context = FacesContext.getCurrentInstance();
    FacesMessage msg = new FacesMessage();
    try {
        TbPublic p = new TbPublic();
        p.setName(getName() );
        p.setTdTipoPublico(getPublicType());

        publicDao.save(p);

        // Showing success msg
        msg.setSummary("Success!");
        msg.setSeverity(FacesMessage.SEVERITY_INFO);
        context.addMessage(null, msg);

        // Load the list of public again
        publicList = publicDao.getAll();


    } catch (Exception e) {
        msg.setSummary("Error !");
        msg.setSeverity(FacesMessage.SEVERITY_ERROR);
        context.addMessage(null, msg);
    }
}

当我点击“新建”按钮时,我的对话框会完美打开。但是在我填写必要的信息并单击“保存”按钮后,没有任何反应。为什么会这样?