Pessoal, tô com uma dúvida.
Toda vez que eu tento rodar esta página dá o seguinte erro:
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>Cadastro de Turma</title>
</head>
<f:view>
<h2>Cadastrar Turma</h2>
<h:form id="form1">
<h:panelGrid>
<h:outputLabel value="Número:" for="numero" />
<h:inputText id="numero" value="#{estudoMBean.turma.numero}" />
</h:panelGrid>
<h:commandButton action="estudoMBean.cadastrarTurma" value="Cadsatrar Turma" />
</h:form>
<h:message for="form1" />
</f:view>
</html>
erro:
exception
org.apache.jasper.JasperException: An exception occurred processing JSP page /cadastrar.jsp at line 8
5: <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
6: <title>Cadastro de Turma</title>
7: </head>
8: <f:view>
9: <h2>Cadastrar Turma</h2>
10: <h:form id="form1">
11: <h:panelGrid>
Stacktrace:
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:521)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:430)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
root cause
java.lang.RuntimeException: Cannot find FacesContext
javax.faces.webapp.UIComponentClassicTagBase.getFacesContext(UIComponentClassicTagBase.java:1855)
javax.faces.webapp.UIComponentClassicTagBase.setJspId(UIComponentClassicTagBase.java:1672)
org.apache.jsp.cadastrar_jsp._jspx_meth_f_005fview_005f0(cadastrar_jsp.java:108)
org.apache.jsp.cadastrar_jsp._jspService(cadastrar_jsp.java:83)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:388)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
note The full stack trace of the root cause is available in the Apache Tomcat/6.0.35 logs.
o que pode ser?
Qual url você está chamando sua aplicação? E como você configurou no web.xml o contexto do JSF?
Aqui está o faces-config:
<?xml version="1.0" encoding="UTF-8"?>
<faces-config
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_2_1.xsd"
version="2.1">
<managed-bean>
<managed-bean-name>estudoMBean</managed-bean-name>
<managed-bean-class>managed.ManagedBean</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
</managed-bean>
</faces-config>
Aqui está o managedBean:
package managed;
import java.util.ArrayList;
import java.util.List;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;
import persistence.AlunoDao;
import persistence.TurmaDao;
import entity.Aluno;
import entity.Turma;
public class ManagedBean {
private Aluno aluno;
private Turma turma;
private List<SelectItem> listaTurma;
private List<Aluno> listaAlunos;
public ManagedBean() {
@SuppressWarnings("unused")
Aluno aluno = new Aluno();
@SuppressWarnings("unused")
Turma turma = new Turma();
}
public Aluno getAluno() {
return aluno;
}
public void setAluno(Aluno aluno) {
this.aluno = aluno;
}
//Listar turmas com seus respectivos cursos
public List<SelectItem> getListaTurma() {
try {
listaTurma = new ArrayList<SelectItem>();
for(Turma t : new TurmaDao().findAll()){
listaTurma.add(new SelectItem(t.getNumero(), t.getCurso()));
}
} catch (Exception e) {
e.printStackTrace();
}
return listaTurma;
}
public void setListaTurma(List<SelectItem> listaTurma) {
this.listaTurma = listaTurma;
}
//Listar alunos e seus respectivos atributos
public List<Aluno> getListaAlunos() {
try {
listaAlunos = new ArrayList<Aluno>();
for(Aluno a : new AlunoDao().findAll()){
listaAlunos.add(a);
}
} catch (Exception e) {
e.printStackTrace();
}
return listaAlunos;
}
public void setListaAlunos(List<Aluno> listaAlunos) {
this.listaAlunos = listaAlunos;
}
public Turma getTurma() {
return turma;
}
public void setTurma(Turma turma) {
this.turma = turma;
}
//Cadastrar as Turmas e seus cursos
public String cadastrarTurma(){
FacesContext fc = FacesContext.getCurrentInstance();
try {
new TurmaDao().salvar(turma);
fc.addMessage("form1", new FacesMessage("Gravado com sucesso!"));
@SuppressWarnings("unused")
Turma turma = new Turma();
} catch (Exception e) {
fc.addMessage("form", new FacesMessage("Erro:" + e.getMessage()));
}
return "cadastrar.jsf";
}
//Cadastrar alunos em uma turma
public String cadastrarAluno(){
FacesContext fc = FacesContext.getCurrentInstance();
try {
aluno.setTurma(turma);
new AlunoDao().savar(aluno);
fc.addMessage("form1", new FacesMessage("Gravado com sucesso!"));
@SuppressWarnings("unused")
Aluno aluno = new Aluno();
} catch (Exception e) {
fc.addMessage("form", new FacesMessage("Erro:" + e.getMessage()));
}
return "cadastrar.jsf";
}
}
Só não entendi o que vc quis dizer sobre a url.
Mano, eu preciso de como você configurou no web.xml a aplicação.
E por qual URL você está acessando seu projeto.
Web.XML:
<?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>estudandoJsf</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.jsf</url-pattern>
</servlet-mapping>
</web-app>
Url:
http://localhost:8080/estudandoJsf/cadastrar.jsp
Ahhhhh! Entendi. A url não pode ser cadastrar.jsp e sim cadastrar.jsf. Na verdade o ciclo de vida de um jsf começa na segunda página, certo? Então
eu crio uma página index.jsp e redireciono para cadastrar.jsf. Me corrija se eu estiver errado, por que aqui já funfou. Muito obrigado mano por abrir minha mente. Abraços.
Essa foi fácil. Inté +! \o_
Só mais uma coisa.
Quando eu preencho o formulário, é lançada ma exception dizendo que turma não está acessível e retorna null:
type Exception report
message
description The server encountered an internal error () that prevented it from fulfilling this request.
exception
javax.servlet.ServletException: /cadastrar.jsp(13,4) '#{estudoMBean.turma.numero}' Target Unreachable, 'turma' returned null
javax.faces.webapp.FacesServlet.service(FacesServlet.java:325)
root cause
org.apache.jasper.el.JspPropertyNotFoundException: /cadastrar.jsp(13,4) '#{estudoMBean.turma.numero}' Target Unreachable, 'turma' returned null
org.apache.jasper.el.JspValueExpression.getType(JspValueExpression.java:61)
com.sun.faces.renderkit.html_basic.HtmlBasicInputRenderer.getConvertedValue(HtmlBasicInputRenderer.java:95)
javax.faces.component.UIInput.getConvertedValue(UIInput.java:1008)
javax.faces.component.UIInput.validate(UIInput.java:934)
javax.faces.component.UIInput.executeValidate(UIInput.java:1189)
javax.faces.component.UIInput.processValidators(UIInput.java:691)
javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1080)
javax.faces.component.UIForm.processValidators(UIForm.java:243)
org.ajax4jsf.component.AjaxViewRoot$3.invokeContextCallback(AjaxViewRoot.java:439)
org.ajax4jsf.component.AjaxViewRoot.processPhase(AjaxViewRoot.java:238)
org.ajax4jsf.component.AjaxViewRoot.processValidators(AjaxViewRoot.java:455)
com.sun.faces.lifecycle.ProcessValidationsPhase.execute(ProcessValidationsPhase.java:76)
com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
javax.faces.webapp.FacesServlet.service(FacesServlet.java:312)
note The full stack trace of the root cause is available in the Apache Tomcat/6.0.35 logs.
Se está retornando null é pq você deve criar uma instância do objeto antes de retornar.
Ou você faz um if ( meuOb == null ) meuOb = new …
Ou então cria um método, anota com @PostConstruct e faz meuOb = new … lá.