Struts1.2+Spring2.5+Hibernate3.2框架搭建(四)
3.2. 配置 web.xml 文件
在web.xml文件中配置监听器以及web应用的初始化参数:
<!-- 向监听器类中传递初始化参数,以获取beanFactory对象 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!--
使用监听器初始化Spring的beanFactory,并将其放入到Context中,在使用时将其取出,以获取IOC容器中的bean
-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
在web.xml文件中配置Spring中的过滤器解决中文乱码问题
<!-- 使用Spring中的过滤器解决中文乱码问题 -->
<filter>
<filter-name>characterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true </param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
在web.xml文件中配置Spring中的过滤器解决hibernate延迟加载的问题
<!-- 解决hibernate延迟加载带来的异常,配置过滤器使Session在请求完成之后再关闭 -->
<filter>
<filter-name>OpenSessionInViewFilter</filter-name>
<filter-class>
com.zlk.util.OpenSessionInViewFilter</filter-class>
<init-param>
<param-name>singleSession</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>OpenSessionInViewFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
注:我重写了org.springframework.orm.hibernate3.support.OpenSessionInViewFilter类
下面是我重写的类:
① com.zlk.override.OpenSessionInViewFilter
package com.zlk.override;
import org.hibernate.FlushMode;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.dao.DataAccessResourceFailureException;
import org.springframework.orm.hibernate3.SessionFactoryUtils;
public class OpenSessionInViewFilter extends
org.springframework.orm.hibernate3.support.OpenSessionInViewFilter {
protected void closeSession(Session session, SessionFactory sessionFactory) {
// TODO Auto-generated method stub
session.flush();
super.closeSession(session, sessionFactory);
}
protected Session getSession(SessionFactory sessionFactory)
throws DataAccessResourceFailureException {
// TODO Auto-generated method stub
Session session = SessionFactoryUtils.getSession(sessionFactory, true);
this.setFlushMode(FlushMode.COMMIT);
return session;
}
}
② com.zlk.util.OpenSessionInViewFilter
package com.zlk.util;
public class OpenSessionInViewFilter extends
com.zlk.override.OpenSessionInViewFilter {
}
上一篇: 2020巅峰极客wp