已阅读5页,还剩9页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
搭建Struts2.3.16,Hibernate4.3.1和Spring4.0.2环境1 下载包hibernate:/orm/downloads/struts2:/download.cgispring4:/release/org/springframework/spring/4.0.2.RELEASE/2 新建空Web Project3 复制struts2常用包到WebRoot/WEB-INF/lib下4 修改web.xml index.jsp struts2 org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter struts2 /* 在WEB-INF下新建jsp文件夹,用来保存jsp文件,也可以直接放在WebRoot下。放在WEB-INF下的好处是只有服务端可以访问。/jsp/Login.jsp: Login /jsp/Welcome.jsp Welcome Welcome! name: $name pwd: $pwd 5 在src目录下新建struts.xml文件struts.xml /WEB-INF/jsp/Login.jsp /WEB-INF/jsp/Welcome.jsp 在src下新建DemoAction.javapackage com.test.struts2;import com.opensymphony.xwork2.ActionSupport;public class DemoAction extends ActionSupport private String name; private String pwd; public String execute() return SUCCESS; public String login() return SUCCESS; public String auth() return SUCCESS; public String getName() return name; public void setName(String name) = name; public String getPwd() return pwd; public void setPwd(String pwd) this.pwd = pwd; 现在的工程结构图:struts就搭好了。访问http:/localhost:8081/demo/Login查看结果。暂时没有做登录验证。只是显示输入的用户名和密码。4 复制hibernate常用包到/WebRoot/WEB-INF/lib下先把lib/required下面的包复制到lib中,然后下载mysql-connector-java-5.1.7-bin.jar 这个包加入。可能会下载到不同的版本,都无所谓。新建Account类:package com.test.model;public class Account private Long id; private String name; private String pwd; public Long getId() return id; public void setId(Long id) this.id = id; public String getName() return name; public void setName(String name) = name; public String getPwd() return pwd; public void setPwd(String pwd) this.pwd = pwd; 新建Account类和数据库表的映射文件Account.hbm.xml: 在本地MySQL或者其他数据库新建数据库demo。新建hibernate配置文件: com.mysql.jdbc.Driver jdbc:mysql:/localhost:3306/demo root root 5 org.hibernate.dialect.MySQLDialect true create thread 根据自己的实际情况填写。现在只要本地可以连接数据库就OK了。新建IDao接口:package com.test.dao.impl;import java.util.List;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.cfg.Configuration;import com.test.dao.IDao;public class IDaoImpl implements IDao private static final SessionFactory sessionFactory; static try sessionFactory = new Configuration() .configure().buildSessionFactory(); catch (Throwable ex) throw new ExceptionInInitializerError(ex); Override public T get(String hql) Session session = sessionFactory.openSession(); try session.beginTransaction(); List list = session.createQuery(hql).list(); if (list != null & list.size() 0) return (T) list.get(0); return null; finally session.getTransaction().commit(); session.close(); 现在Hibernate已经和struts整合完成。实现了用户登录数据库验证。5 搭建Spring环境导入libs/下的*RELEASE.jar文件就可以了。下载commons-dbcp-1.4.jar,commons-pool-1.6.jar包。这两个包跟数据连接池有关。为了实现Service层和dao层的分离,新建Service层。Iservice.java:package com.test.service;public interface IService public T get(String hql); IServiceImpl.javapackage com.test.service.impl;import com.test.dao.IDao;import com.test.service.IService;public class IServiceImpl implements IService private IDao dao; public IDao getDao() return dao; public void setDao(IDao dao) this.dao = dao; Override public T get(String hql) / TODO Auto-generated method stub return dao.get(hql); 为了使用spring,新建applicationContext.xml com/test/model/Account.hbm.xml org.hibernate.dialect.MySQLDialect true true !- - PROPAGATION_REQUIRED 现在Hibernate.cfg.xml已经没用了,删掉这个文件。为了struts和spring结合,加入struts2-spring-plugin-2.3.16.jar包。web.xml index.jsp contextConfigLocation /WEB-INF/classes/applicationContext.xml org.springframework.web.context.ContextLoaderListener struts2 org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter struts2 /* struts.xml /WEB-INF/jsp/Login.jsp /WEB-INF/jsp/Welcome.jsp /WEB-INF/jsp/error.jsp DemoAction.javapackage com.test.struts2;import com.opensymphony.xwork2.ActionSupport;import com.test.model.Account;import com.test.service.IService;public class DemoAction extends ActionSupport private IService service; private String name; private String pwd; public String execute() return SUCCESS; public String login() return SUCCESS; public String auth() String hql = from Account where name= + name + and pwd= + pwd + ; Account account = (Account) service.get(hql); return account = null ? error : SUCCESS; public String getName() return name; public void setName(String name) = name; public String getPwd() return pwd;
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 物流行业货物安全与运输效率提升制度
- 文娱产业内容审查与分级制度
- 医疗机构医疗服务质量监督制度
- 制造业安全生产标准规范制度
- 年产600套万汽车电磁阀200万套电机组件生产线项目可行性研究报告模板-立项申报用
- 响水《安全监理岗》技能专项训练卷
- 模块二 题库及答案
- 护理质控效果评价与改进课件
- 麻醉药品和精神药品使用管理培训考核试题
- 初中沪教版第2节 常见的酸和碱教案设计
- NBT 20299-2014 核电厂温排水环境影晌评价技术规范
- 人工智能教学设计案例初中数学
- 2024年高级统计实务考试真题及答案解析
- DB64+1858-2022+农业气象观测规范宁夏菜心
- 建立模糊专家系统实验报告
- 爱情片《百万英镑》台词-中英文对照
- 基于solidworks的齿轮泵仿真
- 半导体物理学(刘恩科)第七版-完整课后题答案
- 政策监控案例北京动物园搬迁风波
- 理气药的药理作用(中药药理学课件)
- 霍金斯能量层级(全)
评论
0/150
提交评论