I have an Xhtml page with a DataTable and within that Datatable has a list with the result of items that came from the Database, one of the columns has a button called edit where it opens a form with input'se that inputs are filled with the data, but clicking save data is not modified. In the console the update values are '?'. Everything is triggered however the values are not received and sent.
XHTML COM DATABASE
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
xmlns:f="http://xmlns.jcp.org/jsf/core"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:p="http://primefaces.org/ui">
<h:head>
<style>
body {
background: #e8e8e8;
}
#colunanova {
background-color: white;
}
</style>
</h:head>
<h:body>
<ui:include src="menu.xhtml" />
<p:growl id="growl" showDetail="true" sticky="true" />
<h:form>
<p:dataTable var="car" value="#{CarroMB.gerarTabela()}">
<p:column headerText="Id">
<h:outputText value="#{car.id}" />
</p:column>
<p:column headerText="Marca">
<h:outputText value="#{car.marca}" />
</p:column>
<p:column headerText="Modelo">
<h:outputText value="#{car.modelo}" />
</p:column>
<p:column headerText="Valor">
<h:outputText value="#{car.valor}" />
</p:column>
<p:column headerText="Cor">
<h:outputText value="#{car.cor}" />
</p:column>
<p:column id="colunanova">
<p:commandButton id="btnedit" value="Editar" type="button" />
<p:overlayPanel for="btnedit" hideEffect="fade" dynamic="true" style="width:600px">
<p:inputText value="#{car.id}" type="hidden" />
<p:column headerText="Marca">
<h:outputLabel>Marca: </h:outputLabel>
<p:inputText value="#{car.marca}" />
</p:column>
<br/>
<p:column headerText="Modelo">
<h:outputLabel>Modelo: </h:outputLabel>
<p:inputText value="#{car.modelo}" />
</p:column>
<br/>
<p:column headerText="Valor">
<h:outputLabel>Valor: </h:outputLabel>
<p:inputText value="#{car.valor}" />
</p:column>
<br/>
<p:column headerText="Ano">
<h:outputLabel>Ano: </h:outputLabel>
<p:inputText value="#{car.ano}" />
</p:column>
<br/>
<p:column headerText="Cor">
<h:outputLabel>Cor: </h:outputLabel>
<p:inputText value="#{car.cor}" />
</p:column>
<br/>
<p:commandButton value="Salvar" action="#{CarroMB.editar(car)}" update="growl" onclick="PF('new_carro')" />
</p:overlayPanel>
<p:commandButton value="Excluir" action="#{CarroMB.apagar(car)}" />
</p:column>
</p:dataTable>
</h:form>
BEAN
package br.com.controller;
import java.util.ArrayList;
import java.util.List;
import javax.faces.bean.ManagedBean;
import br.com.dao.CarroDAO;
import br.com.model.Carro;
@ManagedBean(name="CarroMB")
public class CarroBean {
private Carro carro = new Carro();
private CarroDAO<Carro> carroDao = new CarroDAO<Carro>();
private List<Carro> carrolista = new ArrayList<Carro>();
public List<Carro> pesquisar(){
return carroDao.pesquisarTabela(carro);
}
public void apagar(Carro car){
carroDao.apagarCarro(car);
}
public void editar(Carro car){
this.carro = car;
carroDao.editarCarro(car);
}
public void adicionar() {
carroDao.adicionarCarro(carro);
}
public List<Carro>gerarTabela() {
return carroDao.gerarTabela();
}
public Carro getCarro() {
return carro;
}
public void setCarro(Carro carro) {
this.carro = carro;
}
public CarroDAO<Carro> getCarroDao() {
return carroDao;
}
public void setCarroDao(CarroDAO<Carro> carroDao) {
this.carroDao = carroDao;
}
public List<Carro> getCarrolista() {
return carrolista;
}
public void setCarrolista(List<Carro> carrolista) {
this.carrolista = carrolista;
}
}
METHODS GENERATE TABLE AND EDIT
@SuppressWarnings("unchecked")
public List<Carro> gerarTabela(){
//conexão com o banco
SessionFactory factory = new Configuration().configure().buildSessionFactory();
Session session = factory.openSession();
session.beginTransaction();
//fim da conexão
List<Carro> listcarros = new ArrayList <Carro>(); //cria a lsta com os atributos carros
listcarros = session.createQuery("from Carro").list(); //faz a query e lista
session.close();
factory.close();
return listcarros; //retorna a listcarros com a query
}
public void editarCarro(Carro car){
car.setId(car.getId());
car.setMarca(car.getMarca());
car.setModelo(car.getModelo());
car.setCor(car.getCor());
car.setAno(car.getAno());
car.setValor(car.getValor());
car.setDescricao(car.getDescricao());
SessionFactory factory = new Configuration().configure().buildSessionFactory();
Session session = factory.openSession();
session.beginTransaction();
session.update(car);
editMessage();
session.getTransaction().commit();
session.close();
factory.close();
}