---
title: "SSH2: Struts2 + Spring + Hibernate 的登录"
author: "yufei"
date: "2007-10-11T14:35:11Z"
tags: []
categories: ["企业架构"]
slug: "ssh2-struts2-spring-hibernate"
draft: false
lastmod: "2007-10-11T14:35:11Z"
id: 131071
url: "https://yufei.iteye.com/blog/131071"
---

使用分层结构,包括 dao,service,control,web层

Struts2 集成 Spring 需要以下几个步骤

1 加入struts2-spring-plugin-2.0.9.jar到项目中(web项目就是丢到/WEB-INF/lib中)

2 Web.xml文件里加入以下listener

```

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--默认加载/WEB-INF 目录下的applicationContext.xml -->

```

如果需要其它的spring配置文件可以在web.xml中加入以下配置

```

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/applicationContext-*.xml,classpath*:applicationContext-*.xml</param-value>
</context-param>
<!--param-value 中间的多个配置文件可以使用 , 隔开-->

```


3 在/WEB-INF 目录下新建applicationContext.xml,并在其中配置Struts2的action及其它需要的bean

4 在struts.xml中将 <action 元素中 class="" 的属性改为spring配置文件中的id

```

         <!-- spring 配置文件中配置 action -->
 <bean id="loginAction" class="login.LoginAction"
		scope="prototype">
		<property name="userService" ref="userService" />
	</bean>

```



```

         <!-- struts 配置文件中配置 action 注意class属性不再是类带路径全名了 -->
 <action name="Login" class="loginAction">
			<result name="success">/login/success.jsp</result>
			<result name="error">/login/error.jsp</result>
	</action>

```


-
-
-

Spring 集成 Hibernate 就不累述了,网上到处都有

-
-

附件里有原代码,导入eclipse(需要myeclipse插件),修改数据源,就能运行了

数据源在src目录下dataSource.properties里,我将数据源配置在外部properties

文件里,spring的配置文件动态载入其内容,如果使用的不是mysql,请注意修改hibernate方言

```

<prop key="hibernate.dialect">
    org.hibernate.dialect.MySQLDialect
</prop>

```


说明:项目内没有所需要的包(struts2,spring2,hibernate3),导入项目以后需要自己

导入相应的三方包,spring使用的是2.0

-

struts2 好像支持不了spring2.0 基于 XML Schmea 的配置,基于这个配置事务应用程序就出错,如果有人知道怎么在struts2集成spring2中使用spring2的xml Schmea的配置,请告知