Struts+Spring+Hibernate--SSH整合实例

Struts+Spring+Hibernate--SSH整合实例: 网上虽然有很多SSH整合的小例子,但很多程序都出现了不是这样,就是那样的错误。自己总结了一下,终于做出的Struts+Spring+Hibernate整合的小例子,也是最基础的SSH实例,虽然是小例子,但什么程序都是从基础的做起。如果你弄明白了这个小实例,相信你的SSH整合框架技术也会提高很多。 在做本例前,需要熟悉对Struts,Hibernate的反向工程等的基本操作。 开发工具:MyEclipse+MySQL+Tomcat 说明:本实例是简单注册程序(只有两个属性) 数据库脚本:user.sql DROP TABLE IF EXISTS `user`; CREATE TABLE `user` ( `Id` int(11) NOT NULL AUTO_INCREMENT, `username` varchar(255) DEFAULT NULL, `password` varchar(255) DEFAULT NULL, PRIMARY KEY (`Id`) ) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8; 开发步骤: 一 1建立Web项目 选择: Java EE 5.5 2添加Struts支持(项目右键àMyEclipse Struts Capabilities …) 选择: 1)Struts specification: Struts 1.3 2)Base package for 那位classes:edu.syict.struts 3添加Spring支持(项目右键àMyEclipse Spring Capabilities …) 选择: 1) Spring version:Spring 2.0 2) Select the libraries to add the buildpath: Spring 2.0 AOP Libraries Spring 2.0 Core Libraries Spring 2.0 Persistence Core Libraries Spring 2.0 Persistence JDBC Libraries Spring 2.0 Web Libraries 3) JAR Library Installation: Copy checked Library contents to project folder (TLDs always copied) Next 4) Spring配置文件选择目录Folder:WebRoot/WEB-INF Finish 4 添加Hibernate支持(项目右键àMyEclipse Hibernate Capabilities …) 选择: 1)Hibernate Specification:Hibernate3.1 2)Select the libraries to ad to the buildpath:Hibernate 3.1 Core Libraries 3)JAR Library Installation:Copy checked Library Jars to project folder and add to build-path Next 4)选择:Spring configuration file(applicationContext.xml) Next 5)选择:Existing Spring configuration file SessionFactory Id: sessionFactory àNext 6)Bean Id:dataSource DB Driver:选择配置好的Hibernate。àNext 7)不建立SessionFactory(不选择Create SessionFactory class?) 8)Finish。 二 数据库方面 1)建立包edu.syict.pojo Hibernate反向工程: 选择下列内容: Create POJO<>DB Table mapping information复选框 Create a Hibernate mapping file单选框 Update Hibernate configuration with mapping resource location复选框 Java Data Object(POJO<>DB Table)复选框 其余的都不选择。àNext Id Generator:native àFinish 2)新建接口:edu.syict.dao.UserDao.java (所在包:edu.syict.dao ) package edu.syict.dao; import edu.syict.pojo.User; public interface UserDao { public void save(User user); } 3)建立UserDao接口实现类edu.syict.dao.impl.UserDaoImpl 类继承HibernateDaoSupport,接口继承UserDao package edu.syict.dao; import org.springframework.orm.hibernate3.support.HibernateDaoSupport; import edu.syict.pojo.User; public class UserDaoImpl extends HibernateDaoSupport implements UserDao { public void save(User user) { try{ this.getHibernateTemplate().saveOrUpdate(user); }catch(RuntimeException re){ throw re; } } } 三 建立JSP 页面,Action类 1) 打开struts-config.xml配置文件:右键new àForm,Action and JSP 2) User case:register Form Impl:Dynamic FormBean Properties:username password JSP:选择Create JSP form àNext 3) Action 默认就可以了 àFinish 4) 配置struts-config.xml文件,将Struts交给Spring管理 a) 加入插件 (message-resources标记下) b) 将Action类移交(message-resources标记上) struts-config.xml文件(全部) struts-config.xml文件(全部)

5) register.jsp页面(WebRoot/form/register.jsp)

SSH整合


用户注册

用户名:
密    码:
    
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/yefengmeander/archive/2009/02/23/3930069.aspx

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值