Estou tentando recuperar um objeto do dataTable, só que recupero o ID do objeto mais não mostra os objetos no formulário depois de encaminhado. Fiz um converter mais não deu certo.
OBS: Já consigo Excluir e Persitir. OBS: segue meu codigo com um erro quando recupero o objeto.
Minha pagina de pesquisa.
<ui:composition template="/WEB-INF/template/layoutPadrao.xhtml"
xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:f="http://xmlns.jcp.org/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:p="http://primefaces.org/ui"
xmlns:o="http://omnifaces.org/ui">
<ui:define name="titulo">Pesquisa de Fabricantes</ui:define>
<ui:define name="corpo">
<f:metadata>
<f:viewAction action="#{consultaFabricante.inicializa}" />
</f:metadata>
<h1>Pesquisa de Fabricantes</h1>
<h:form id="frmPesquisa">
<p:messages id="messages" autoUpdate="true" closable="true" />
<p:toolbar style="margin-top: 20px">
<p:toolbarGroup>
<p:button value="Novo" />
</p:toolbarGroup>
</p:toolbar>
<p:dataTable id="fabricantesTable" value="#{consultaFabricante.fabricantes}" var="fabricante"
style="margin-top: 20px" emptyMessage="Nenhum fabricante encontrado." rows="20"
paginator="true" paginatorAlwaysVisible="false" paginatorPosition="bottom">
<p:column headerText="Código" style="text-align: center; width: 100px">
<h:outputText value="#{fabricante.codigo}" />
</p:column>
<p:column headerText="Nome">
<h:outputText value="#{fabricante.nome}" />
</p:column>
<p:column style="width: 100px; text-align: center" headerText="Editar / Excluir">
<p:button outcome="cadastroFabricante" icon="ui-icon-pencil" title="Editar">
<f:param name="fabricante" value="#{fabricante.codigo}" />
</p:button>
<p:commandButton icon="ui-icon-trash" title="Excluir" oncomplete="PF('confirmacaoExclusao').show()"
process="@this" update=":frmPesquisa:confirmacaoExclusaoDialog">
<f:setPropertyActionListener value="#{fabricante}" target="#{consultaFabricante.fabricanteSelecionado}" />
</p:commandButton>
</p:column>
</p:dataTable>
<p:confirmDialog header="Exclusão de fabricante" id="confirmacaoExclusaoDialog"
widgetVar="confirmacaoExclusao" message="Tem certeza que deseja excluir o fabricante "
severity="alert">
<p:button value="Não" onclick="PF('confirmacaoExclusao').hide(); return false;" />
<p:commandButton value="Sim" update=":frmPesquisa:fabricantesTable" oncomplete="PF('confirmacaoExclusao').hide()"
action="#{consultaFabricante.excluir}" />
</p:confirmDialog>
</h:form>
</ui:define>
</ui:composition>
Minha página que possui o formulário.
<ui:composition template="/WEB-INF/template/layoutPadrao.xhtml"
xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:f="http://xmlns.jcp.org/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:p="http://primefaces.org/ui"
xmlns:o="http://omnifaces.org/ui">
<ui:define name="titulo">Pesquisa de Fabricantes</ui:define>
<ui:define name="corpo">
<f:metadata>
<f:viewAction action="#{consultaFabricante.inicializa}" />
</f:metadata>
<h1>Pesquisa de Fabricantes</h1>
<h:form id="frmPesquisa">
<p:messages id="messages" autoUpdate="true" closable="true" />
<p:toolbar style="margin-top: 20px">
<p:toolbarGroup>
<p:button value="Novo" />
</p:toolbarGroup>
</p:toolbar>
<p:dataTable id="fabricantesTable" value="#{consultaFabricante.fabricantes}" var="fabricante"
style="margin-top: 20px" emptyMessage="Nenhum fabricante encontrado." rows="20"
paginator="true" paginatorAlwaysVisible="false" paginatorPosition="bottom">
<p:column headerText="Código" style="text-align: center; width: 100px">
<h:outputText value="#{fabricante.codigo}" />
</p:column>
<p:column headerText="Nome">
<h:outputText value="#{fabricante.nome}" />
</p:column>
<p:column style="width: 100px; text-align: center" headerText="Editar / Excluir">
<p:button outcome="cadastroFabricante" icon="ui-icon-pencil" title="Editar">
<f:param name="fabricante" value="#{fabricante.codigo}" />
</p:button>
<p:commandButton icon="ui-icon-trash" title="Excluir" oncomplete="PF('confirmacaoExclusao').show()"
process="@this" update=":frmPesquisa:confirmacaoExclusaoDialog">
<f:setPropertyActionListener value="#{fabricante}" target="#{consultaFabricante.fabricanteSelecionado}" />
</p:commandButton>
</p:column>
</p:dataTable>
<p:confirmDialog header="Exclusão de fabricante" id="confirmacaoExclusaoDialog"
widgetVar="confirmacaoExclusao" message="Tem certeza que deseja excluir o fabricante "
severity="alert">
<p:button value="Não" onclick="PF('confirmacaoExclusao').hide(); return false;" />
<p:commandButton value="Sim" update=":frmPesquisa:fabricantesTable" oncomplete="PF('confirmacaoExclusao').hide()"
action="#{consultaFabricante.excluir}" />
</p:confirmDialog>
</h:form>
</ui:define>
</ui:composition>
Meu Converter
package com.irmaosuspension.controleservico.converter;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;
import javax.inject.Inject;
import com.irmaosuspension.controleservico.dao.FabricanteDAO;
import com.irmaosuspension.controleservico.modelo.Fabricante;
@FacesConverter(forClass=Fabricante.class)
public class FabricanteConverter implements Converter{
@Inject
private FabricanteDAO dao;
public Object getAsObject(FacesContext arg0, UIComponent arg1, String value) {
Fabricante fabricante = null;
if(value != null){
fabricante = dao.buscarPeloCodigo(new Long(value));
}
return fabricante;
}
public String getAsString(FacesContext arg0, UIComponent arg1, Object value) {
if(value != null){
Long code = ((Fabricante) value).getCodigo();
return code == null ? null : code.toString();
}
return "";
}
}
Meu DAO
package com.irmaosuspension.controleservico.dao;
import java.io.Serializable;
import java.util.List;
import javax.inject.Inject;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceException;
import com.irmaosuspension.controleservico.jpa.Transactional;
import com.irmaosuspension.controleservico.modelo.Fabricante;
import com.irmaosuspension.controleservico.util.NegocioException;
public class FabricanteDAO implements Serializable {
private static final long serialVersionUID = 1L;
@Inject
private EntityManager em;
public void salvar(Fabricante fabricante) {
em.merge(fabricante);
}
public List<Fabricante> buscarTodos() {
return em.createQuery("from Fabricante", Fabricante.class).getResultList();
}
@Transactional
public void excluir(Fabricante fabricante) throws NegocioException {
try {
Fabricante fabricanteTemp = this.buscarPeloCodigo(fabricante.getCodigo());
em.remove(fabricanteTemp);
em.flush();
} catch (PersistenceException e) {
throw new NegocioException("Fabricante não pode ser excluído.");
}
}
public Fabricante buscarPeloCodigo(Long codigo) {
return em.find(Fabricante.class, codigo);
}
}
Error quando recupero o Objeto
GRAVE: Servlet.service() for servlet [Faces Servlet] in context with path [/suspension] threw exception [null] with root cause
java.lang.NullPointerException
at com.irmaosuspension.controleservico.converter.FabricanteConverter.getAsObject(FabricanteConverter.java:22)
at com.sun.faces.renderkit.html_basic.HtmlBasicInputRenderer.getConvertedValue(HtmlBasicInputRenderer.java:171)
at javax.faces.component.UIViewParameter.getConvertedValue(UIViewParameter.java:437)
at javax.faces.component.UIInput.validate(UIInput.java:975)
at javax.faces.component.UIInput.executeValidate(UIInput.java:1248)
at javax.faces.component.UIInput.processValidators(UIInput.java:712)
at javax.faces.component.UIViewParameter.processValidators(UIViewParameter.java:278)
at javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1261)
at javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1261)
at javax.faces.component.UIViewRoot.processValidators(UIViewRoot.java:1195)
at com.sun.faces.lifecycle.ProcessValidationsPhase.execute(ProcessValidationsPhase.java:76)
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:198)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:658)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:230)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:474)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:624)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:349)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:495)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:767)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1347)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:745)