Galera, estou com o seguinte erro :javax.servlet.ServletException: /Incluir.xhtml @14,27 value="#{clienteMB.cliente.nome}": Target Unreachable, ‘cliente’ returned null
javax.faces.webapp.FacesServlet.service(FacesServlet.java:606)
Mas possuo o managed bean clienteMB e tb a classe ja instanciada cliente com o get e set nome.
segue managed bean
package managedbean;
import java.util.ArrayList;
import java.util.List;
import ejb.ClienteBean;
import entidade.Cliente;
@ManagedBean (name = “clienteMB” )
@ViewScoped
public class ClienteMB {
private Cliente cliente = new Cliente();
private List<Cliente> lstCliente = new ArrayList<Cliente>();
public String incluirCliente() {
InitialContext ini;
try {
ini = new InitialContext();
ClienteBean clienteBean = (ClienteBean)ini.lookup("java:module/ClienteBean");
clienteBean.salvar(cliente);
cliente = new Cliente();
lstCliente = clienteBean.buscarClientes();
} catch (NamingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
public Cliente getCliente() {
return cliente;
}
public void setCliente(Cliente cliente) {
this.cliente = cliente;
}
public List<Cliente> getLstCliente() {
return lstCliente;
}
public void setLstCliente(List<Cliente> lstCliente) {
this.lstCliente = lstCliente;
}
}
segue xhtml
<h:head><title>Minha Aplicação</title></h:head>
<h:body>
<h:form>
<h:outputLabel value="Nome: " for="nome" />
<h:inputText id="nome" value="#{clienteMB.cliente.nome}" required="true" requiredMessage="Campo obrigatório"
maxlength="200"/>
<h:commandButton value="Salvar" action="#{clienteMB.incluirCliente()}" />
</h:form>
<h:dataTable id="tabela" value="#{clienteMB.lstCliente}" var="item">
<h:column>
<f:facet name="header">
<h:outputText value="ID" />
</f:facet>
<h:outputText value="#{item.id}"/>
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="Nome" />
</f:facet>
<h:outputText value="#{item.nome}"/>
</h:column>
</h:dataTable>
</h:body>
Aguem pode me ajudar?
Obrigado
coloque o código dentro da tag code
fica mais fácil e visualizar, blz
o erro acontece ao renderizar a página?
É estranho dar Target Unreachable no cliente, mas tenta colocar no managed bean
private Cliente cliente;
no lugar de
private Cliente cliente = new Cliente();
e ve se funciona assim.
Boa. tarde
felipeaqueiroz , fiz o que vc disse e não funcionou, o erro é quando eu clico no botão salvar, que executa o metodo incluirCLiente do managedbean.
Muda seu codigo e poe esse aqui :
Bean:
package managedbean;
import java.util.ArrayList;
import java.util.List;
import ejb.ClienteBean;
import entidade.Cliente;
@ManagedBean (name = "clienteMB" )
@ViewScoped
public class ClienteMB {
private Cliente cliente;
private List<Cliente> lstCliente = new ArrayList<Cliente>();
private String nome;
public String incluirCliente() {
InitialContext ini;
try {
ini = new InitialContext();
ClienteBean clienteBean = (ClienteBean)ini.lookup("java:module/ClienteBean");
cliente = new Cliente();
cliente.setNome(nome);
clienteBean.salvar(cliente);
lstCliente = clienteBean.buscarClientes();
} catch (NamingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
public Cliente getCliente() {
return cliente;
}
public void setCliente(Cliente cliente) {
this.cliente = cliente;
}
public List<Cliente> getLstCliente() {
return lstCliente;
}
public void setLstCliente(List<Cliente> lstCliente) {
this.lstCliente = lstCliente;
}
}
Não esqueça de gerar os getters e setters.
xhtml:
<!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:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets">
<h:head><title>Minha Aplicação</title></h:head>
<h:body>
<h:form>
<h:outputLabel value="Nome: " for="nome" />
<h:inputText id="nome" value="#{clienteMB.nome}" required="true" requiredMessage="Campo obrigatório"
maxlength="200"/>
<h:commandButton value="Salvar" action="#{clienteMB.incluirCliente()}" />
</h:form>
<h:dataTable id="tabela" value="#{clienteMB.lstCliente}" var="item">
<h:column>
<f:facet name="header">
<h:outputText value="ID" />
</f:facet>
<h:outputText value="#{item.id}"/>
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="Nome" />
</f:facet>
<h:outputText value="#{item.nome}"/>
</h:column>
</h:dataTable>
</h:body>
</html>
dofun12, assim o XHTML não acha o atributo nome no managed bean, pq sera?
Voce pode ter esquecido de criar os getter and setters.
Verifica se no seu código tem esse trecho :
public String getNome() {
return nome;
}
public void setNome(String nome) {
this.nome = nome;
}