0
I’m new to Java and I’m having trouble showing on the page a grid with data from a database table. I am using java + Hibernate + JSF 2 + Tomcat 8. When I run my xhtml page it presents the error below. The problem is in the xhtml form list, because the form edition runs smoothly.I’ve been trying to solve for a long time and I can’t evolve.
Error
javax.servlet.Servletexception: javax/Enterprise/context/spi/Contextual Apr 11, 2016 11:11:04 PM org.apache.Catalina.core.Standardwrappervalve invoke SEVERE: Servlet.service() for Servlet [Faces Servlet] in context with path [/] threw Exception [null] with root cause javax.servlet.Servletexception
Filter Code
@Override
public void doFilter(ServletRequest servletFilter, ServletResponse servletResponse, FilterChain chain)
throws IOException, ServletException {
try { //trata exceção caso dê alguma coisa errada
/*O processamento inicial passa por este método*/
this.sf.getCurrentSession().beginTransaction(); //pega a sessão corrente e inicia uma transação
chain.doFilter(servletFilter, servletResponse); //passa o filter e o response para o filter chain
this.sf.getCurrentSession().getTransaction().commit(); //dá um comite na transação aberta acima
this.sf.getCurrentSession().close(); //fecha sessão corrente
} catch (Throwable ex) {
try { // trata o rollback
//verifica o status da transação e se estiver ativa dá um rollback
System.out.println("Deu exceção 1: " + ex);
if (this.sf.getCurrentSession().getTransaction().getStatus() == TransactionStatus.ACTIVE) {
this.sf.getCurrentSession().getTransaction().rollback();
}
} catch (Throwable t) {
System.out.println("Deu exceção 2: " + t);
t.printStackTrace(); //joga a exceção do rollback e imprime na tela
}
throw new ServletException(); /*Joga a exceção*/
}
}
XHTML code
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html">
<ui:composition template="/templates/template.xhtml">
<ui:define name="pageTitle">Cidade</ui:define>
<ui:define name="pageHeader">Cadastro de Cidades</ui:define>
<ui:define name="body">
<h:form id="edicao">
<h:messages /> <!-- viabiliza as mensagens na tela -->
<h:panelGrid columns = "2">
<h:outputLabel value="Nome" />
<h:inputText value="#{cidadeBean.cidadeSelecionado.nome}" />
<h:outputLabel value="Estado" />
<h:inputText value="#{cidadeBean.cidadeSelecionado.estado}" />
</h:panelGrid>
<h:commandButton action="#{cidadeBean.salvar}" value="Salvar" />
</h:form>
<h:form id="lista">
<h:messages />
<h:dataTable value="#{cidadeBean.lista}" var="cidade" border = "1">
<f:facet name="header">Listagem de Cidades</f:facet>
<h:column>
<f:facet name="header">Nome</f:facet>
#{cidade.nome}
</h:column>
<h:column>
<f:facet name="header">Estado</f:facet>
#{cidade.estado}
</h:column>
</h:dataTable>
</h:form>
</ui:define>
</ui:composition>
Code of the Citadel
package oneTrade.cidade;
import java.util.ArrayList;
import java.util.List;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
@ManagedBean(name = "cidadeBean")
@RequestScoped //vai ser do tipo request pois só vai enviar o formulário
public class CidadeBean {
private Cidade cidadeSelecionado = new Cidade();
private List<Cidade> lista = null;
public void salvar(){
CidadeRN cidadeRN = new CidadeRN();
cidadeRN.salvar(cidadeSelecionado);
FacesMessage faces = new FacesMessage("Cidade Cadastrada com sucesso");
FacesContext contexto = FacesContext.getCurrentInstance();
contexto.addMessage(null, faces);
}
public Cidade getCidadeSelecionado() {
return cidadeSelecionado;
}
public void setCidadeSelecionado(Cidade cidadeSelecionado) {
this.cidadeSelecionado = cidadeSelecionado;
}
public List<Cidade> getLista() {
CidadeRN cidadeRN = new CidadeRN();
if (lista == null){
lista = cidadeRN.listar();
}
return lista;
}
}
Code - Cityohibernate.java
package oneTrade.cidade;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.Session;
public class CidadeDAOHibernate implements CidadeDAO {
private Session sessao;
public Session getSessao() {
return sessao;
}
public void setSessao(Session sessao) {
this.sessao = sessao;
}
@Override
public void salvar(Cidade cidade) {
this.sessao.save(cidade);
}
@SuppressWarnings("unchecked")
@Override
public List<Cidade> listar() {
Criteria lista = sessao.createCriteria(Cidade.class);
return lista.list();
}
}
Follow console error
Deu exceção 1: javax.servlet.ServletException: javax/enterprise/context/spi/Contextual
abr 13, 2016 9:21:49 PM org.hibernate.engine.internal.StatisticalLoggingSessionEventListener end
INFO: Session Metrics {
1254927 nanoseconds spent acquiring 1 JDBC connections;
0 nanoseconds spent releasing 0 JDBC connections;
0 nanoseconds spent preparing 0 JDBC statements;
0 nanoseconds spent executing 0 JDBC statements;
0 nanoseconds spent executing 0 JDBC batches;
0 nanoseconds spent performing 0 L2C puts;
0 nanoseconds spent performing 0 L2C hits;
0 nanoseconds spent performing 0 L2C misses;
0 nanoseconds spent executing 0 flushes (flushing a total of 0 entities and 0 collections);
0 nanoseconds spent executing 0 partial-flushes (flushing a total of 0 entities and 0 collections)
}
abr 13, 2016 9:21:49 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [Faces Servlet] in context with path [/oneTrade] threw exception [null] with root cause
javax.servlet.ServletException
at oneTrade.util.ConexaoHibernateFilter.doFilter(ConexaoHibernateFilter.java:48)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilt erChain.java:240)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:212)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:141)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:522)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1095)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:672)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1500)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1456)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Unknown Source)
If possible, put the error that appears on the console.
– adelmo00
Thank you for responding adelmo00. I complemented at the end of the post acim the error that appears on the console.
– raoliv
Some attribute is null, it is fact. Debug your code and check which attribute of the filter is null.
– adelmo00
Adelmo00 doFilter has two parameters (servletFilter and Servlet Response), both of which have values. There are some attributes within the two parameters that are null, for example: in servletFilter the parameters: asyncContext, authtype, cookies, Event, localAddr, localName, Parts,.
– raoliv
This is where the error occurs: chain.doFilter(servletFilter, servletResponse); //passes the filter and the Response to the filter chain ??
– adelmo00
Adelmo, it is really in this line that the problems are occurring, but I do not know why the error occurs. I’ve debugged the doFilter method and I can’t find the problem.
– raoliv