---
title: "s2sh整合开发(1)"
author: "returnofking"
date: "2010-01-08T11:10:31Z"
tags: []
categories: ["编程语言"]
slug: "s2sh-1"
draft: false
lastmod: "2010-01-08T11:15:07Z"
id: 565632
url: "https://ReturnOfKing.iteye.com/blog/565632"
---

Struts 2,Spring 2,Hibernate 整合  
开发工具Eclipse 6.5/6.6 GA  
1.首先创建一个web工程，我们暂且启明为s2sh  
这里我们选择的是Java EE 5.0

![](/upload/attachment/116151/4399e412-a817-39c7-85c2-f235153e7abf.jpg)

finish  
2.向工程加入hibernate支持，这里我们使用的是hibernate 3.2

![](/upload/attachment/116153/f181d429-3000-363f-9ec4-f75b15fb1aa8.jpg)

默认就可以，不要忘记将jar包加入到/WebRoot/WEB-INF/lib

![](/upload/attachment/116155/e059580e-85bf-32ac-82db-baa857a36d4a.jpg)

接着next

![](/upload/attachment/116157/b20239e2-9378-36b6-9cdf-c9e94cae750b.jpg)

实际上这里的hibernate.cfg.xml不起作用，因为该配置文件的内容由后面的spring配置文件进行管理，我们可以在创建之后将其删除  
默认next

![](/upload/attachment/116159/6929bb1f-ec56-3beb-97d6-dd05a9171cfc.jpg)

我们根本不用hibernate.cfg.xml,所以这里根本不需要进行数据库配置，当然默认也无所谓。  
继续next

![](/upload/attachment/116161/1ef19f62-0c15-363d-8956-73115c1c24e9.jpg)

sessionFactory我们在之后也会直接使用spring为我们提供的，所以这里不需要创建。  
finish  
3.向工程加入spring支持，这里我们使用的是spring2

![](/upload/attachment/116163/a20ca315-c299-3ecb-b04d-fe68f9952068.jpg)

这里我们选择的是spring2，

![](/upload/attachment/116165/ce5bfbde-283c-35e4-b2e7-b9b23d5272f4.jpg)

不要忘记选中这5个库包：  
Spring 2.0 AOP Libraries  
Spring 2.0 Core Libraries  
Spring 2.0 Persistence Core Libraries  
Spring 2.0 Persistence JDBC Libraries  
Spring 2.0 Web Libraries  
也不要忘记将jar包加入到/WebRoot/WEB-INF/lib。  
next

![](/upload/attachment/116167/181630b8-bca9-3091-9f80-a1de5885eaf4.jpg)

这里我们不需要Enable AOP 所以不需要选中  
不要忘记将配置文件applicationContext.xml指定在/WEB-INF目录下，不然服务器启动时无法加载  
next

![](/upload/attachment/116169/8c142473-151c-3b4b-8410-ad615de6f39d.jpg)

这里的sessionFactory我们也不需要，之后会手动配置。  
finish  
4.加入struts2支持，由于MyEclipse并没有加入Struts2的支持，所以我们需要手动导入相关jar包。  
这里我们使用的是struts 2.0.11版本  
需要的相关jar包如下：  
freemarker-2.3.8.jar  
ognl-2.6.11.jar  
struts2-core-2.0.11.jar  
xwork-2.0.4.jar  
struts2-spring-plugin-2.0.11.jar

4.进行相关文件的配置  
首先我们需要在src下加入struts的配置文件struts.xml

Xml代码 [![复制代码](http://llying.iteye.com/images/icon_copy.gif)](http://llying.iteye.com/blog/410201 "复制代码")

1. **&lt;?xml** version="1.0" encoding="UTF-8" **?&gt;**
2. &lt;!DOCTYPE struts PUBLIC
3. "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
4. "http://struts.apache.org/dtds/struts-2.0.dtd"**&gt;**
5. **&lt;struts&gt;**
6. **&lt;package** name="s2sh" extends="struts-default"**&gt;**
7. **&lt;/package&gt;**
8. **&lt;/struts&gt;**

```
<?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>
	<package name="s2sh" extends="struts-default">
	</package>
</struts>
```

接下来我们配置web.xml

Xml代码 [![复制代码](http://llying.iteye.com/images/icon_copy.gif)](http://llying.iteye.com/blog/410201 "复制代码")

01. **&lt;?xml** version="1.0" encoding="UTF-8"**?&gt;**
02. **&lt;web-app** version="2.5"
03. xmlns="http://java.sun.com/xml/ns/javaee"
04. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
05. xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
06. http://java.sun.com/xml/ns/javaee/web-app\_2\_5.xsd"**&gt;**
07. &lt;!-- 配置struts2的过滤器 --&gt;
08. **&lt;filter&gt;**
09. **&lt;filter-name&gt;**struts2**&lt;/filter-name&gt;**
10. **&lt;filter-class&gt;**
11. org.apache.struts2.dispatcher.FilterDispatcher
12. **&lt;/filter-class&gt;**
13. **&lt;/filter&gt;**
14. **&lt;filter-mapping&gt;**
15. **&lt;filter-name&gt;**struts2**&lt;/filter-name&gt;**
16. **&lt;url-pattern&gt;**/\***&lt;/url-pattern&gt;**
17. **&lt;/filter-mapping&gt;**
18. &lt;!-- 配置spring的监听器 --&gt;
19. **&lt;listener&gt;**
20. **&lt;listener-class&gt;**
21. org.springframework.web.context.ContextLoaderListener
22. **&lt;/listener-class&gt;**
23. **&lt;/listener&gt;**
24. **&lt;welcome-file-list&gt;**
25. **&lt;welcome-file&gt;**index.jsp**&lt;/welcome-file&gt;**
26. **&lt;/welcome-file-list&gt;**
27. **&lt;/web-app&gt;**

```
<?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">
<!-- 配置struts2的过滤器 -->
<filter>
	<filter-name>struts2</filter-name>
	<filter-class>
		org.apache.struts2.dispatcher.FilterDispatcher
	</filter-class>
</filter>

<filter-mapping>
	<filter-name>struts2</filter-name>
	<url-pattern>/*</url-pattern>
</filter-mapping>

<!-- 配置spring的监听器 -->
<listener>
	<listener-class>
		org.springframework.web.context.ContextLoaderListener
	</listener-class>
</listener>

  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>

</web-app>
```

到目前为止我们的整合工作已经完成。我们可以将该工程打包，以备下次直接使用。  
这里由一个问题就是如果采用myeclipse加载hibernate和spring的lib包形式发布项目的时候会出现异常，我们可以手动去掉一个asm-2.2.3jar即可。