p:更改选项卡时,selectOneMenu已更改

时间:2014-04-25 14:14:51

标签: jsf-2 primefaces

我有一个Primefaces dialog,里面有一个tabView。在这个tabView中我有3个选项卡,每个选项卡都指向ui:include。在第一个标签中,我有selectOneMenu,它定义了来自班级tipoProprietario的名为ProprietarioORM的属性。选项卡之间的导航不是由标签本身完成的(在此阶段它们被禁用),而是在对话框底部的commandButtons。当我单击commandButton从第一个选项卡传递到第二个选项卡时tipoProprietario被正确填充。但是,当我单击从第二个执行第三个选项卡(或甚至第一个选项卡)时,我发现selectItem值已更改为null。因此,tipoProprietario属性也更改为null。调试,我看到它首先被称为Select Item监听器方法,并且在tipoProprietario setter之后。

有人可以解释为什么会这样吗?如何纠正?

dialog.xhtml

<ui:composition xmlns="http://www.w3.org/1999/xhtml"
    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">
    <p:dialog id="dlgEdicao" widgetVar="dlgEdicao" resizable="false"
        closable="true" modal="true" closeOnEscape="true"
        header="#{(proprietarioMB.edicao)? msg['proprietario.titulo.editar']: msg['proprietario.titulo.incluir']}">
        <p:tabView id="tabEdicao" widgetVar="tabEdicao" dynamic="true"
            styleClass="tabEdicaoProprietario" cache="false"
            binding="#{proprietarioMB.tabEdicao}">
            <p:tab id="tabProprietario" title="#{msg['proprietario.titulo']}"
                disabled="#{not proprietarioMB.edicao}" titleStyleClass="">
                <ui:include src="./aba_proprietario.xhtml" />
            </p:tab>
            <p:tab id="tabEnderecoContato"
                title="#{msg['proprietario.titulo.aba.endereco']}"
                disabled="#{not proprietarioMB.edicao}">
                <ui:include src="./aba_endereco_contato.xhtml" />
            </p:tab>
            <p:tab id="tabVeiculo"
                title="#{msg['proprietario.titulo.aba.veiculo']}"
                disabled="#{not proprietarioMB.edicao}">
                <ui:include src="./aba_veiculo.xhtml" />
            </p:tab>
        </p:tabView>

        <f:facet name="footer">
            <p:commandButton id="btnDialogoAnterior"
                value="#{msg['titulo.anterior']}"
                rendered="#{not proprietarioMB.edicao}" immediate="true"
                disabled="#{not proprietarioMB.btnAnteriorHabilitado}"
                actionListener="#{proprietarioMB.doEventClickBtnAnterior}"
                update="@this tabEdicao btnDialogoSeguinte btnDialogoConcluir"
                style="margin-right: 20px;" />

            <p:commandButton id="btnDialogoSeguinte"
                value="#{msg['titulo.proximo']} #{(not proprietarioMB.btnConcluirVisivel)? '': 'display: none;'}"
                rendered="#{not proprietarioMB.edicao}"
                actionListener="#{proprietarioMB.doEventClickBtnSeguinte}"
                update="@this tabEdicao btnDialogoAnterior btnDialogoConcluir"
                style="margin-right: 20px; #{(not proprietarioMB.btnConcluirVisivel)? '': 'display: none;'}" />

        </f:facet>

    </p:dialog>
</ui:composition>

aba_proprietario.xhtml(第一个标签)

