Galera, estou fazendo um exemplo de uma apostila mas está apresentando erro. Será que alguém pode me ajudar?! É assim, o usuário cadastra o nome, endereço e sexo e os dados cadastrados irão aparecer em uma tabela na mesma página.
MBean 1package br.com.agenda;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
@ManagedBean(name = "contato")
@SessionScoped
public class Contato {
private String nome;
private String sexo;
private String endereco;
public String getNome() {
return this.nome;
}
public void setNome(String nome) {
this.nome = nome;
}
public String getEndereco() {
return this.endereco;
}
public void setEndereco(String endereco) {
this.endereco = endereco;
}
public String getSexo() {
return this.sexo;
}
public void setSexo(String sexo) {
this.sexo = sexo;
}
}
package br.com.agenda;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
@ManagedBean(name = "agendaContatoBean")
@SessionScoped
public class AgendaContatoBean {
@ManagedProperty(value = "#{contato}")
private Contato contato;
private List<Contato> contatos;
public AgendaContatoBean() {
this.setContatos(new ArrayList<Contato>());
this.setContato(new Contato());
}
public void incluirContato(ActionEvent event) {
this.getContatos().add(this.getContato());
this.setContato(new Contato());
}
public List<Contato> getContatos() {
return this.contatos;
}
public void setContatos(List<Contato> contatos) {
this.contatos = contatos;
}
public Contato getContato() {
return this.contato;
}
public void setContato(Contato contato) {
this.contato = contato;
}
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html lang="pt-br" xmlns:f="http://java.sun.com/jsf/core" xmlns:h="http://java.sun.com/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets" xmlns:c="http://java.sun.com/jsp/jstl/core"
>
<head>
<title>Agenda Contatos</title>
</head>
<body>
<f:view>
<h:form>
<h:outputText value="Nome: " />
<h:inputText value="#{agendaContatoBean.contato.nome}"/>
<br />
<h:outputText value="Endereço: " />
<h:inputText value="#{agendaContatoBean.contato.endereco }" />
<br />
<h:outputText value="Sexo: " />
<h:inputText value="#{agendaContatoBean.contato.sexo }" />
<br />
<h:commandButton actionListener="#{agendaContatoBean.incluirContato}" value="Incluir" />
<br />
<br />
<h:dataTable var="obj" value="#{agendaContatoBean.contatos }" border="1" width="100%">
<h:column>
<f:facet name="header">
<h:outputText value="Nome" />
</f:facet>
<h:outputText value="#{obj.nome}" />
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="Endereço" />
</f:facet>
<h:outputText value="#{obj.endereco}" />
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="Sexo" />
</f:facet>
<h:outputText value="#{obj.sexo}" />
</h:column>
</h:dataTable>
</h:form>
</f:view>
</body>
</html>
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5"
>
<display-name>JavaServerFaces</display-name>
<!-- Change to "Production" when you are ready to deploy -->
<context-param>
<param-name>javax.faces.PROJECT_STAGE</param-name>
<param-value>Development</param-value>
</context-param>
<welcome-file-list>
<welcome-file>faces/agendaContato.xhtml</welcome-file>
</welcome-file-list>
<!-- JSF mapping -->
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- Map these files with JSF -->
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>/faces/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.jsf</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.faces</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
</web-app>
Agora, o erro que dá é esse:
javax.el.PropertyNotFoundException: Target Unreachable, identifier 'agendaContatoBean' resolved to nullviewId=/agendaContato.xhtml
location=D:\Workspace Eclipse Windows\.metadata\.plugins\org.eclipse.wst.server.core\tmp1\wtpwebapps\SisGemat\agendaContato.xhtml
phaseId=PROCESS_VALIDATIONS(3)Caused by:
javax.el.PropertyNotFoundException - Target Unreachable, identifier 'agendaContatoBean' resolved to null
at org.apache.el.parser.AstValue.getTarget(AstValue.java:98)/agendaContato.xhtml at line 16 and column 60 value="#{agendaContatoBean.contato.nome}"
Já procurei bastante pra ver qual seria o problema, mas pelo que li não consegui achar o problema. Valeu pela ajuda, pessoal!
