How to edit a database value using JSF?

1

I'm trying to edit a database record using JSF. The logic is simple, there is a list of clients, and by pressing the edit button in front of each client in the list, a form opens, with inputTexts loaded with the current values, and when editing the text and pressing the save button, JPA launches an update command on the client table. Well, but by using the code below to accomplish such a task, the inputText components are not loaded with the current values of the clients. Why?

XHTML Page Code:

<p:panel header="Todos os clientes">
            <p:dataTable id="clientes" value="#{clienteMB.clientes}" var="item">
                <p:column headerText="CNPJ" style="text-align: center">
                    <h:outputLabel value="#{item.cnpj}" />
                </p:column>

                <p:column headerText="Razão Social" style="text-align: center">
                    <h:outputLabel value="#{item.razao_social}" />
                </p:column>

                <p:column style="width:40px;text-align: center">
                     <p:commandButton image="images/edit_icon.png" action="#{clienteMB.editarCliente(item.cnpj)}" onclick="PF('new_cliente').show()"
                     title="#{item.cnpj}"/>
                </p:column>

                <p:column style="width:40px;text-align: center">
                     <p:commandButton image="images/remove_icon.png" action="#" onclick="PF('new_cliente').show()"/>
                </p:column>

                <p:column style="width:40px;text-align: center">
                     <p:commandButton image="images/log_icon.png" action="#" onclick="PF('new_cliente').show()"/>
                </p:column>

            </p:dataTable>
        </p:panel>

        </p:panel>       
    </p:panelGrid>

    <p:dialog header="Pronto" widgetVar="dlg2" modal="true" height="100">
            <h:outputText value="Cliente salvo com sucesso." />
    </p:dialog>  

    <p:dialog header="Editar Cliente" widgetVar="new_cliente" modal="true" height="100">
            <h:form>
                <h:outputText value="CNPJ:" />
                <h:inputText id="cnpj_novo" value="#{clienteMB.alt.cnpj}"/>
                <h:outputText value="Razão Social:" />
                <h:inputText id="razao_nova" value="#{clienteMB.alt.razao_social}"/>
                <br/>
                <br/>
                <br/>
                <p:commandButton value="Salvar" action="#{editClienteMB.editarCliente}" process="@form"/>
                <p:commandButton value="Excluir"/>
            </h:form>
    </p:dialog> 

ManagedBean Code:

@ManagedBean
@ViewScoped
public class ClienteMB {
    Cliente cliente;
    Cliente alt;
    String nova_razao;
    String result;
    ArrayList<Cliente> clientes;
    HibernateUtil util = new HibernateUtil();
    HttpService http = new HttpService();


    public ClienteMB() throws Exception{
        cliente = new Cliente();
        alt.setRazao_social("A");
        clientes = http.getClientes();
    }

    public Cliente getCliente() {
        return cliente;
    }

    public void setCliente(Cliente cliente) {
        this.cliente = cliente;
    }

    public String getResult(){
        return result;
    }


    public ArrayList<Cliente> getClientes() {
        return clientes;
    }

    public void setClientes(ArrayList<Cliente> clientes) {
        this.clientes = clientes;
    }

    public Cliente getAlt() {
        return alt;
    }

    public void setAlt(Cliente alt) {
        this.alt = alt;
    }

    public void Salvar() throws Exception{

        try{
            util.manager.getTransaction().begin();

            util.manager.persist(this.cliente);

            util.manager.getTransaction().commit();

            util.manager.close();

            result = "Cliente salvo com sucesso";

        }catch(Exception e){
            result = e.getMessage();
        }

    }

    public void editarCliente(String cnpj){
        alt = util.manager.find(Cliente.class, cnpj);
        System.out.println(alt.getRazao_social());
    }

}
    
asked by anonymous 07.09.2015 / 20:36

2 answers

2

Good afternoon, I was also having the same problem, and I solved it as follows: I put update referencing dialog and changed onclick to oncomplete . Follow the html widget with the respective changes

update=":#{p:component('idDialog')}" oncomplete="PF('new_cliente').show()"
    
14.06.2016 / 19:43
0

It is unnecessary to fetch the client from the database when selecting, change your client selection method by passing the selected client as a parameter:

public void editarCliente(Cliente clienteSelecionado){
    alt = clienteSelecionado;
    System.out.println("cliente " + clienteSelecionado.getRazao_social() + " selecionado!");
}

And on the screen:

            <p:column style="width:40px;text-align: center">
                 <p:commandButton image="images/edit_icon.png" actionListener="#{clienteMB.editarCliente(item)}" onclick="PF('new_cliente').show();"
                      title="#{item.cnpj}"/>
            </p:column>
    
14.09.2015 / 20:56