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

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

最近在搞一个项目要求用新技术来开发。于是找到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配置完毕!由于时间问题例子比较简单!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值