1

这是 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_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>Projeto_joas</display-name>

<resource-ref>
<res-ref-name>jdbc/DiaDiaDev</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>


<servlet>
<display-name>FacesServlet</display-name>
<servlet-name>FacesServlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>FacesServlet</servlet-name>
<url-pattern>*.jsf</url-pattern>
</servlet-mapping>

<context-param>
<param-name>javax.faces.PROJECT_STAGE</param-name>
<param-value>Development</param-value>
 </context-param>


<context-param>
  <param-name>com.sun.faces.expressionFactory</param-name>
     <param-value>com.sun.el.ExpressionFactoryImpl</param-value>
</context-param>


<!-- <filter> -->

<!--<filter-name>FilterHibernate</filter-name> -->
<!--<filter-class>util.filter.ConexaoHibernateFilter</filter-class> -->
<!--</filter> -->

<!--<filter-mapping> -->
<!-- <filter-name>FilterHibernate</filter-name> -->
<!--<url-pattern>*.jsf</url-pattern> -->

<!--</filter-mapping> -->
</web-app>

服务器以这种方式运行得很好,如果我删除过滤器上的注释,它就不会启动并且会抛出这些异常:

严重:子容器在启动 java.util.concurrent.ExecutionException 期间失败:org.apache.catalina.LifecycleException:无法启动组件 [StandardEngine[Catalina].StandardHost[localhost]] 原因:org.apache.catalina.LifecycleException:无法启动组件 [StandardEngine[Catalina].StandardHost[localhost]] 原因:org.apache.catalina.LifecycleException:子容器在启动期间失败

警告:无法检索容器 [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/Projeto_joas]] 的 JNDI 命名上下文,因此没有对该容器执行清理 javax.naming.NamingException:没有命名上下文绑定到此类加载器

过滤器类:

package util.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

import org.hibernate.SessionFactory;

import util.HibernateUtil;

public class ConexaoHibernateFilter implements Filter {

    private SessionFactory sf;

    @Override
    public void init(FilterConfig arg0) throws ServletException {
        this.sf = HibernateUtil.getSessionFactory();

    }




    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse,
            FilterChain chain) throws IOException, ServletException {

        try {

            this.sf.getCurrentSession().beginTransaction();
            chain.doFilter(servletRequest, servletResponse);
            this.sf.getCurrentSession().getTransaction().commit();
            this.sf.getCurrentSession().close();

        } catch (Throwable ex) {
            try {
                if (this.sf.getCurrentSession().getTransaction().isActive()) {
                    this.sf.getCurrentSession().getTransaction().rollback();
                }
            } catch (Throwable t) {
                t.printStackTrace();
            }
            throw new ServletException(ex);
        }

    }

    @Override
    public void destroy() {
        // TODO Auto-generated method stub

    }

}
4

0 回答 0