estou tentando editar meu objeto, só que ao tentar editar consigo recuperar o objeto pelo ID. Ou seja, ele recupera da URI http://localhost:8080/suspension/carro/cadastroCarro.xhtml?carro=6 Só que não monta as informações no meu formulário, fiz um converter mais não consegui.
<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">
<f:metadata>
<f:viewAction action="#{consultaCarroBean.inicializa}" />
</f:metadata>
<ui:define name="titulo">Pesquisa de Carro</ui:define>
<ui:define name="corpo">
<h1>Pesquisa de Veículos</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" outcome="cadastroCarro" />
</p:toolbarGroup>
</p:toolbar>
<p:dataTable id="carrosTable" value="#{consultaCarroBean.carros}" var="carro" style="margin-top: 20px"
emptyMessage="Nenhum carro encontrado." rows="5"
paginator="true" paginatorAlwaysVisible="false" paginatorPosition="bottom"
lazy="true" rowsPerPageTemplate="5,10,15">
<p:column headerText="Código" style="text-align: center; width: 100px">
<h:outputText value="#{carro.codigo}" style="text-align: center" />
</p:column>
<p:column headerText="Modelo" style="text-align: center; width: 150px">
<h:outputText value="#{carro.modeloCarro.nome}" />
</p:column>
<p:column headerText="Placa" style="text-align: center; width: 100px">
<h:outputText value="#{carro.placa}" />
</p:column>
<p:column headerText="Cor" style="text-align: center; width: 100px">
<h:outputText value="#{carro.cor}" />
</p:column>
<p:column headerText="Ano" style="text-align: center; width: 100px">
<h:outputText value="#{carro.ano}" />
</p:column>
<p:column style="width: 100px; text-align: center" headerText="Editar / Excluir" >
<p:button outcome="cadastroCarro" icon="ui-icon-pencil" title="Editar">
<f:param name="carro" value="#{carro.codigo}" />
</p:button>
-
<p:commandButton icon="ui-icon-trash" title="Excluir" oncomplete="PF('confirmacaoExclusao').show()"
process="@this" update=":frmPesquisa:confirmacaoExclusaoDialog">
<f:setPropertyActionListener value="#{carro}" target="#{consultaCarroBean.carroselecionado}" />
</p:commandButton>
</p:column>
</p:dataTable>
<p:confirmDialog id="confirmacaoExclusaoDialog" widgetVar="confirmacaoExclusao"
message="Tem certeza que deseja excluir o carro de placa "
header="Exclusão de carro" severity="alert">
<p:button value="Não" onclick="PF('confirmacaoExclusao').hide(); return false;" />
<p:commandButton value="Sim" update=":frmPesquisa:carrosTable" process="@this" oncomplete="PF('confirmacaoExclusao').hide()"
action="#{consultaCarroBean.excluir}" />
</p:confirmDialog>
</h:form>
</ui:define>
</ui:composition>
Cadastro
<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">
<f:metadata>
<o:viewParam name="carros" value="#{cadastroCarroBean.carro}" />
<f:event listener="#{cadastroCarroBean.inicializar}" type="preRenderView" />
</f:metadata>
<ui:insert name="titulo">#{cadastroCarroBean.editando ? 'Editando Carro' : 'Novo Carro'}</ui:insert>
<ui:define name="corpo">
<h1>#{cadastroCarroBean.editando ? 'Editando Carro' : 'Novo Carro'}</h1>
<h:form id="frmCadastro" enctype="multipart/form-data">
<p:messages id="messages" autoUpdate="true" closable="true" />
<p:toolbar style="margin-top: 20px">
<p:toolbarGroup>
<p:commandButton value="Salvar" action="#{cadastroCarroBean.salvar}" id="botaoSalvar" update="frmCadastro" />
</p:toolbarGroup>
<p:toolbarGroup align="right">
<p:button value="Pesquisa" outcome="pesquisaCarro" />
</p:toolbarGroup>
</p:toolbar>
<p:panelGrid columns="2" id="painel" style="width: 100%" columnClasses="rotulo, campo">
<p:outputLabel value="Código" for="codigo" />
<p:inputText id="codigo" value="#{cadastroCarroBean.carro.codigo}" size="20" maxlength="20" readonly="true" />
<p:outputLabel value="Modelo Carro" for="modeloCarro" />
<p:selectOneMenu id="modeloCarro" value="#{cadastroCarroBean.carro.modeloCarro}" required="true">
<f:selectItem itemLabel="--escolha o carro--" noSelectionOption="true"/>
<f:selectItems value="#{cadastroCarroBean.modeloCarros}" var="modeloCarros" itemLabel="#{modeloCarros.nome}" itemValue="#{modeloCarros}"/>
</p:selectOneMenu>
<p:outputLabel value="placa" for="placa" />
<p:inputMask mask="aaa-9999" value="#{cadastroCarroBean.carro.placa}" size="11" style="text-transform:uppercase;" styleClass="campo"
id="placa" required="true" />
<p:outputLabel value="ano" for="ano" />
<p:inputMask mask="9999" value="#{cadastroCarroBean.carro.ano}" size="11" styleClass="campo" id="ano" required="true" />
<p:outputLabel value="cor" for="cor" />
<p:inputText maxlength="15" value="#{cadastroCarroBean.carro.cor}" size="11" styleClass="campo" id="cor" required="true">
<f:validateRegex pattern="[a-zA-Z]+" />
</p:inputText>
</p:panelGrid>
</h:form>
</ui:define>
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 com.irmaosuspension.controleservico.cdi.CDIServiceLocator;
import com.irmaosuspension.controleservico.dao.CarroDAO;
import com.irmaosuspension.controleservico.modelo.Carro;
@FacesConverter(forClass=Carro.class)
public class ConverterCarro implements Converter{
private CarroDAO dao;
public ConverterCarro() {
this.dao = CDIServiceLocator.getBean(CarroDAO.class);
}
public Object getAsObject(FacesContext arg0, UIComponent arg1, String value) {
Carro carro = null;
if(value != null && value !=""){
carro = this.dao.buscarPeloCodigo( new Long(value));
}
return carro;
}
public String getAsString(FacesContext arg0, UIComponent arg1, Object value) {
if(value != null){
Long code = ((Carro)value).getCodigo();
return code == null ? null : code.toString();
}
return "";
}
}