免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1519 | 回复: 0
打印 上一主题 下一主题

MyEclipse6.0:struts2+spring2+hibernate3.1整合实例 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2007-12-30 14:29 |只看该作者 |倒序浏览

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



2.创建struts2
   1.创建action 类

然后导入struts2的包

然后在www底下创建index.jsp
代码如下:
  

Example
  
  
      
      
      
      
用户名:
      
      
      
      
密码:
      
      
      
      
      
  
下面我们开始写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来写我们的跳转:
      
"-//Apache Software Foundation//DTD Struts Configuration
2.0//EN"
      
"http://struts.apache.org/dtds/struts-2.0.dtd">
是在后面的spring2配置中用到
意思是把控制交给spring2  -->
   
   
这里用class来关联spring2配置文件中的配置关联
-->
      
      
/index.jsp
           
/success.html
           
/error.html
      
   
成功和失败的页面大家自己建一下就行了!
下面我们来配置web.xml
   
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">
test
  

     
contextConfigLocation

     
/WEB-INF/applicationContext.xml


encodingFilter
org.springframework.web.filter.CharacterEncodingFilter

encoding
UTF-8



struts2
org.apache.struts2.dispatcher.FilterDispatcher


struts2
/*

   
      
index.jsp
   
   

  
  
org.springframework.web.context.ContextLoaderListener
  

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


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

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


选好要导入的jar包点next。

点next.

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

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

我们要用的是spring2的SessionFactory控制所以不用hibernate3.1
自己的SessionFactory。
点finish
下面我们来看看applicationContext.xml更新后的文件
   
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">
   
      
class="org.apache.commons.dbcp.BasicDataSource">
      
         
value="net.sourceforge.jtds.jdbc.Driver">
      
      
         
value="jdbc:jtds:sqlserver://127.0.0.1/blog">
      
      
      
   
   
   
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
      
         
      
      
         
            
                 
org.hibernate.dialect.SQLServerDialect
            
         
      
   
5 创建数据源


点New 出现一个窗口,

Driver template (数据库类型)
Driver name(创建的数据源名称)
Connection URL (连接数据库用到的驱动URL)
User Name (数据库的用户名)
Password (数据库的密码)
Driver JARS (数据库驱动包)
Driver Classname (数据库驱动类)
点finish完成创建。

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

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

然后选择Hibernate Reverse Engineering

点next

选择native 之后点next.

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

我们基本上实现了struts2+spring2+hibernate3的衔接了。下面我们来具体调试一下。
6 调试
首先我们要导入两个jar包

如果不导入这两个包连接就会出错。并且把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中加入以下代码:
   
      
         
      
  
   
我们现在进行测试一下!
启动Tomcat

然后我们点Submit.
看看结果!

再看看数据库的结果!

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

本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/21344/showart_453509.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP