此过滤器的功能就是让Session始终保持着一个打开的状态:
/** * @Title: OpenSessionFilter.java * @Package org.web * @Description: TODO该方法的主要作用: * @author A18ccms A18ccms_gmail_com * @date 2017-4-19 下午6:51:37 * @version V1.0 */ package org.web; 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.Session; import org.hibernate.Transaction; import org.util.HibernateUtil; /** * * 项目名称:struts_easyui * 类名称:OpenSessionFilter * 类描述: * 创建人:Mu Xiongxiong * 创建时间:2017-4-19 下午6:51:37 * 修改人:Mu Xiongxiong * 修改时间:2017-4-19 下午6:51:37 * 修改备注: * @version * */ public class OpenSessionFilter implements Filter { /**(非 Javadoc) * <p>Title: destroy</p> * <p>Description(描述): </p> * @see javax.servlet.Filter#destroy() */ @Override public void destroy() { // TODO Auto-generated method stub } /**(非 Javadoc) * <p>Title: doFilter</p> * <p>Description(描述): </p> * @param arg0 * @param arg1 * @param arg2 * @throws IOException * @throws ServletException * @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest, javax.servlet.ServletResponse, javax.servlet.FilterChain) */ @Override public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain arg2) throws IOException, ServletException { Session session =null; Transaction tx=null; try { session =HibernateUtil.getSession(); tx=session.beginTransaction(); arg2.doFilter(arg0, arg1); tx.commit(); } catch (Exception e) { tx.rollback(); } } /**(非 Javadoc) * <p>Title: init</p> * <p>Description(描述): </p> * @param arg0 * @throws ServletException * @see javax.servlet.Filter#init(javax.servlet.FilterConfig) */ @Override public void init(FilterConfig arg0) throws ServletException { // TODO Auto-generated method stub } }