p:inputText is not updating

Asked

Viewed 726 times

1

I have in the following code, a form that works both to change and to add an entity.

The logic is this, I have a variable alteracao.

  • if alteracao == true the form will behave to change
  • if alteracao == false the form shall consist of add(default)

The state of this variable is being changed in Managedbean in the correct way, everything is running well.

The problem is being "only" in updating the form fields at the time of the change. By clicking the Change button in the listing(datatable), the object responsible for recovering the inputs entidade is set in the correct way, but the form does not update with its values. ONLY 'updates' to the value of Id, which by default is hidden, but by clicking the change button it becomes rendered and with the value there.

I wanted, by clicking the Change button, the form to be filled with the values of the object I recovered.

Java bean.

package managedbeans;

//imports.....

@ManagedBean
@ViewScoped
public class Bean {

    private List<Entidade> entidades;
    private List<String> campo2Disponiveis;

    private Entidade entidade;    

    private boolean alteracao;

    public List<Entidade> getEntidades() {
        return entidades;
    }

    public Entidade getEntidade() {
        return entidade;
    }

    public void setEntidade(Entidade entidade) {
        this.entidade = entidade;
    }

    public List<String> getCampo2Disponiveis() {
        return campo2Disponiveis;
    }

    public boolean isAlteracao() {
        return alteracao;
    }

    public void setAlteracao(boolean alteracao) {
        this.alteracao = alteracao;
    }

    @PostConstruct
    public void init() {
        entidade = new Entidade();
        alteracao = false;

        carregaComponentes();
    }

    private void carregaComponentes() {
        EntityManager entityManager = JPAUtil.getEntityManager();
        EntidadeDAO entidadeDAO = new EntidadeDAO(entityManager);

        entidades = entidadeDAO.getEntidades();
        campo2Disponiveis = entidades.stream().map(Entidade::getCampo2).distinct().collect(Collectors.toList());


        entityManager.close();
    }

    public void adicionaOuAlteraEntidade() {
        EntityManager entityManager = JPAUtil.getEntityManager();
        EntidadeDAO entidadeDAO = new EntidadeDAO(entityManager);

        if(alteracao) {
            alteracao = false;
            entidade = new Entidade();
        }else {
            entidadeDAO.inserir(entidade);
            entidades.add(entidade);
            entidades.sort((o1, o2) -> o1.getCampo2().compareTo(o2.getCampo2()));
            entidade = new Entidade();
        }

        entityManager.close();
    }

    public void alteraEntidade() {
        alteracao = true;

        entidade = new Entidade(entidade);
    }

}

.xhtml

<h:form id="form">
    <div class="ui-g-12 ui-md-6">
        <h2>
            <p:outputLabel value="#{bean.alteracao ? 'Alterar' : 'Adicionar'}" />
        </h2>
        <p:panelGrid id="formGrid" columns="2">

            <p:outputLabel value="Id *" rendered="#{bean.alteracao}"/>
            <p:inputText id="id" placeholder="ID"
                value="#{bean.entidade.id}" readonly="true" 
                required="true" rendered="#{bean.alteracao}"/>

            <p:outputLabel value="Campo 1" />
            <p:inputText value="#{bean.entidade.campo1}" placeholder="Descrição do campo 1" required="true"/>

            <p:outputLabel value="Campo 2 *" />
            <p:selectOneMenu  value="#{bean.entidade.campo2}" placeholder="Selecione um campo2  ou crie um novo" effect="fold" editable="true" required="true" style="width: 100%">
                <f:selectItem itemLabel="" itemValue="" />
                <f:selectItems value="#{bean.campo2Disponiveis}" />
            </p:selectOneMenu>

        </p:panelGrid>
        <div class="ui-g-12 ui-g-nopad" style="text-align: center">
            <p:commandButton
                value="#{bean.alteracao ? 'Alterar' : 'Adicionar'}"
                actionListener="#{bean.adicionaOuAlteraEntidade}"
                update="form" style="text-align: center; margin-top: 10px" />
        </div>
    </div>

    <div class="ui-g-12">
        <p:dataTable id="entidadesDataTableId"
            value="#{bean.entidades}" var="entidade"
            emptyMessage="Nenhuma entidade registrada" reflow="true">

            <f:facet name="header">Entidades</f:facet>

            <p:column headerText="Id" style="text-align: center" width="20">
                <h:outputText value="#{entidade.id}" />
            </p:column>
            <p:column headerText="Campo 1" style="text-align: left">
                <h:outputText value="#{entidade.campo1}" />
            </p:column>
            <p:column headerText="Campo 2">
                <h:outputText value="#{entidade.campo2}" />
            </p:column>
            <p:column headerText="Alterar" style="text-align: center"
                width="70">
                <p:commandButton value="Alterar" immediate="true"
                    action="#{bean.alteraEntidade}" update="form">
                    <f:setPropertyActionListener value="#{entidade}"
                        target="#{bean.entidade}" />
                </p:commandButton>
            </p:column>
        </p:dataTable>
    </div>
</h:form>

1 answer

0


Because of the component <p:inputText> have a rule there of rendered, the processing of this component is skipped, that is, in the 'apply request values' phase the JSF "jumps" the processing of this component (I am not sure, but it is defensively, to avoid malicious requests).

The way to solve this problem is by adding the attribute proccess="@this" in the <p:commandButton> that fires this ajax ensuring the call of the action passed in the component.

The code is then

Code of <p:commandButton>

<p:column headerText="Alterar" style="text-align: center" width="70">
    <p:commandButton value="Alterar" immediate="true" process="@this"
        action="#{bean.alteraEntidade}" update="form">
        <f:setPropertyActionListener value="#{entidade}"
            target="#{bean.entidade}" />
    </p:commandButton>
</p:column>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.