1、SSH整合练习开发Struts2、Hibernate、Spring集成程序Myeclipse10.7下struts2.1 spring3.1 hibernate4.1整合图解ssh整合遇到的问题很多,这里只对我自己遇到的问题进行一个详细的说明:1、create a web project ,输入项目名称,选择java 6.0 ,finish。 二、选择新建的项目名,右键myeclipse ,选择add struts capabilities选择struts2.1,并选择filter name 默认只struts2,url pattren为/*,点击next.为新建的项目选择struts2 li
2、braries,如图所示至此添加struts2 完成项目目录如下图3、搭建spring右键选择工程名,选择myeclipse,然后add spring capabilities选择spring3.1 ,并添加三个包,spring core libraries,spring persistence libraries和spring web libraries,JAR Library Installation里选择Copy到lib目录(避免发布到Tomcat时出问题)。生成applicationContext.xml文件,Folder中选择WebRoot/WEB-INF,然后点击Finish。 至
3、此搭建spring完成4、搭建hibernate 选择项目工程名,右键myeclipse,add hibernate capabilities选择hibernate 4.1 ,并选择两个包,拷贝到lib文件夹下面选择使用spring管理hibernate选择使用spring 配置文件这里进行数据库链接配置,我的数据库连接已经完成,所以就直接选择不创建 SessionFactory至此ssh环境就搭建完成了6、启动tomcat,进行测试环境是否成功 在web.xml中添加配置 org.springframework.web.context.ContextLoaderListener contex
4、tConfigLocation /WEB-INF/applicationContext.xml 在浏览器中测试结果8、在此基础上进一步搭建扩展,开发一个简单的用户登陆程序 首先编辑index.jsp,如图所示 再分别新建error.jsp和welcome.jsp error.jspwelcome.jsp 添加反向工程 打开myeclipse的hibernate explorer perspective,打开mysql数据库连接,选中用户user表,生成与数据库对应的java对象和映像文件点击next,选择id的生成方式nativeNext-Finish。创建loginaction LoginA
5、ction.java代码:import java.util.List;import org.hibernate.Query;import org.hibernate.Session;import org.hibernate.SessionFactory;import com.opensymphony.xwork2.*;/* * author Administrator * 用户登录的action */public class LoginAction extends ActionSupport private String username; private String password; p
6、rivate SessionFactory sessionFactory; / private String message;public String execute() throws Exception Session session = sessionFactory.openSession(); String sql=from User u where u.username=? and u.password=?; Query query= session.createQuery(sql); query.setParameter(0, username); query.setParamet
7、er(1, password); List user = query.list(); session.close(); if(user.size()0) return success; else return error; /* * get and set 方法 * return */ public String getUsername() return username;public void setUsername(String username) this.username = username;public String getPassword() return password;pu
8、blic void setPassword(String password) this.password = password;public SessionFactory getSessionFactory() return sessionFactory;public void setSessionFactory(SessionFactory sessionFactory) this.sessionFactory = sessionFactory; 配置struts.xml /welcome.jsp /error.jsp 配置spring,ApplicationContext.xml。 org
9、.hibernate.dialect.MySQLDialect ./User.hbm.xml 增加struts.properties在src目录下,增加struts.properties,代码,struts.objectFactory=spring或者在struts.xml中添加 9、将项目添加到tomcat中,启动tomcat,这时似乎一切进行的很顺利,但是会发现项目无法正常运行,这是由于在添加struts spring 和hibernate时候使用的是myeclipse自带的jar报,会出现包冲突的情况。 解决办法:关掉tomcat,进入tomcat目录下的webapps目录,找到SSH项目所在,进入web-inf/lib目录下,找到名字相同版本不同的jar包,以保留高版本的原则进行删除
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1