---
title: "MyEclipse6.0：struts2+spring2+hibernate3.1整合实例"
author: "aijun980204"
date: "2010-04-24T10:48:13Z"
tags: ["Bean", "Hibernate", "Spring", "Struts", "XML"]
categories: ["编程语言"]
slug: "myeclipse6-0-struts2-spring2-hibernate3-1"
draft: false
lastmod: "2010-04-24T10:48:13Z"
id: 651973
url: "https://aijun980204.iteye.com/blog/651973"
---

最近在搞一个项目要求用新技术来开发。于是找到struts2 + spring2 + hibernate3.1 一开始去到网上搜索了很多例子和配置都只有配置文件本人看的是眼都绿了，有的都没有写完全。下面我把我的成功例子发出来与大家分享一下。

用的工具Tomcat 5.5,MyEclipse6.0,sqlserver2005

1创建工程

 ![MyEclipse6.0：struts2+spring2+hibernate3.1整合实例(原创)](http://static.photo.sina.com.cn/middle/4c5ce20744058bca9ccba&000 "MyEclipse6.0：struts2+spring2+hibernate3.1整合实例(原创)")

![MyEclipse6.0：struts2+spring2+hibernate3.1整合实例(原创)](http://static.photo.sina.com.cn/middle/4c5ce20744058bd589916&000 "MyEclipse6.0：struts2+spring2+hibernate3.1整合实例(原创)")

2.创建struts2

   1．创建action 类

![MyEclipse6.0：struts2+spring2+hibernate3.1整合实例(原创)](http://static.photo.sina.com.cn/middle/4c5ce20744058bdd9674b&000 "MyEclipse6.0：struts2+spring2+hibernate3.1整合实例(原创)")

然后导入struts2的包

![MyEclipse6.0：struts2+spring2+hibernate3.1整合实例(原创)](http://static.photo.sina.com.cn/middle/4c5ce20744058bdeaa40f&000 "MyEclipse6.0：struts2+spring2+hibernate3.1整合实例(原创)")

然后在www底下创建index.jsp

代码如下：

&lt;%@ page contentType="text/html; charset=UTF-8" %&gt;

&lt;%@ taglib prefix="s" uri="/struts-tags" %&gt;

&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"&gt;

&lt;html&gt;

  &lt;head&gt;

  &lt;title&gt;Example&lt;/title&gt;

  &lt;/head&gt;

  &lt;body&gt;

        &lt;table width="300" border="0"&gt;

        &lt;s:form action="LoginAction" theme="simple"&gt;

        &lt;tr&gt;

        &lt;td width=50%&gt;用户名:&lt;/td&gt;

        &lt;td width=50%&gt;&lt;s:textfield name="username" /&gt;&lt;/td&gt;

        &lt;/tr&gt;

        &lt;tr&gt;

        &lt;td width=50%&gt;密码:&lt;/td&gt;

        &lt;td width=50%&gt;&lt;s:textfield name="password" /&gt;&lt;/td&gt;

        &lt;/tr&gt;

        &lt;tr&gt;&lt;td colspan=2 align=center width=100%&gt;&lt;s:submit /&gt;&lt;/td&gt;&lt;/tr&gt;

        &lt;/s:form&gt;

        &lt;/table&gt;

  &lt;/body&gt;

&lt;/html&gt;

下面我们开始写LoginAction类：

**package** com.test.action;

**import** com.opensymphony.xwork2.ActionSupport;

**public** **class** LoginAction **extends** ActionSupport {

 

    **private** String username;

    **private** String password;

    **public** String getUsername() {

       **return** username;

    }

    **public** **void** setUsername(String username) {

       **this** .username = username;

    }

    **public** String getPassword() {

       **return** password;

    }

    **public** **void** setPassword(String password) {

       **this** .password = password;

    }

    **public** String execute() {

       **return** *SUCCESS* ;

    }

}

再接下来就是在src目录底下创建一个struts.xml来写我们的跳转：

&lt;!DOCTYPE struts PUBLIC

        "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"

        "http://struts.apache.org/dtds/struts-2.0.dtd"&gt;

&lt;struts&gt;

&lt;!-- &lt;constant name="struts.objectFactory" value="spring" /&gt; 是在后面的spring2配置中用到 意思是把控制交给spring2  --&gt;

    &lt;constant name="struts.objectFactory" value="spring" /&gt;

    &lt;include file="struts-default.xml"&gt;&lt;/include&gt;

&lt;package name="test" extends="struts-default"&gt;

&lt;!-- &lt;action name="LoginAction" class="LoginAction"&gt; 这里用class来关联spring2配置文件中的配置关联  --&gt;

        &lt;action name="LoginAction" class="LoginAction"&gt;

        &lt;result name ="input" &gt;/index.jsp &lt;/result &gt;

            &lt;result name="success"&gt;/success.html&lt;/result&gt;

            &lt;result name ="error" &gt;/error.html&lt;/result &gt;

        &lt;/action&gt;

    &lt;/package&gt;

&lt;/struts&gt;

成功和失败的页面大家自己建一下就行了！

下面我们来配置web.xml

&lt;?xml version="1.0" encoding="UTF-8"?&gt;

&lt;web-app version="2.5" id="WebApp"

    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"&gt;

&lt;display-name&gt;test&lt;/display-name&gt;

  &lt;context-param&gt;

        &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;

        &lt;param-value&gt;/WEB-INF/applicationContext.xml&lt;/param-value&gt;

 &lt;/context-param&gt;

 &lt;filter&gt;

 &lt;filter-name&gt;encodingFilter&lt;/filter-name&gt;

&lt;filter-class&gt;org.springframework.web.filter.CharacterEncodingFilter&lt;/filter-class&gt;

 &lt;init-param&gt;

 &lt;param-name&gt;encoding&lt;/param-name&gt;

 &lt;param-value&gt;UTF-8&lt;/param-value&gt;

 &lt;/init-param&gt;

 &lt;/filter&gt;

 &lt;filter&gt;

 &lt;filter-name&gt;struts2&lt;/filter-name&gt;

&lt;filter-class&gt;org.apache.struts2.dispatcher.FilterDispatcher&lt;/filter-class&gt;

 &lt;/filter&gt;

&lt;!-- 那么哪些请求交给struts过滤呢，这里包括 /struts2spring2hibernate3下和根目录/下的所有请求--&gt;

 &lt;filter-mapping&gt;

 &lt;filter-name&gt;struts2&lt;/filter-name&gt;

 &lt;url-pattern&gt;/\*&lt;/url-pattern&gt;

 &lt;/filter-mapping&gt;

&lt;!-- servlet定义一个servlet为struts的ActionServlet --&gt;

&lt;!-- 定义默认返回页，如输入http://127.0.0.1/那么根目录下的index.html或者其他文件就被请求 --&gt;

    &lt;welcome-file-list&gt;

       &lt;welcome-file&gt;index.jsp&lt;/welcome-file&gt;

    &lt;/welcome-file-list&gt;

     &lt;listener&gt;

&lt;!—下面也是spring2的配置 --&gt;

  &lt;listener-class&gt;

   org.springframework.web.context.ContextLoaderListener

  &lt;/listener-class&gt;

 &lt;/listener&gt;

&lt;/web-app&gt;

到这里我们的struts2配置完了 下面我们运用MyEclipse 来加入spring2 首先提示一下有些朋友会说到为什么不先加hibernate3.1呢？如果先加入hibernate3.1的话 那我们再加入spring2后要自己手动配置相信没有多少人喜欢自己配置吧？呵呵

3 创建spring2

![MyEclipse6.0：struts2+spring2+hibernate3.1整合实例(原创)](http://static.photo.sina.com.cn/middle/4c5ce20744058be006d99&000 "MyEclipse6.0：struts2+spring2+hibernate3.1整合实例(原创)")

![MyEclipse6.0：struts2+spring2+hibernate3.1整合实例(原创)](http://static.photo.sina.com.cn/middle/4c5ce20744058be0861cc&000 "MyEclipse6.0：struts2+spring2+hibernate3.1整合实例(原创)")

选好要导入的jar包点next --&gt; finish。

![MyEclipse6.0：struts2+spring2+hibernate3.1整合实例(原创)](http://static.photo.sina.com.cn/middle/4c5ce20744058be0737da&000 "MyEclipse6.0：struts2+spring2+hibernate3.1整合实例(原创)")

spring2加入到了我们的项目中了。下面我们来把hibernate3.1加入到项目中去。

4 创建hibernate3.1

![MyEclipse6.0：struts2+spring2+hibernate3.1整合实例(原创)](http://static.photo.sina.com.cn/middle/4c5ce20744058be27a4da&000 "MyEclipse6.0：struts2+spring2+hibernate3.1整合实例(原创)")

![MyEclipse6.0：struts2+spring2+hibernate3.1整合实例(原创)](http://static.photo.sina.com.cn/middle/4c5ce20744058be54ac47&000 "MyEclipse6.0：struts2+spring2+hibernate3.1整合实例(原创)")

选好要导入的jar包点next。

![MyEclipse6.0：struts2+spring2+hibernate3.1整合实例(原创)](http://static.photo.sina.com.cn/middle/4c5ce20744058bcc13e11&000 "MyEclipse6.0：struts2+spring2+hibernate3.1整合实例(原创)")

点next.

![MyEclipse6.0：struts2+spring2+hibernate3.1整合实例(原创)](http://static.photo.sina.com.cn/middle/4c5ce20744058bcd8f4b6&000 "MyEclipse6.0：struts2+spring2+hibernate3.1整合实例(原创)")

SessionFactory ID 是写在applicationContext.xml中的在创建完成后会在直接更新applicationContext.xml文件的。

![MyEclipse6.0：struts2+spring2+hibernate3.1整合实例(原创)](http://static.photo.sina.com.cn/middle/4c5ce20744058bce48797&000 "MyEclipse6.0：struts2+spring2+hibernate3.1整合实例(原创)")

Bean ID 也是写在applicationContext.xml中的在创建完成后会在直接更新applicationContext.xml文件。点next。

![MyEclipse6.0：struts2+spring2+hibernate3.1整合实例(原创)](http://static.photo.sina.com.cn/middle/4c5ce20744058bceda988&000 "MyEclipse6.0：struts2+spring2+hibernate3.1整合实例(原创)")

我们要用的是spring2的SessionFactory控制所以不用hibernate3.1 自己的SessionFactory。

点finish

下面我们来看看applicationContext.xml更新后的文件

&lt;?xml version="1.0" encoding="UTF-8"?&gt;

&lt;beans

    xmlns="http://www.springframework.org/schema/beans"

    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd"&gt;

    &lt;bean id="DataSource"

       class="org.apache.commons.dbcp.BasicDataSource"&gt;

       &lt;property name="driverClassName"

           value="net.sourceforge.jtds.jdbc.Driver"&gt;

       &lt;/property&gt;

       &lt;property name="url"

           value="jdbc:jtds:sqlserver://127.0.0.1/blog"&gt;

       &lt;/property&gt;

       &lt;property name="username" value="sa"&gt;&lt;/property&gt;

       &lt;property name="password" value="123123"&gt;&lt;/property&gt;

    &lt;/bean&gt;

    &lt;bean id="SessionFactory"

    class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"&gt;

       &lt;property name="dataSource"&gt;

           &lt;ref bean="DataSource" /&gt;

       &lt;/property&gt;

       &lt;property name="hibernateProperties"&gt;

           &lt;props&gt;

              &lt;prop key="hibernate.dialect"&gt;

                  org.hibernate.dialect.SQLServerDialect

              &lt;/prop&gt;

           &lt;/props&gt;

       &lt;/property&gt;

    &lt;/bean&gt;

&lt;/beans&gt;

5 创建数据源

![MyEclipse6.0：struts2+spring2+hibernate3.1整合实例(原创)](http://static.photo.sina.com.cn/middle/4c5ce20744058bcf469ba&000 "MyEclipse6.0：struts2+spring2+hibernate3.1整合实例(原创)")

![MyEclipse6.0：struts2+spring2+hibernate3.1整合实例(原创)](http://static.photo.sina.com.cn/middle/4c5ce20744058bd0d82a7&000 "MyEclipse6.0：struts2+spring2+hibernate3.1整合实例(原创)")

点New 出现一个窗口，

![MyEclipse6.0：struts2+spring2+hibernate3.1整合实例(原创)](http://static.photo.sina.com.cn/middle/4c5ce20744058bd06be0a&000 "MyEclipse6.0：struts2+spring2+hibernate3.1整合实例(原创)")

Driver template （数据库类型）

Driver name（创建的数据源名称）

Connection URL （连接数据库用到的驱动URL）

User Name （数据库的用户名）

Password （数据库的密码）

Driver JARS （数据库驱动包）

Driver Classname （数据库驱动类）

点finish完成创建。

![MyEclipse6.0：struts2+spring2+hibernate3.1整合实例(原创)](http://static.photo.sina.com.cn/middle/4c5ce20744058bd124f8a&000 "MyEclipse6.0：struts2+spring2+hibernate3.1整合实例(原创)")

下面我们来针对表用hibernate来创建程序和数据库之间的联系。

![MyEclipse6.0：struts2+spring2+hibernate3.1整合实例(原创)](http://static.photo.sina.com.cn/middle/4c5ce20744058bd49348d&000 "MyEclipse6.0：struts2+spring2+hibernate3.1整合实例(原创)")

我们对userInfomation表进行操作并且实现通过hibernate把程序和数据库连接在一起，首先在userInfomation 上右击然后出现一个菜单，

![MyEclipse6.0：struts2+spring2+hibernate3.1整合实例(原创)](http://static.photo.sina.com.cn/middle/4c5ce20744058bd419324&000 "MyEclipse6.0：struts2+spring2+hibernate3.1整合实例(原创)")

然后选择Hibernate Reverse Engineering

![MyEclipse6.0：struts2+spring2+hibernate3.1整合实例(原创)](http://static.photo.sina.com.cn/middle/4c5ce20744058bd59e47b&000 "MyEclipse6.0：struts2+spring2+hibernate3.1整合实例(原创)")

点next

![MyEclipse6.0：struts2+spring2+hibernate3.1整合实例(原创)](http://static.photo.sina.com.cn/middle/4c5ce20744058bd70933b&000 "MyEclipse6.0：struts2+spring2+hibernate3.1整合实例(原创)")

选择native 之后点next.

![MyEclipse6.0：struts2+spring2+hibernate3.1整合实例(原创)](http://static.photo.sina.com.cn/middle/4c5ce20744058bd953965&000 "MyEclipse6.0：struts2+spring2+hibernate3.1整合实例(原创)")

这个菜单的设置是表与表的范式关系。点finish.

![MyEclipse6.0：struts2+spring2+hibernate3.1整合实例(原创)](http://static.photo.sina.com.cn/middle/4c5ce20744058bda4f45a&000 "MyEclipse6.0：struts2+spring2+hibernate3.1整合实例(原创)")

我们基本上实现了struts2+spring2+hibernate3的衔接了。下面我们来具体调试一下。

6 调试

首先我们要导入两个jar包

![MyEclipse6.0：struts2+spring2+hibernate3.1整合实例(原创)](http://static.photo.sina.com.cn/middle/4c5ce20744058bdadaa2f&000 "MyEclipse6.0：struts2+spring2+hibernate3.1整合实例(原创)")

如果不导入这两个包连接就会出错。并且把MyEclipse创建spring2时候生成的commons-collections2.11删除。接下来我们来完善LoginAction.java

代码如下：

**package** com.test.action;

**import** com.opensymphony.xwork2.ActionSupport;

**import** com.test.hibernate.UserInfomationDAO;

**import** com.test.hibernate.UserInfomation;

**public** **class** LoginAction **extends** ActionSupport {

 

    **private** UserInfomationDAO userInfomationDAO;

    **private** String username;

    **private** String password;

    **public** String getUsername() {

       **return** username;

    }

    **public** **void** setUsername(String username) {

       **this** .username = username;

    }

    **public** String getPassword() {

       **return** password;

    }

    **public** **void** setPassword(String password) {

       **this** .password = password;

    }

    **public** **void** setUserInfomationDAO(UserInfomationDAO userInfomationDAO) {

       **this** .userInfomationDAO = userInfomationDAO;

    }

    **public** String execute() {

       UserInfomation  userInfomation = **new** UserInfomation();

       userInfomation.setUsername(username);

       userInfomation.setPassword(password);

       **try** {

       userInfomationDAO.save(userInfomation);

       }**catch** (Exception ex){

           ex.printStackTrace();

           **return** *ERROR* ;

       }

       **return** *SUCCESS* ;

    }

}

下面我们把struts2的控制转交给spring2

在appliction.xml中加入以下代码：

    &lt;bean id="LoginAction" class="com.test.action.LoginAction" &gt;

       &lt;property name="userInfomationDAO"&gt;

           &lt;ref bean="UserInfomationDAO" /&gt;

       &lt;/property&gt;  

    &lt;/bean&gt;

我们现在进行测试一下！

启动Tomcat

![MyEclipse6.0：struts2+spring2+hibernate3.1整合实例(原创)](http://static.photo.sina.com.cn/middle/4c5ce20744058bdabf947&000 "MyEclipse6.0：struts2+spring2+hibernate3.1整合实例(原创)")

然后我们点Submit.

看看结果！

![MyEclipse6.0：struts2+spring2+hibernate3.1整合实例(原创)](http://static.photo.sina.com.cn/middle/4c5ce20744058bdc306ef&000 "MyEclipse6.0：struts2+spring2+hibernate3.1整合实例(原创)")

再看看数据库的结果！

![MyEclipse6.0：struts2+spring2+hibernate3.1整合实例(原创)](http://static.photo.sina.com.cn/middle/4c5ce20744058bdcb6fa2&000 "MyEclipse6.0：struts2+spring2+hibernate3.1整合实例(原创)")

到此为止struts2+spring2+hibernate3配置完毕！由于时间问题例子比较简单！