`
aijun980204
  • 浏览: 97506 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

MyEclipse6.0:struts2+spring2+hibernate3.1整合实例

    博客分类:
  • java
阅读更多

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

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

1创建工程
 MyEclipse6.0:struts2+spring2+hibernate3.1整合实例(原创)
MyEclipse6.0:struts2+spring2+hibernate3.1整合实例(原创)

2.创建struts2

   1.创建action 类

MyEclipse6.0:struts2+spring2+hibernate3.1整合实例(原创)

然后导入struts2的包

MyEclipse6.0:struts2+spring2+hibernate3.1整合实例(原创)

然后在www底下创建index.jsp

代码如下:

<%@ page contentType="text/html; charset=UTF-8" %>

<%@ taglib prefix="s" uri="/struts-tags" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

  <head>

  <title>Example</title>

  </head>

  <body>

        <table width="300" border="0">

        <s:form action="LoginAction" theme="simple">

        <tr>

        <td width=50%>用户名:</td>

        <td width=50%><s:textfield name="username" /></td>

        </tr>

        <tr>

        <td width=50%>密码:</td>

        <td width=50%><s:textfield name="password" /></td>

        </tr>

        <tr><td colspan=2 align=center width=100%><s:submit /></td></tr>

        </s:form>

        </table>

  </body>

</html>

下面我们开始写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来写我们的跳转:

<!DOCTYPE struts PUBLIC

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

        "http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>

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

    <constant name="struts.objectFactory" value="spring" />

    <include file="struts-default.xml"></include>

<package name="test" extends="struts-default">

<!-- <action name="LoginAction" class="LoginAction"> 这里用class来关联spring2配置文件中的配置关联  -->

        <action name="LoginAction" class="LoginAction">

        <result name ="input" >/index.jsp </result >

            <result name="success">/success.html</result>

            <result name ="error" >/error.html</result >

        </action>

    </package>

</struts>

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

下面我们来配置web.xml

<?xml version="1.0" encoding="UTF-8"?>

<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">

<display-name>test</display-name>

  <context-param>

        <param-name>contextConfigLocation</param-name>

        <param-value>/WEB-INF/applicationContext.xml</param-value>

 </context-param>

 <filter>

 <filter-name>encodingFilter</filter-name>

<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>

 <init-param>

 <param-name>encoding</param-name>

 <param-value>UTF-8</param-value>

 </init-param>

 </filter>

 <filter>

 <filter-name>struts2</filter-name>

<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>

 </filter>

<!-- 那么哪些请求交给struts过滤呢,这里包括 /struts2spring2hibernate3下和根目录/下的所有请求-->

 <filter-mapping>

 <filter-name>struts2</filter-name>

 <url-pattern>/*</url-pattern>

 </filter-mapping>

<!-- servlet定义一个servlet为struts的ActionServlet -->

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

    <welcome-file-list>

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

    </welcome-file-list>

     <listener>

<!—下面也是spring2的配置 -->

  <listener-class>

   org.springframework.web.context.ContextLoaderListener

  </listener-class>

 </listener>

</web-app>

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

3 创建spring2

MyEclipse6.0:struts2+spring2+hibernate3.1整合实例(原创)

MyEclipse6.0:struts2+spring2+hibernate3.1整合实例(原创)

选好要导入的jar包点next --> finish。

MyEclipse6.0:struts2+spring2+hibernate3.1整合实例(原创)

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

4 创建hibernate3.1

MyEclipse6.0:struts2+spring2+hibernate3.1整合实例(原创)

MyEclipse6.0:struts2+spring2+hibernate3.1整合实例(原创)

选好要导入的jar包点next。

MyEclipse6.0:struts2+spring2+hibernate3.1整合实例(原创)

点next.

MyEclipse6.0:struts2+spring2+hibernate3.1整合实例(原创)

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

MyEclipse6.0:struts2+spring2+hibernate3.1整合实例(原创)

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

MyEclipse6.0:struts2+spring2+hibernate3.1整合实例(原创)

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

点finish

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

<?xml version="1.0" encoding="UTF-8"?>

<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">

    <bean id="DataSource"

       class="org.apache.commons.dbcp.BasicDataSource">

       <property name="driverClassName"

           value="net.sourceforge.jtds.jdbc.Driver">

       </property>

       <property name="url"

           value="jdbc:jtds:sqlserver://127.0.0.1/blog">

       </property>

       <property name="username" value="sa"></property>

       <property name="password" value="123123"></property>

    </bean>

    <bean id="SessionFactory"

    class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">

       <property name="dataSource">

           <ref bean="DataSource" />

       </property>

       <property name="hibernateProperties">

           <props>

              <prop key="hibernate.dialect">

                  org.hibernate.dialect.SQLServerDialect

              </prop>

           </props>

       </property>

    </bean>

</beans>

5 创建数据源

MyEclipse6.0:struts2+spring2+hibernate3.1整合实例(原创)

MyEclipse6.0:struts2+spring2+hibernate3.1整合实例(原创)

点New 出现一个窗口,

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整合实例(原创)

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

MyEclipse6.0:struts2+spring2+hibernate3.1整合实例(原创)

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

MyEclipse6.0:struts2+spring2+hibernate3.1整合实例(原创)

然后选择Hibernate Reverse Engineering

MyEclipse6.0:struts2+spring2+hibernate3.1整合实例(原创)

点next

MyEclipse6.0:struts2+spring2+hibernate3.1整合实例(原创)

选择native 之后点next.

MyEclipse6.0:struts2+spring2+hibernate3.1整合实例(原创)

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

MyEclipse6.0:struts2+spring2+hibernate3.1整合实例(原创)

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

6 调试

首先我们要导入两个jar包

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中加入以下代码:

    <bean id="LoginAction" class="com.test.action.LoginAction" >

       <property name="userInfomationDAO">

           <ref bean="UserInfomationDAO" />

       </property>  

    </bean>

我们现在进行测试一下!

启动Tomcat

MyEclipse6.0:struts2+spring2+hibernate3.1整合实例(原创)

然后我们点Submit.

看看结果!

MyEclipse6.0:struts2+spring2+hibernate3.1整合实例(原创)

再看看数据库的结果!

MyEclipse6.0:struts2+spring2+hibernate3.1整合实例(原创)

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

分享到:
评论

相关推荐

    Struts2_Spring_Hibernate集成

    - Struts2: 2.0.1 - Spring: 2.0 - Hibernate: 3.1 #### 三、集成步骤 ##### 1. 创建Web项目并设定包结构 首先,在MyEclipse中创建一个新的Web项目,并根据需要设计合理的包结构,这有助于代码的组织和管理。 ...

    SSH2三大框架整合

    本文将详细介绍如何在MyEclipse 6.5环境下整合Struts2 2.2.3.1、Spring 2.5、Hibernate 3.2,并完成一个简单的Web项目。 #### 二、环境搭建 ##### 工具准备 - 开发工具:MyEclipse 6.5 - 应用服务器:Tomcat 6.0 ...

    JBPM工作流演示系统使用说明.doc

    开发环境采用Eclipse 3.3+MyEclipse 6.0,JBPM版本为3.2.3,同时系统依赖于Spring 2.0、Struts 1.2和Hibernate 3.1,数据库使用MySQL 5.0,字符编码为utf-8。 2. **系统架构**:系统采用了经典的MVC架构,包括视...

    Java Web应用详解.张丽(带详细书签).pdf

    14.3 Spring 和 Hibernate 组合开发实例 第15章 SSH整合应用 15.1 SSH整合理念 15.2 网络留言板V7.0 15.3 实例开发步骤 15.4 实例完善 项目实战篇 第16章 通用论坛BBS设计与实现 16.1 关键技术解析 16.2 系统...

    JBPM工作流演示系统使用说明

    - **框架与数据库**:系统构建于Spring2.0、Struts1.2和Hibernate3.1之上,不向下兼容低版本;数据库选用MySql5.0,数据编码统一采用utf-8标准,确保数据的一致性和完整性。 #### 二、系统架构与功能模块 系统架构...

Global site tag (gtag.js) - Google Analytics