---
title: "spring+struts+hibernate OpenSessionInView 配置学习"
author: "laukin"
date: "2007-07-21T15:55:00Z"
tags: ["Hibernate", "Servlet", "Spring", "Struts", "配置管理"]
categories: ["企业架构"]
slug: "spring-struts-hibernate-opensessioninview"
draft: false
lastmod: "2007-07-21T15:55:00Z"
id: 103771
url: "https://laukin.iteye.com/blog/103771"
---

## spring+struts+hibernate OpenSessionInView 配置学习

在web.xml中使用OpenSessionInView不能配置：

xml 代码

1. &lt;servlet&gt;
2. &lt;servlet-name&gt;contextservlet-name&gt;
3. &lt;servlet-class&gt;
4. org.springframework.web.context.ContextLoaderServlet
5. servlet-class&gt;
6. &lt;load-on-startup&gt;1load-on-startup&gt;
7. servlet&gt;

需要配置 org.springframework.web.context.ContextLoaderListener：

xml 代码

1. &lt;listener&gt;
2. &lt;listener-class&gt;
3. org.springframework.web.context.ContextLoaderListener
4. listener-class&gt;
5. listener&gt;

并且需要配置 org.springframework.orm.hibernate3.support.OpenSessionInViewFilter   

xml 代码

01. &lt;filter&gt;
02. &lt;filter-name&gt;OpenSessionInViewfilter-name&gt;
03. &lt;filter-class&gt;
04. org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
05. filter-class&gt;
06. &lt;init-param&gt;
07. &lt;param-name&gt;singleSessionparam-name&gt;
08. &lt;param-value&gt;trueparam-value&gt;
09. init-param&gt;
10. filter&gt;

 格外注意：

在配置struts-config.xml文件时，为了整合spring ，需要配置plugin：

xml 代码

1. &lt;plug-in className="org.springframework.web.struts.ContextLoaderPlugIn"&gt;
2. &lt;set-property property="contextConfigLocation"
3. value="/WEB-INF/classes/action- servlet.xml"/&gt;
4. plug-in&gt;

 

具体见：[http://www.iteye.com/topic/15057](http://www.iteye.com/topic/15057)

但是，指定contextConfigLocation属性时， 只需要将配置strutsAction部分的spring配置文件引入，不能将配置数据源和事务管理的配置文件引入，否则OpenSessionInView将失效。