---
title: "eclipse搭建SSH框架详解[转]"
author: "jdw"
date: "2012-02-21T11:04:29Z"
tags: []
categories: ["编程语言"]
slug: "eclipse-ssh"
draft: false
lastmod: "2012-02-21T11:04:29Z"
id: 1415409
url: "https://jdw.iteye.com/blog/1415409"
---

SSH框架是最常用的框架之一，在搭建SSH框架的时候总有人遇到这样，那样的问题。下面我介绍一下SSH框架搭建的全过程。   
第一步：准备工作。   
  下载好eclipse,Struts2,Spring,Hibernate。   
  1.eclipse：eclipse下载的时候建议下载JavaEE版的eclipse。  
                 当然你也可以下载eclipse-SDK。(下载eclipse-SDK需要下载Web，Tomcat等plugins)   
  2.Struts2：[http://struts.apache.org/download](http://struts.apache.org/download)   
         1)引入Struts的jar包。下载 struts-\*-all.zip 解压后，struts\\lib目录下是struts所有的相关jar包。   
         其中有5个是必须的：

               Commons-logging-1.0.4.jar，Freemarker-2.3.13.jar，   
               Ognl-2.6.11.jar，Struts2-core-2.1.6.jar，Xwork-2.1.2.jar   
         其余jar包并不是struts必须的。还有3个包也要注意导入。不导入运行Tomcat时候可能会出现异常。   
               commons-io-1.3.2.jar，commons-fileupload-1.2.1.jar，javassist-3.7.ga.jar   
         注意：javassist-3.7.ga.jar包是在struts2-blank-2.2.1.war示例工程中的web-inf/lib下的。 

  3.Spring：[http://www.springsource.com/download/community](http://www.springsource.com/download/community)   
        还可以在eclipse下安装下载。具体步骤是这样的：  
        1)打开eclipse-help-Software Updates.

   
        2) 在打开的对话框中选择上面的第二项(Available Software)。

   
        3)点击Add Site按钮，弹出URL对话框。 

        4)在对话框里输入：[http://springide.org/updatesite/](http://springide.org/updatesite/)点击OK。 

        5)选择sping IDE点击安装(Install)。

  4.Hibernate：[http://sourceforge.net/projects/hibernate/files/hibernate3/](http://sourceforge.net/projects/hibernate/files/hibernate3/) 

  5.Jdk的src.zip包导入。(当然不导入也可以。。。) 

第二步： 

  1.创建一个 Web Progect，自己起一个喜欢的名字。 

  2.修改WEB-INF下的web.xml文件，增加struts2的配置。

Xml代码  [![收藏代码](http://xsxjb.iteye.com/images/icon_star.png)](http://xsxjb.iteye.com/blog/965689 "收藏这段代码")

01. &lt;?xml version="1.0" encoding="UTF-8"?&gt;
02. &lt;web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
03. xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app\_2\_5.xsd"
04. xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app\_2\_5.xsd"
05. id="WebApp\_ID" version="2.5"&gt;
06. &lt;display-name&gt;SSHTest&lt;/display-name&gt;
07. &lt;!-- struts Framework --&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.ng.filter.StrutsPrepareAndExecuteFilter
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;!-- welcome file --&gt;
19. &lt;welcome-file-list&gt;
20. &lt;welcome-file&gt;index.jsp&lt;/welcome-file&gt;
21. &lt;/welcome-file-list&gt;
22. &lt;/web-app&gt;

  3.在WEB-INF/classes目录下添加struts.xml配置文件： 

Xml代码  [![收藏代码](http://xsxjb.iteye.com/images/icon_star.png)](http://xsxjb.iteye.com/blog/965689 "收藏这段代码")

01. &lt;?xml version="1.0" encoding="UTF-8"?&gt;
02. &lt;!DOCTYPE struts PUBLIC
03. "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
04. "http://struts.apache.org/dtds/struts-2.0.dtd"&gt;
05. &lt;struts&gt;
06. &lt;package namespace="/" name="struts2" extends="struts-default"&gt;
07. &lt;action name="login" method="execute" class="loginAction"&gt;
08. &lt;result name="success"&gt;/WEB-INF/jsp/login.jsp&lt;/result&gt;
09. &lt;result name="input"&gt;/WEB-INF/index.jsp&lt;/result&gt;
10. &lt;/action&gt;
11. &lt;/package&gt;
12. &lt;/struts&gt;

   
  4.配置Spring 

    1)导入spring包。spring-framework-\*\*.zip解压后,将spring-framework-\*\*文件夹的dist目录下的jar包导入工程中。 

    2)配置web.xml文件。 

Xml代码  [![收藏代码](http://xsxjb.iteye.com/images/icon_star.png)](http://xsxjb.iteye.com/blog/965689 "收藏这段代码")

01. &lt;!-- Spring Framework --&gt;
02. &lt;listener&gt;
03. &lt;listener-class&gt;
04. org.springframework.web.context.ContextLoaderListener
05. &lt;/listener-class&gt;
06. &lt;/listener&gt;
07. &lt;context-param&gt;
08. &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
09. &lt;param-value&gt;
10. classpath:/applicationContext\*.xml
11. &lt;/param-value&gt;
12. &lt;/context-param&gt;

   
    3)添加applicationContext.xml文件。 

Xml代码  [![收藏代码](http://xsxjb.iteye.com/images/icon_star.png)](http://xsxjb.iteye.com/blog/965689 "收藏这段代码")

01. &lt;?xml version="1.0" encoding="UTF-8"?&gt;
02. &lt;beans xmlns="http://www.springframework.org/schema/beans"
03. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
04. xmlns:aop="http://www.springframework.org/schema/aop"
05. xmlns:tx="http://www.springframework.org/schema/tx"
06. xsi:schemaLocation="
07. http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
08. http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
09. http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"&gt;
10. &lt;!-- Action --&gt;
11. &lt;bean id="loginAction" scope="prototype" class="action.LoginAction"&gt;&lt;/bean&gt;
12. &lt;/beans&gt;

   
    4)整合Spring与Struts。在Struts的lib目录中找到struts2-spring-plugin-\*.jar，引入到工程中。 

  5.配置Hibernate 

        1)解压缩hibernate-distribution-\*.zip。导入hibernate-distribution-\*GA\\lib\\required目录中的jar包。 

                hibernate3.jar                         核心类库   
                antlr-2.7.6.jar                          代码扫描器,用来翻译HQL语句   
                commons-collections-3.1.jar    Apache Commons包中的一个，包含了一些Apache开发的集合类， 

                                                                功能比java.util.\*强大   
                dom4j-1.6.1.jar                        一个Java的XML API，类似于jdom，用来读写XML文件的   
                javassist-3.4.GA.jar                 Javassist 字节码解释器   
                jta-1.1.jar                                标准的JTA API。   
                slf4j-api-1.5.2.jar   
                slf4j-nop-1.5.2.jar 

        2)创建Hibernate配置文件。在WEB-INF/calsses目录下建立链接数据库的配置文件hibernate.cfg.xml。   
            (本人比较懒，公司电脑中只有Access，也懒得下载别的DBMS。所以例子是连接Access的大家将就看吧。   
            \*注意：需要导入Access\_JDBC30.jar。   
   hibernate.cfg.xml：

Xml代码  [![收藏代码](http://xsxjb.iteye.com/images/icon_star.png)](http://xsxjb.iteye.com/blog/965689 "收藏这段代码")

01. &lt;?xml version="1.0" encoding="UTF-8"?&gt;
02. &lt;!DOCTYPE hibernate-configuration PUBLIC
03. "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
04. "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"&gt;
05. &lt;hibernate-configuration&gt;
06. &lt;session-factory&gt;
07. &lt;property name="connection.driver\_class"&gt;
08. com.hxtt.sql.access.AccessDriver
09. &lt;/property&gt;
10. &lt;property name="connection.url"&gt;
11. jdbc:access:///D:/workspace/SSHTest/TestDatabase.accdb
12. &lt;/property&gt;
13. &lt;!--  数据库连接设置 --&gt;
14. &lt;property name="eclipse.connection.profile"&gt;access&lt;/property&gt;
15. &lt;property name="connection.username"&gt;&lt;/property&gt;
16. &lt;property name="connection.password"&gt;&lt;/property&gt;
17. &lt;property name="dialect"&gt;com.hxtt.support.hibernate.HxttAccessDialect&lt;/property&gt;
18. &lt;!-- show\_sql 生成SQL语句 --&gt;
19. &lt;property name="show\_sql"&gt;true&lt;/property&gt;
20. &lt;!-- SQL dialect 方言 --&gt;
21. &lt;property name="hibernate.dialect"&gt;
22. com.hxtt.support.hibernate.HxttAccessDialect
23. &lt;/property&gt;
24. &lt;!-- 添加实体类的映射文件--&gt;
25. &lt;mapping resource="Login.hbm.xml" /&gt;
26. &lt;!-- Annotation方式配置
27. &lt;mapping class="entity.Login"/&gt;
28. --&gt;
29. &lt;/session-factory&gt;
30. &lt;/hibernate-configuration&gt;

   
             注意：单独使用Hibernate需要创建Session工厂类HibernateSessionFactory.java   
                     (如果用Spring整合就不需要了。Spring会在applicationContext.xml中创建。)   
                      Hibernat 对数据库的操作是通过Session来实现的，这里的session不同于页面间传递参数的session,   
                      而是类似于JDBC中的 Connection。Session是Hibernate运作的中心，   
                      对象的生命周期、事务的管理、数据库的存取都与session息息相关。   
                      而Session是由HibernateSessionFactory创建的，是线程安全的，   
                      可以让多个执行线程同时存取HibernateSessionFactory而不会有数据共享的问题，   
                      但不能让多个线程共享一个Session。 

       3)Login.hbm.xml文件

Xml代码  [![收藏代码](http://xsxjb.iteye.com/images/icon_star.png)](http://xsxjb.iteye.com/blog/965689 "收藏这段代码")

01. &lt;?xml version="1.0" encoding="UTF-8"?&gt;
02. &lt;!DOCTYPE hibernate-mapping PUBLIC
03. "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
04. "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" &gt;
05. &lt;hibernate-mapping package="包名"&gt;
06. &lt;class name="类名" table="表名"&gt;
07. &lt;id name="主键在java类中的字段名" column="对应表中字段" type="类型 "&gt;
08. &lt;generator class="主键生成策略"/&gt;
09. &lt;/id&gt;
10. &lt;/class&gt;
11. &lt;/hibernate-mapping&gt;

 

  6.Spring整合Hibernate。Spring对hibernate的Session的创建、提交、关闭的整个生命周期进行管理。   
        1)  配置sessionFactory，让spring来创建Session。在applicationContext.xml中增加如下代码：

Xml代码  [![收藏代码](http://xsxjb.iteye.com/images/icon_star.png)](http://xsxjb.iteye.com/blog/965689 "收藏这段代码")

1. &lt;!-- sessionFactory --&gt;
2. &lt;bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"&gt;
3. &lt;property name="configLocation"&gt;
4. &lt;value&gt;classpath:/hibernate.cfg.xml&lt;/value&gt;
5. &lt;/property&gt;
6. &lt;/bean&gt;