Bom dia estou tendo uma dificuldade ao fazer um exemplo que peguei na apostila da K19 sobre JPA2 e JSF 2.
O meu managedbean vem como null e ocorre o seguinte erro:
AVISO: /carros.xhtml @17,69 value="#{carroBean.carro.marca}": Target Unreachable, identifier ‘carroBean’ resolved to null
javax.el.PropertyNotFoundException: /carros.xhtml @17,69 value="#{carroBean.carro.marca}": Target Unreachable, identifier ‘carroBean’ resolved to null
Vou postar o código abaixo para quem puder me ajudar vou agradecer bastante.
Tenho minha classe Carro:
@Entity
public class Carro {
@Id
@GeneratedValue
private Long id;
private String marca;
private String modelo;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getMarca() {
return marca;
}
public void setMarca(String marca) {
this.marca = marca;
}
public String getModelo() {
return modelo;
}
public void setModelo(String modelo) {
this.modelo = modelo;
}
Minha Classe CarroBean:
@ManagedBean
public class CarroBean {
private Carro carro = new Carro();
public void adicionaCarro() {
EntityManager manager = this.getEntityManager();
CarroRepository repository = new CarroRepository(manager);
repository.adiciona(carro);
this.carro = new Carro();
}
public List<Carro> getCarros() {
EntityManager manager = this.getEntityManager();
CarroRepository repository = new CarroRepository(manager);
return repository.buscaTodos();
}
private EntityManager getEntityManager(){
FacesContext fc = FacesContext.getCurrentInstance();
ExternalContext ec = fc.getExternalContext();
HttpServletRequest request = (HttpServletRequest) ec.getRequest();
EntityManager manager = (EntityManager) request.getAttribute("EntityManager");
return manager;
}
public Carro getCarro() {
return carro;
}
public void setCarro(Carro carro) {
this.carro = carro;
}
e a página carros.xhtml
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core">
<h:head>
<title>Projeto Carros</title>
</h:head>
<h:body>
<h:form>
<h:panelGrid columns="2">
<h:outputLabel value="Marca: " for="campo-marca"/>
<h:inputText value="#{carroBean.carro.marca}" id="campo-marca" />
<h:outputLabel value="Modelo: " for="campo-modelo" />
<h:inputText value="#{carroBean.carro.modelo}" id="campo-modelo" />
<h:commandButton value="Adicionar" action="#{carroBean.adicionaCarro}"/>
</h:panelGrid>
</h:form>
</h:body>
Obrigado pela ajuda de todos.