




已阅读5页,还剩33页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
基于myeclpise 8.5 的 ssh 整合zero atlantis2011-01-29基于 myeclpise 的 ssh 整合实验环境 . 3创建工程 . 3 添加 struts 支持. 5 添加 spring 支持 . 6 整合 struts 和 spring . 9 添加 hibernate 支持. 11 整理 jar 包. 18 建立目录架构. 19 生成 hibernate 反向工程. 20 编写 service. 29 创建 form、action 和 jsp . 32 收尾. 3838实验环境 myeclipse 8.5 (集成 jdk 和 tomcat) struts 1.2 hibernate 3.2 spring 2.5 mysql 5.1.4数据库脚本数据库的准备和 hibernate 数据源的配置都比较简单,在这里就不累赘了。这边只给 出数据库的脚本,数据库本例中数据库名称 ssh。数据库脚本如下:drop table if exists user; create table user (id int not null auto_increment, name varchar(50) default null,password varchar(50) default null,primary key (id);insert into user(name,password) values (admin, admin);创建工程filenewweb project工程名字本例选择 java ee 5.0点击 finish 完成项目的创建选择 yes项目创建就完成了,接下来就要添加 ssh 框架了。这三个框架的添加有一定的顺序。曾经有人强调添加的顺序一定要是 springhibernatestruts。其实不然,严格意义上来讲 是有两个框架的添加必须要有顺序,就是必须是先加 spring,再添加 hibernate。好了,接 下来就开始添加框架吧。我们选择 strutsspringhibernate 的顺序。添加 struts 支持在工程 ssh 上右击,选择 myeclipse,再选择 add struts capabilities这样 struts 支持就添加好了,跟正常添加 struts 支持一模一样的。好了,接下来添加spring 支持。添加 spring 支持在工程 ssh 上右击,选择 myeclipse,再选择 add spring capabilities选择将 jar 包复制到 /web/web-inf/lib 下。需要添加的 jar 包如下将 folder 修改为 webroot/web-inf ,其他默认。这样就添加好了 spring 支持。接下来就先整合 struts 和 spring。整合 struts 和 spring首先,需要修改 web.xml 文件。在 web.xml 中添加如下代码。contextconfiglocation/web-inf/applicationcontext.xmlspringcontextservletorg.springframework.web.context.contextloaderservlet1完成的 web.xml 文件如下:contextconfiglocation/web-inf/applicationcontext.xmlspringcontextservletorg.springframework.web.context.contextloaderservlet1actionorg.apache.struts.action.actionservletconfig/web-inf/struts-config.xmldebug3detail30action*.doindex.jsp接下来我们来创建一个 action 的基类,在之后创建的 action 都继承于该基类。在 com.ssh.struts.action 创建一个类 baseaction.java,继承与 actionsupport。具体代码 如下。package com.ssh.struts.action;import org.springframework.web.context.webapplicationcontext;import org.springframework.web.context.support.webapplicationcontextutils;import org.springframework.web.struts.actionsupport;public class baseaction extends actionsupport protected object getbean(string id) webapplicationcontext ctx =webapplicationcontextutils.getwebapplicationcontext(this.servlet.getservletcontext();return ctx.getbean(id);这样就完成了 struts 和 spring 的整合了。接下来,该添加 hibernate 支持了。添加 hibernate 支持在工程 ssh 上右击,选择 myeclipse,再选择 add hibernate capabilities选择将 jar 包复制到/webroot/web-inf/lib 下选择 spring configuration file(applicationcontext.xml)选择 existing spring configuration file选择数据源,途中的 db driver:mysql 是我之前配置好的数据源,这里根据实际情况选择。不创建 sessionfactory。这样,hibernate 支持就添加好了,在 applicationcontext.xml 也自动配置了 hibernate的 sessionfactory。此时的 applicationcontext.xml 文件如下:org.hibernate.dialect.mysqldialect整理 jar 包三个框架都添加好了。但是由于 myeclipse 的过于强大,添加完三个框架后就有几个 jar 包重复了。得删掉几个 jar 包。我们就删位于/webroot/web-inf/lib 中的 jar 包。删掉 antlr-2.7.6.jar。antlr.jar 在 struts 1.2 libraries 中存在了。删除 asm-2.2.3.jar,lib 下有另外一 个文件 asm.jar。删除 commons-fileupload.jar,该包在 struts 1.2 libraries 中存在了。删除 commons-logging-1.0.4.jar 和 mons-logging.jar 在 struts 1.2libraries 中存在了。删除 log4j-1.2.11.jar。lib 下有另外一个版本的 jar 包:log4j-1.2.15.jar。 删除 struts.jar。该包在 struts 1.2 libraries 中存在了。总结一下,以下是需删除的 jar 包: antlr-2.7.6.jar asm-2.2.3.jar commons-fileupload.jar commons-logging-1.0.4.jar commons-logging.jar log4j-1.2.11.jar struts.jar然后我们将 struts 1.2 libraries 中所有的包都复制到 lib 下。jar 文件需要到 myeclipse的安装目录下去找。我这边的目录是 c:program filesgenuitecmyeclipseconfigurationorg.eclipse.osgibundles101.cpdata1.2lib,大家可以参考一下。在项目的 jar 包文件中 也看到这个目录。然后我们可以在工程里将 struts 1.2 libraries 删除。我在删 jar 包时,将struts.jar 这个 jar 包也删除了。如果不把 struts 1.2 libraries 里的 jar 包拷贝到 lib 里的话,系统可能会提示 struts 这个 jar 包无法找到。不解。如果不想把 jar 包拷过来的 话,可以不删除 struts.jar 这个 jar 包。建立目录架构建立包的目录,如下-com.ssh.dao-com.ssh.dao.impl-com.ssh.pojo-com.ssh.struts-com.ssh.struts.action-com.ssh.struts.form-com.ssh.service-com.ssh.service.impl生成 hibernate 反向工程如下图设置。id generator 选择 nativeid generator 同样选择 native接下来我们先新建一个接口。userdao.java。代码如下:package com.ssh.dao; import java.util.list; import com.ssh.pojo.user;public interface userdao public user findbyid(integer id);public list findbyname(object name);接下来我们将 com.ssh.pojo 下的 userdao.java 重名为为 userdaoimpl.java然后我们将 userdaoimpl.java 拖去 com.ssh.dao.impl 包中。点住左键不放,拖到com.ssh.dao.impl 包中即可。会出现 move 的对话框。点 ok 即可。然后要稍微修改一下 userdaoimpl.java 这个文件。因为这个文件应该是 userdao 接口的实现。所以修改一下吧。修改后的文件如下,红色字体是修改的部分。package com.ssh.dao.impl;import java.util.list;import mons.logging.log;import mons.logging.logfactory;import org.hibernate.lockmode;import org.springframework.context.applicationcontext;import org.springframework.orm.hibernate3.support.hibernatedaosupport;import com.ssh.dao.userdao;import com.ssh.pojo.user;/* a data access object (dao) providing persistence and search support for user* entities. transaction control of the save(), update() and delete() operations* can directly support spring container-managed transactions or they can be* augmented to handle user-managed spring transactions. each of these methods* provides additional information for how to configure it for the desired type* of transaction control.* see com.ssh.pojo.user* author myeclipse persistence tools*/public class userdaoimpl extends hibernatedaosupport implements userdao private static final log log = logfactory.getlog(userdaoimpl.class);/ property constantspublic static final string name = name;public static final string password = password;protected void initdao() / do nothingpublic void save(user transientinstance) log.debug(saving user instance); try gethibernatetemplate().save(transientinstance);log.debug(save successful); catch (runtimeexception re) log.error(save failed, re);throw re;public void delete(user persistentinstance) log.debug(deleting user instance);try gethibernatetemplate().delete(persistentinstance);log.debug(delete successful); catch (runtimeexception re) log.error(delete failed, re);throw re;public user findbyid(java.lang.integer id) log.debug(getting user instance with id: + id);try user instance = (user) gethibernatetemplate().get(com.ssh.pojo.user, id);return instance; catch (runtimeexception re) log.error(get failed, re);throw re;public list findbyexample(user instance) log.debug(finding user instance by example);try list results = gethibernatetemplate().findbyexample(instance);log.debug(find by example successful, result size: + results.size();return results; catch (runtimeexception re) log.error(find by example failed, re);throw re;public list findbyproperty(string propertyname, object value) log.debug(finding user instance with property: + propertyname+ , value: + value);try string querystring = from user as model where model.+ propertyname + = ?;return gethibernatetemplate().find(querystring, value); catch (runtimeexception re) log.error(find by property name failed, re);throw re;public list findbyname(object name) return findbyproperty(name, name);public list findbypassword(object password) return findbyproperty(password, password);public list findall() log.debug(finding all user instances);try string querystring = from user;return gethibernatetemplate().find(querystring); catch (runtimeexception re) log.error(find all failed, re);throw re;public user merge(user detachedinstance) log.debug(merging user instance);try user result = (user) gethibernatetemplate().merge(detachedinstance);log.debug(merge successful);return result; catch (runtimeexception re) log.error(merge failed, re);throw re;public void attachdirty(user instance) log.debug(attaching dirty user instance);try gethibernatetemplate().saveorupdate(instance);log.debug(attach successful); catch (runtimeexception re) log.error(attach failed, re);throw re;public void attachclean(user instance) log.debug(attaching clean user instance); try gethibernatetemplate().lock(instance, lockmode.none);log.debug(attach successful); catch (runtimeexception re) log.error(attach failed, re);throw re;public static userdaoimpl getfromapplicationcontext(applicationcontext ctx) return (userdaoimpl) ctx.getbean(userdao);编写 service接下来开始编写 service 接口和实现类。userservicedao.java 和 userservicedaoimpl.java代码如下:package com.ssh.service;import com.ssh.pojo.user;public interface userservicedao public user getuserbyid(integer id);public user getuserbyname(string name);package com.ssh.service.impl;import java.util.list;import com.ssh.dao.userdao;import com.ssh.pojo.user;import com.ssh.service.userservicedao;public class userservicedaoimpl implements userservicedao private userdao userdao;public user getuserbyid(integer id) / todo auto-generated method stubreturn userdao.findbyid(id);public user getuserbyname(string name) / todo auto-generated method stublist l = userdao.findbyname(name);if (l.size()=0) return null; else return (user)l.get(0);public userdao getuserdao() return userdao;public void setuserdao(userdao userdao) this.userdao = userdao;接口和实现类编写好了,现在要在 applicationcontext.xml 文件中添加相应的 bean 了。添加的代码如下:现在完整的 applicationcontext.xml 文件如下:org.hibernate.dialect.mysqldialectcom/ssh/pojo/user.hbm.xml创建 form、action 和 jsp创建 jsp修改 superclass,添加 forwards编写 loginaction.java 代码如下:/* generated by myeclipse struts* template path: templates/java/javaclass.vtl*/package com.ssh.struts.action;import javax.servlet.http.httpservletrequest; import javax.servlet.http.httpservletresponse; import org.apache.struts.action.actionform; import org.apache.struts.action.actionforward; import org.apache.struts.action.actionmapping;import com.ssh.pojo.user;import com.ssh.s
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 四川公务员真题2025
- 蚌埠事业单位笔试真题2025
- 第2课 信息处理的好助手说课稿-2023-2024学年小学信息技术(信息科技)第一册(供三年级使用)浙教版(广西)
- 2025加工承揽合同与销售代理合同的区别
- 广东省仲元中学高中信息技术教案实例解析Photoshop图层样式的各项命令
- Unit 3 Mysteries of the World说课稿-2025-2026学年高中英语重庆大学版选修十一-重大版2004
- 本册综合说课稿-2025-2026学年高中体育人教版2019必修第一册-人教版
- 劳动项目三 烹调酸辣汤说课稿-2023-2024学年初中劳动七年级上册人教版
- 六、运动和力的关系说课稿-2023-2024学年初中物理八年级全一册北京课改版
- 2017年高二人教版选修3-1第三章磁场第四节《通电导线在磁场中受到的力》教学设计
- 有效竞品分析(好产品竞品分析方法论)
- 2024年游泳初级指导员认证理论考试题库(浓缩500题)
- 地铁站内二次结构支模工程及支撑体系施工工艺技术
- 常见业务场景网络安全建设VISIO图合集(27个类型)v2023
- 新能源发电技术 电子课件 2.5 可控核聚变及其未来利用方式
- 移动互联网时代的信息安全与防护学习通超星期末考试答案章节答案2024年
- 体育与健康-《立定跳远》教学设计
- 人工智能训练师理论知识考核要素细目表一级
- GB/T 9799-2024金属及其他无机覆盖层钢铁上经过处理的锌电镀层
- ASME-第九卷焊接和钎焊评定标准-资料
- 国家机关事业单位工作人员受到行政刑事处罚工资处理意见
评论
0/150
提交评论