How to edit a database value using JSF?

Asked

Viewed 722 times

1

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

Code of XHTML page:

<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> 

Code of the Managedbean:

@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());
    }

}
  • Put an ID in your dialog, and on the edit button insert the update with such ID.

  • After the Restart of your application server (eg Tomcat), the data is shown correctly?

2 answers

2

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

update=":#{p:component('idDialog')}" oncomplete="PF('new_cliente').show()"

0

It is unnecessary to search for the client in the database when selecting, change its client selection method by passing the selected client as 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>

Browser other questions tagged

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