`
周禄康
  • 浏览: 157827 次
  • 性别: Icon_minigender_1
  • 来自: 石家庄
社区版块
存档分类
最新评论

Struts1.2+Spring2.5+Hibernate3.2框架搭建(四)

    博客分类:
  • ssh
阅读更多

3.2. 配置 web.xml 文件

     web.xml文件中配置监听器以及web应用的初始化参数

    <!-- 向监听器类中传递初始化参数,以获取beanFactory对象 -->

    <context-param>

       <param-name>contextConfigLocation</param-name>

       <param-value>classpath:applicationContext.xml</param-value>

    </context-param>

    <!--

       使用监听器初始化SpringbeanFactory,并将其放入到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 {

 

}

 

分享到:
评论
4 楼 zdzyh1989 2011-04-19  
其实spring是放在web.xml中启动的,
这个问题时期找到原因了,因为分层不彻底,所以service层和action并没有分开,调用的时候就没有正确的处理opensessionview
http://www.iteye.com/topic/15057
3 楼 周禄康 2010-07-02  
zdzyh1989 写道
在struts中加
<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
    <set-property value="/WEB-INF/action-servlet.xml" property="contextConfigLocation"/>
    </plug-in>
这个了吗,为什么加了之后OpenSessionInViewFilter不管用了呢,

这是因为他俩的启动顺序的问题,你必须让spring先启动才行。
OpenSessionInViewFilter 是在web.xml中的,而你的spring是加载在Struts的配置文件中的 ,这样你的spring还没启动起来呢,就用她的过滤器,你想想可能么。所以你得该一下spring的启动方式,把他放在web。xml中,让他先启动。这样就可以用他的过滤器了。
2 楼 zdzyh1989 2010-06-04  
在struts中加
<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
    <set-property value="/WEB-INF/action-servlet.xml" property="contextConfigLocation"/>
    </plug-in>
这个了吗,为什么加了之后OpenSessionInViewFilter不管用了呢,
1 楼 zdzyh1989 2010-06-04  
来看看,

相关推荐

Global site tag (gtag.js) - Google Analytics