无法在Managed Bean中将项添加到集合

时间:2014-05-08 16:18:19

标签: java jsf jsf-2 primefaces

我有这个托管bean我有一个地址集合(每个客户端可能有N个地址)我有一个表单使用JSF(Primefaces)从我发送一些数据填充一个对象(Endereco.java)并添加这个对象是集合。

发送数据后,我检查添加的对象,它具有它应该具有的所有属性,问题是在我检查集合列表中的第一个对象之后它不存在。

可能出现什么问题?

MB中的行动:

/**
 * Action to add the object endereco to the collection in this MB
 */
public void adicionarEndereco() {
    getRemetente().getEnderecos().add(endereco);

    limparEndereco();

    // At this point the collection is still the same it was before
    Collection<Endereco> addressList = getRemetente().getEnderecos();
    String logradouro = addressList.iterator().next().getLogradouro();
    System.out.println("String from the the property in the first item: " + logradouro); //

    // close dialog box
    if (true) {
        RequestContext.getCurrentInstance().execute("dlgEndereco.hide()");
    }
}

将数据发送到MB的xhtml代码:

<h:form id="formEndereco">

    <div class="table_row_box">
        <div class="colLabel">
            <h:outputText value="#{msg['label.endereco.logradouro']}" />
        </div>
        <div class="colField">
            <h:inputText value="#{remetenteMB.endereco.logradouro}"
                label="#{msg['label.endereco.logradouro']}" required="true" maxlength="14"
                id="campoLogradouro" styleClass="input-full-width">
            </h:inputText>
        </div>
        <div class="colMessage">
            <h:message for="campoLogradouro" styleClass="error-msg" id="logradouroErrorMessage" />
        </div>
    </div>

    <div class="table_row_box">
        <div class="colLabel">
            <h:outputText value="#{msg['label.endereco.numero']}" />
        </div>
        <div class="colField">
            <h:inputText value="#{remetenteMB.endereco.numero}" label="#{msg['label.endereco.numero']}"
                required="true" maxlength="7" id="campoNumero" styleClass="w80">
            </h:inputText>
        </div>
        <div class="colMessage">
            <h:message for="campoNumero" styleClass="error-msg" id="numeroErrorMessage" />
        </div>
    </div>

    <div class="table_row_box">
        <div class="colLabel">
            <h:outputText value="#{msg['label.endereco.complemento']}" />
        </div>
        <div class="colField">
            <h:inputText value="#{remetenteMB.endereco.complemento}"
                label="#{msg['label.endereco.complemento']}" required="true" maxlength="14"
                id="campoComplemento" styleClass="input-full-width">
            </h:inputText>
        </div>
        <div class="colMessage">
            <h:message for="campoComplemento" styleClass="error-msg" id="complementoErrorMessage" />
        </div>
    </div>

    <div class="table_row_box">
        <div class="colLabel">
            <h:outputText value="#{msg['label.endereco.bairro']}" />
        </div>
        <div class="colField">
            <h:inputText value="#{remetenteMB.endereco.bairro}" label="#{msg['label.endereco.bairro']}"
                required="true" maxlength="14" id="campoBairro" styleClass="input-full-width">
            </h:inputText>
        </div>
        <div class="colMessage">
            <h:message for="campoBairro" styleClass="error-msg" id="bairroErrorMessage" />
        </div>
    </div>

    <div class="table_row_box">
        <div class="colLabel">
            <h:outputText value="#{msg['label.endereco.cidade']}" />
        </div>
        <div class="colField">
            <h:inputText value="#{remetenteMB.endereco.cidade}" label="#{msg['label.endereco.cidade']}"
                required="true" maxlength="14" id="campoCidade" styleClass="input-full-width">
            </h:inputText>
        </div>
        <div class="colMessage">
            <h:message for="campoCidade" styleClass="error-msg" id="cidadeErrorMessage" />
        </div>
    </div>

    <div class="table_row_box">
        <div class="colLabel">
            <h:outputText value="#{msg['label.endereco.estado']}" />
        </div>
        <div class="colField">
            <p:selectOneMenu id="campoEstado" value="#{remetenteMB.endereco.estado}" effect="fade"
                width="130">
                <f:selectItems value="#{estadoMB.lista}" var="estado" />
            </p:selectOneMenu>
        </div>
        <div class="colMessage">
            <h:message for="campoEstado" styleClass="error-msg" id="estadoErrorMessage" />
        </div>
    </div>

    <div class="table_row_box">
        <div class="colLabel">
            <h:outputText value="#{msg['label.endereco.referencia']}" />
        </div>
        <div class="colField">
            <h:inputText value="#{remetenteMB.endereco.referencia}"
                label="#{msg['label.endereco.referencia']}" required="true" maxlength="80"
                id="campoReferencia" styleClass="input-full-width">
            </h:inputText>
        </div>
        <div class="colMessage">
            <h:message for="campoReferencia" styleClass="error-msg" id="referenciaErrorMessage" />
        </div>
    </div>

    <div class="table_row_box" style="text-align: right;">
        <p:commandButton value="#{msg['label.adicionar']}" update=":formRemetente,formEndereco" id="btnAdicionar"
            ajax="true" actionListener="#{remetenteMB.adicionarEndereco}" icon="ui-icon-plus"
            styleClass="ui-priority-primary" style="margin-right: 5px;" />

        <p:commandButton id="btnCancelar" value="#{msg['label.cancelar']}" icon="ui-icon-cancel"
            onclick="PF('dlgEndereco').hide(); $('.ui-messages-close').click();" type="button" />
    </div>
</h:form>

任何帮助?

0 个答案:

没有答案