<ui:composition xmlns="http://www.w3.org/1999/xhtml"
    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">
    <p:panel id="pnlDadosProprietario">
        <p:focus for="selTipoEdicao" />
        <p:fieldset id="fieldDadosProprietario"
            legend="#{msg['proprietario.titulo']}"
            rendered="#{not empty proprietarioMB.proprietario}">
            <p:panelGrid id="pnlDadosProprietarioEdicao">
                <p:row>
                    <p:column>
                        <p:outputLabel id="lblTipoEdicao"
                            value="#{msg['proprietario.titulo.tipo']}" for="selTipoEdicao" />
                    </p:column>
                    <p:column>
                        <p:selectOneMenu id="selTipoEdicao"
                            value="#{proprietarioMB.proprietario.tipoProprietario}"
                            valueChangeListener="#{proprietarioMB.doEventTipoDocumentoPesquisaChanged}"
                            immediate="true">
                            <f:selectItems value="#{proprietarioMB.tiposProprietarios}" />
                            <p:ajax event="change" update="txtDocumentoEdicao" />
                        </p:selectOneMenu>
                    </p:column>
                    <p:column>
                        <p:outputLabel id="lblDocumentoEdicao"
                            value="#{msg['proprietario.titulo.documento']}"
                            for="txtDocumentoEdicao" />
                    </p:column>
                    <p:column>
                        <p:inputMask id="txtDocumentoEdicao"
                            value="#{proprietarioMB.proprietario.documento}"
                            mask="#{proprietarioMB.mascaraDocumento}" />
                    </p:column>
                </p:row>
            </p:panelGrid>
        </p:fieldset>
    </p:panel>
</ui:composition>

Managed Bean

@ManagedBean
@ViewScoped
public class ProprietarioMB {


    private static final String MASCARA_CPF = "999.999.999-99";

    private static final String MASCARA_CNPJ = "99.999.999/9999-99";

    private static final SelectItem[] tiposProprietarios = new SelectItem[] {
            new SelectItem(JURIDICA, JURIDICA.getDescricao()),
            new SelectItem(FISICA, FISICA.getDescricao()), };

    private ProprietarioORM proprietario;

    private String mascaraDocumento;

    private TabView tabEdicao;

    /**
     * Select Item listener method: Changes inputMask mask
     */
    public void doEventTipoDocumentoPesquisaChanged(ValueChangeEvent event) {
            this.mascaraDocumento = (event.getNewValue() == FISICA) ? MASCARA_CPF
                    : MASCARA_CNPJ;
    }

    public void doEventClickBtnAnterior() {
        System.out.println("ProprietarioMB.doEventClickBtnAnterior(): "
                + this.tabEdicao.getActiveIndex());
            this.tabEdicao.setActiveIndex(this.tabEdicao.getActiveIndex() - 1);
    }

    public void doEventClickBtnSeguinte() {
        System.out.println("ProprietarioMB.doEventClickBtnSeguinte(): "
                + this.tabEdicao.getActiveIndex());
        System.out.println("ProprietarioMB.doEventClickBtnSeguinte(): "
                + this.proprietario);
        if (this.tabEdicao.getActiveIndex() == 0) {
            if (this.validarProprietario()) { // Validation method
                this.tabEdicao.setActiveIndex(1);
            }
        } else if (this.tabEdicao.getActiveIndex() == 1) {
            this.tabEdicao.setActiveIndex(2);
        }
    }

    // Other methods + getters & setters

}

ProprietarioORM

public class ProprietarioORM {

    private String nome;

    private TipoProprietarioEnum tipoProprietario;

    // getters & setters

}

2 个答案:

答案 0 :(得分:0)

这很可能是因为binding="#{proprietarioMB.tabEdicao}"

首先,您似乎根本不需要绑定。即使你这样做了,也无法初始化你的支持bean中的组件,这个错误会导致<p:tabView/>尝试绑定到空变量tabEdicao

由于您似乎没有在支持bean中使用tabview变量,我的建议是摆脱它。如果你真的需要它,只需初始化它:

private TabView tabEdicao = new TabView();

答案 1 :(得分:0)

Kolossus:

当我将attibute dynamic="true"添加到对话框时,此问题已得到解决。实际上,真正的价值是dynamic=#{empty proprietarioMB.proprietario},因为我在加载页面时正在考虑NullPointerExceptionproprietario属性已加载@PostConstruct方法。