Hibernate 配置一个过滤器CloseSessionFilter 不知道咋弄啊 救命救命啊
ClossSessionFilter
package com.sdut.dao.hibernate;
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 javax.servlet.http.*;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public class CloseSessionFilter implements Filter {
Log logger=LogFactory.getLog(this.getClass());
protected FilterConfig config;
//初始化方法
public void init(FilterConfig arg0) throws
ServletException{
this.config=arg0;
}
//doFilter方法,定义操作
public void doFilter(ServletRequest request,ServletResponse response,FilterChain chain)throws
IOException,ServletException{
try{
chain.doFilter((HttpServletRequest)request,(HttpServletResponse)response);
}finally{
try{
HibernateUtil.commitTransaction();
}catch(Exception e){
HibernateUtil.rollbackTransaction();
}finally{
HibernateUtil.closeSession();
}
}
}
//destroy方法
public void destroy(){
this.config=null;
}
}
<filter>
<filter-name>CloseSessionFilter</filter-name>
<filter-class>com.sdut.dao.hibernate.ClossSessionFilter</filter-class>
下面我就不会配置了
应该有<init-param></init-param> 和<filter-mapping></filter-mapping>吧
麻烦 前辈们给指点下怎么配置 在Web.xml中 还是在Hibernate.cfg.xml中啊 呵呵 原谅一下菜鸟
拜托了 谢谢大家了!!
------解决方案--------------------
我也不会 只好帮你顶一下了
------解决方案--------------------
filter配置,不在web.xml中配置,在哪?
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
<!--
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
-->
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>