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.
– Rodrigo
After the Restart of your application server (eg Tomcat), the data is shown correctly?
– Fellipe Soares