`
longgangbai
  • 浏览: 7238867 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

在项目中SSH或SSH2中OpenSessionInViewFilter的配置顺序(实质过滤器的配置顺序)

阅读更多

   网上一些网友上在项目中SSH或SSH2中OpenSessionInViewFilter的配置顺序(实质过滤器的配置顺序),配置了但是报session关闭的错误,其实原因在Filter配置顺序的原因:

SSH2正确顺序:

OpenSessionInViewFilter

ActionContextCleanUp

FilterDispatcher

以及其他的Filter

 

配置:


 <filter>
  <filter-name>openSessionInViewFilter</filter-name>
  <filter-class>
   org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
  </filter-class>
  <init-param>
   <param-name>sessionFactoryBeanName</param-name>
   <param-value>hibernate.sessionFactory</param-value>
  </init-param>
 </filter>

 

 

注意:无论怎么配置OpenSessionInViewFilter的配置顺序很重要!!同时配置SessionFactory的名称。

 

 

 

struts/spring/hibernate的时候同样使用OpenSessionInView:

1.首先是web.xml

	<filter>
        <filter-name>OpenSessionInViewFilter</filter-name>
        <filter-class>org.springframework.orm.hibernate.support.OpenSessionInViewFilter</filter-class>
    </filter>
    
    <filter-mapping>
        <filter-name>OpenSessionInViewFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    
    <listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>

......

 

2. 然后是struts-config.xml:

<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
	<set-property property="contextConfigLocation" 
				  value="/WEB-INF/action-servlet.xml" 
	/>
</plug-in>

 

 

 

 

S2SH配置:

applicationContext.xml配置如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-2.5.xsd
           http://www.springframework.org/schema/aop
           http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
           http://www.springframework.org/schema/tx
		   http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
		   
	<context:component-scan base-package="com.fsj" /><!-- 启用自动扫描 -->
	<!-- 基于hibernate注解的sessionFactory -->
	<bean id="sessionFactory"
		class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
		<property name="configLocation" value="classpath:hibernate.cfg.xml">
		</property>
	</bean>
	<!-- 基于hibernate的事务管理器 -->
	<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
		<property name="sessionFactory" ref="sessionFactory" />
	</bean>
	<!-- 采用注解形式的声明式事务管理 -->
	<tx:annotation-driven transaction-manager="txManager"/>
	
</beans>

 

web.xml如下设置:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
	xmlns="http://java.sun.com/xml/ns/javaee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
	
	<context-param>
	   <param-name>contextConfigLocation</param-name>
	   <param-value>classpath:applicationContext.xml</param-value>
	</context-param>
	
	<listener>
	      <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
		
						
	<filter>
	        <filter-name>OpenSessionInViewFilter</filter-name>
	        <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
	</filter>
	<filter-mapping>
	        <filter-name>OpenSessionInViewFilter</filter-name>
	        <url-pattern>/*</url-pattern>
	</filter-mapping>
	
	<filter>
		<filter-name>struts2</filter-name>
		<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>
		
	<filter>
		<filter-name>encoding</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>
	</filter>
	<filter-mapping>
		<filter-name>encoding</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
		
</web-app>

 

struts.xml配置:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>

	<!-- 指定Web应用的默认编码集,相当于调用HttpServletRequest的setCharacterEncoding方法 -->
	<constant name="struts.i18n.encoding" value="UTF-8" />
	<!-- 设置浏览器是否缓存静态内容,默认值为true(生产环境下使用),开发阶段最好关闭 -->
	<constant name="struts.serve.static.browserCache" value="false" />
	<!-- 当struts的配置文件修改后,系统是否自动重新加载该文件,默认值为false(生产环境下使用),开发阶段最好打开 -->
	<constant name="struts.configuration.xml.reload" value="true" />
	<!-- 开发模式下使用,这样可以打印出更详细的错误信息 -->
	<constant name="struts.devMode" value="true" />
	<!-- 默认的视图主题 -->
	<constant name="struts.ui.theme" value="simple" />
	<!-- 把action对象交给spring创建 -->
	<constant name="struts.objectFactory" value="spring" />

	<package name="myDefault" extends="struts-default">
		<default-action-ref name="indexPage" />
		<global-results>
			<result name="exceptionPage">/WEB-INF/exceptionPage.jsp
			</result>
		</global-results>
		<global-exception-mappings>
			<exception-mapping result="exceptionPage" exception="java.lang.Exception" />
		</global-exception-mappings>		
		<action name="indexPage">
			<result>/login.jsp</result>
		</action>
	</package>

	<package name="user" namespace="/user" extends="myDefault">
		<!-- 这里面的class不是指完整类路径,而是指在spring中定义的bean的名称 -->
		<action name="*UserAction" class="userAction" method="{1}">
			<result name="success">/WEB-INF/user/loginSuccess.jsp</result>
			<result name="input">/login.jsp</result>
		</action>
	</package>

</struts>

 

 

   项目中采用单一的SessionFactory,如果有多个SessionFactory该怎么办呢?希望牛人指点!!

 

 

分享到:
评论
1 楼 juliancg65 2010-03-01  
不知道是否解决,多sessionfactory的配置?

相关推荐

    OA项目SSH整合框架

    3,在web.xml中配置 spring 的 OpenSessionInView 过滤器(解决抛LazyInitializationException的问题) 1,配置 &lt;!-- 配置 spring 的 OpenSessionInView 过滤器 --&gt; &lt;filter-name&gt;OpenSessionInView ...

    关于OpenSessionInViewFilter的学习

    NULL 博文链接:https://yanzhenwei.iteye.com/blog/1701164

    OpenSessionInViewFilter

    OpenSessionInViewFilter个人学习总结

    SSH项目整合示例【OpenSessionInView】所用到的jar包

    SSH项目整合示例【OpenSessionInView】所用到的jar包 包含Struts + Hibernate + Spring所有jar及其依赖的jar

    jar包(struts2.0.8+spring2.0+hibernate3.2)

    struts2.0.8+spring2.0+hibernate3.2 jar包

    spring_demo:Spring MVC示范项目

    在更新或删除数据时,必须调用getHibernateTemplate().flush(); 且在web.xml中添加 &lt;filter&gt;openSessionInViewFilter &lt;filter&gt;org.springframework.orm.hibernate4.support.OpenSessionInViewFilter &lt;param&gt;...

    S2SH集成 案例

    该案例实现了一个简单的登录功能,但里面将S2SH集成的所有配置信息都添加进去了。 如,OpenSessionInViewFilter、声明式事务、三层等等

    Mac Mysql数据库中文乱码问题解决

    如:在使用Java中得SSH框架时,我们需要在web.xml文件中配置编码的filter,具体代码是: &lt;span xss=removed&gt;&lt;!-- 表单处理乱码,必须在OpenSessionInViewFilter的filter之前 --&gt; &lt;filter&gt;CharacterFilter ...

    struts+spring+hibernate整合

    Spring4.0、Struts2.3.15、Hibernate4.2.4、jQuery1.9.1涉及到了诸多开发时的细节:ModelDriven、Preparable 拦截器、编写自定义的类型转换器、Struts2 处理 Ajax、OpenSessionInViewFilter、迫切左外连接、Spring ...

    Sping 事务管理.doc

    OpenSessionInViewFilter解决Web应用程序的问题

    spring_note.rar_inversion_spring concept

    课程内容 面向接口(抽象)编程的概念与好处 IOC/DI的概念与好处 inversion of control dependency injection AOP的概念与好处 ...opensessionInviewfilter(记住,解决什么问题,怎么解决) Spring JDBC

    SPRING API 2.0.CHM

    All Classes ...Cglib2AopProxy.SerializableNoOp CglibSubclassingInstantiationStrategy ChainedExceptionListener ChainedPersistenceExceptionTranslator CharacterEditor CharacterEncodingFilter ...

Global site tag (gtag.js) - Google Analytics