【Struts2+Hibernate3+Spring3】利用SSH整合,完成打印用户表,用户登录、注册、修改密码系统

来源:互联网 发布:打广告软件 编辑:程序博客网 时间:2024/04/25 02:46

本文视图尽可能不杂糅其它技术,尽可能少写代码,完成SSH整合。以致于各位在现有网上资料越来越天花龙凤之下,清晰地了解到传说中的三大框架SSH是怎么整合的。


一、SSH的下载

首先SSH的下载就已经是一个难点。SSH三个地方同时推出各自的新技术,已经要求利用Maven完成其版本的同步。毕竟Spring的版本,决定其能整合什么版本的Struts与Hibernate。Spring3.0.5就不能整合Hibernate4.x了。因此本文选取的SSH的版本,分别为struts 2.3.20 GA、Hibernate3.6.10Final、Spring3.0.5,最后有一个log4j,因为Spring3.0.5找不到log4j,不整合log4j就会每次在服务器启动时弹log4j警告!

1、struts 2.3.20 GA

直接在Struts2的官网(点击打开链接)下载最新版的Struts2,选择其中的Full版本,理论上只下载lib版本也可以,但是不知道会不会少了东西,而且最主要是笔者的网络下载lib版没有资源,所以直接下载Full版本,打开速度可能比较慢,也可以把http://222.200.129.45/mirrors.cnnic.cn/apache//struts/2.3.20/struts-2.3.20-all.zip这个下载地址复制到下载工具里面直接下载。

理论上,任意Struts2版本都是OK的,因为Spring3.0.5能整合所有Struts2.x,就只有Struts2够良心,一个版本可以用这么久,还不停地修复。

这里与《【Struts2】Struts2纯手工安装、配置以及Helloworld,以最新版struts 2.3.20 GA做例子》(点击打开链接)一文是完全相同的。

2、Hibernate3.6.10Final

此乃Hibernate3.x的最终版,这东西在其官网上已经绝迹了,我找了很久才找到其在Sourceforge官网的仓库,并且挖出下载地址,这里直接给出,大家利用下载工具下载,不要在下载这里面折腾了。

http://222.200.129.44/ncu.dl.sourceforge.net/project/hibernate/hibernate3/3.6.10.Final/hibernate-distribution-3.6.10.Final-dist.zip

这里,为了大家方便,我还在CSDN上传了一份http://download.csdn.net/detail/yongh701/8685299

3、Spring3.0.5

因为在Spring3.0.5所有Spring的依赖包都是需要通过Maven来运行,不能够自己解压的lib文件夹。我们不用Maven。Spring这个东西自己提倡解耦什么的,自己必须通过一堆依赖包才能运行,真开国际玩笑!

这些框架的官网改来改去,然后在强推其新技术,把历史版本通通都去掉了。正如ExtJs一样,只能在国内的网上搜一下历史版本下载。要搞Spring3.0.5必须要下两个压缩包。一个是spring-framework-3.0.5.RELEASE-with-docs.zip,另一个是spring-framework-3.0.5.RELEASE-dependencies.zip。

这里感谢,两位CSDN网友无私提供的下载地址。

spring-framework-3.0.5.RELEASE-with-docs.zip:http://download.csdn.net/download/hl518_818/4796207

spring-framework-3.0.5.RELEASE-dependencies.zip:http://download.csdn.net/download/u014175572/7514027

4、log4j

这个版本真心没有所谓了。

Log4j则打开Apache的官网(点击打开链接)如下图,选择log4j-1.2.17.zip(Windows)或者log4j-1.2.17.tar.gz(Linux)。同样是Apache的东西,在一些高版本的Tomcat中还整合了这个东西,当然,你最好还是在WEB-INF\lib目录下补上这个包,以致于你的工程在所有Tomcat都能跑。


二、SSH的配置

这里讲述,在Eclipse for JavaEE中新建一个Dynamic Web Project工程之后,怎么把刚才下载好的各个压缩包里面的东西拷贝到里面的WEB-INF\lib文件夹。

1、struts 2.3.20 GA

下载之后解压把lib中的:


jar包复制到你web工程的lib文件里面,不要抱有为了省时间,或者什么的,把lib中的所有包,复制到WEB-INF\lib文件夹的幻想。这样反而启动不了工程,即使web.xml写好了,也会有Dispatcher initialization failed的问题。

这里比《【Struts2】Struts2纯手工安装、配置以及Helloworld,以最新版struts 2.3.20 GA做例子》(点击打开链接)一文中拷贝的lib,多了一个struts2-spring-plugin-2.3.20.jar。

2、Hibernate3.6.10Final

把hibernate-distribution-3.6.10.Final解压之后,把根目录的hibernate3.jar,hibernate-distribution-3.6.0.Final\lib\required下的所有jar,hibernate-distribution-3.6.0.Final\lib\jpa下的所有jar也就是那个hibernate-jpa-2.0-api-1.0.1.Final.jar,hibernate-distribution-3.6.0.Final\lib\optional\c3p0下的所有包,也就是那个c3p0-0.9.1.jar,拷贝到WEB-INF\lib文件夹。

3、Spring3.0.5

把spring-framework-3.0.5.RELEASE-dependencies的所有东西,与spring-framework-3.0.5.RELEASE\dist中的所有Jar包不包括那个LIBD文件,拷贝到Java工程下的lib文件夹。这里拷贝的东西与《【Spring】Spring3.0.5的下载、配置与Helloworld》(点击打开链接)一文完全一样。

4、Log4j

apache-log4j-1.2.17下的log4j-1.2.17.jar,拷贝到WEB\lib文件夹。

5、JDBC

因为Hibernate3.x至少依附一种数据库连接运行,这里用Mysql做例子,因此你还需要一个Java连接Mysql的数据库包mysql-connector-java-5.1.32.jar,这东西网上一搜一大把,你也可以在Mysql的官网下载,但是这个Mysql官网打开很慢的,经常打不开。版本不重要,版本号5打头就行了。

我也在CSDN中上传了一份:http://download.csdn.net/detail/yongh701/8685375

反正你搞好之后,工程的WEB-INF\lib如下所示:


所有的SSH的包,我已经打包上传了,懒人可以直接下载,http://download.csdn.net/detail/yongh701/8685335,希望自己体验整个下载过程的,可以参照上面的文字。


三、制作目标

下面我们要利用SSH整合,配合Mysql中的一张Usertable用户信息表:


制作如下的一个用户系统,具有打印用户表,用户登录、注册、修改密码系统的功能,程序是健壮的,各个情况给出提示:



四、基本思想

首先贴上整个工程的目录结构。这里我没有在Service搞一个impl接口,dao中又搞一个hibernate接口,去符合那种所谓的制作规范。仅希望以最简单的方式,把这个脉路呈现给大家。


我们先要再WEB-INF下面的applicationContext.xml利用Spring整合Hibernate,在web.xml启动Log4j,Struts2,Spring3,顺序不能错。在log4j.properties搞好Log4j。

其中src中,struts.xml定义好各个Action的流向。各个包中分别完成各层的动作。配合前台页面,也就完成如下的MVC结构,这里配合下面的Java文件大家就应该明白了。


仅仅是index.jsp,result.jsp展示给用户,index.jsp是用户填表单的页面,输入页,result.jsp是结果显示页,结果输出页。


五、制作过程

1、先搞web.xml与log4j.properties,这里主要启动Log4j,Struts2,Spring3,利用Spring整合Log4j。这里的内容在《【Spring】Spring在JavaWeb工程中整合log4j》(点击打开链接)一文已经说过了。唯一的不同,是这里仅输出ERROR的信息。此工程运行之后会在C盘多出一个c:\log.txt。

web.xml:

<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns="http://java.sun.com/xml/ns/javaee"xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"version="3.0"><!-- Log4j配置 --><listener><listener-class>org.springframework.web.util.Log4jConfigListener</listener-class></listener><!-- 指定Log4j的配置文件所在目录。默认配置在WEB-INF目录下 --><context-param><param-name>log4jConfigLocation</param-name><param-value>/WEB-INF/log4j.properties</param-value></context-param><!-- Struts2配置 --><filter><filter-name>struts2</filter-name><filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class></filter><filter-mapping><filter-name>struts2</filter-name><url-pattern>/*</url-pattern></filter-mapping><!-- Spring配置 --><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><!-- 指定Spring Bean的配置文件所在目录。默认配置在WEB-INF目录下 --><context-param><param-name>contextConfigLocation</param-name><param-value>/WEB-INF/applicationContext.xml</param-value></context-param></web-app>  
log4j.properties:

#log4j.rootLogger = [ level ] , appenderName, appenderName, ...log4j.rootLogger = ERROR, console, R#Consolelog4j.appender.console = org.apache.log4j.ConsoleAppenderlog4j.appender.console.layout = org.apache.log4j.PatternLayoutlog4j.appender.console.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [%c]-[%p] %m%n#Filelog4j.appender.R = org.apache.log4j.RollingFileAppenderlog4j.appender.R.File = c:/log.txtlog4j.appender.R.MaxFileSize = 500KBlog4j.appender.R.MaxBackupIndex = 1log4j.appender.R.layout = org.apache.log4j.PatternLayoutlog4j.appender.R.layout.ConversionPattern=%-d{yyyy-MM-dd HH:mm:ss} [%c]-[%p] - %m%n
2、applicationContext.xml,这里完成Spring整合JDBC、Hibernate的过程,同时指明层与层的关系。这个文件其实根本无须像网上的例子那样写得天花龙凤!

<?xml version="1.0" encoding="UTF-8"?><beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns="http://www.springframework.org/schema/beans"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsd"><!-- 所谓的“data source 基本的连接池” --><!-- 其实就是Spring整合JDBC,写好数据库链接的基本信息,没必要再多开一个关于JDBC的配置文件了 --><bean id="dataSource"class="org.springframework.jdbc.datasource.DriverManagerDataSource"><property name="driverClassName" value="com.mysql.jdbc.Driver" /><property name="url" value="jdbc:mysql://localhost:3306/test" /><property name="username" value="pc" /><property name="password" value="admin" /></bean><!-- Spring整合Hibernate,有了这段,配合上一段,再也不用写Hibernate.cfg.xml那个文件了 --><bean id="sessionFactory"class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"><property name="dataSource" ref="dataSource" /><!-- 所谓的“设置数据库方言”,只是指明,我要链接Mysql数据而已! --><property name="hibernateProperties"><props><prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop></props></property><!-- 所谓的“指明了使用注释的持久类的包”,这里主要是写明映射数据库表的Java在哪里 --><property name="packagesToScan"><list><value>model</value></list></property></bean><!-- 在Dao中使用hibernateTemplate --><bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate"><property name="sessionFactory" ref="sessionFactory" /></bean><bean id="usertableDao" class="dao.UsertableDao"><property name="hibernateTemplate" ref="hibernateTemplate" /></bean><!-- 在Service中使用DAO --><bean id="usertableService" class="service.UsertableService"><property name="usertableDao" ref="usertableDao"></property></bean><!-- 在Action中使用Service --><bean id="webAction" class="action.webAction"><property name="usertableService" ref="usertableService"></property></bean></beans>
3、之后,我们先写持久层M,model包下的Entity实体Usertable.java。采用注解的方式,就不用再额外开一个.xml了。这部分在《【Hibernate】Hibernate的在Eclipse+Mysql的配置、安装,纯Java,利用Annotation与HQL完成数据库的增删改查》(点击打开链接)已经写过,不再解释了。Hibernate4.x的写法与Hibernate3.x一模一样。如果SSH每一个技术都要写一次,那么本文会长死。

package model;import javax.persistence.*;@Entity@Table(name = "usertable")public class Usertable {private int id;private String username;private String password;// 表示主键与自动生成项@Id@GeneratedValuepublic int getId() {return id;}public void setId(int id) {this.id = id;}@Column(name = "username")public String getUsername() {return username;}public void setUsername(String username) {this.username = username;}@Column(name = "password")public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}@Overridepublic String toString() {return id + "," + username + "," + password;}}
4、之后完成持久层M的数据逻辑业务包dao下的UsertableDao.java,这里就是《【Hibernate】Hibernate的层次划分,Hibernate4.3的初始化的新写法》(点击打开链接)中的HQL.java,主要是抽象出一些数据库的增删改查的内容。这里的SessionFactory已经注入到hibernateTemplate,各个变量的初始化已经在那个applicationContext.xml完成了,补上hibernateTemplate的getter与setter就可以直接用。利用hibernateTemplate抽象出取单一变量、利用HQL取变量,插入、修改的方法。

package dao;import java.util.List;import model.Usertable;import org.springframework.orm.hibernate3.HibernateTemplate;@SuppressWarnings("unchecked")public class UsertableDao {private HibernateTemplate hibernateTemplate;public HibernateTemplate getHibernateTemplate() {return hibernateTemplate;}public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {this.hibernateTemplate = hibernateTemplate;}public List<Usertable> findAllbyHQL(String hql) {return hibernateTemplate.find(hql);}public Usertable findOnebyKey(String key,String value){List<Usertable> queryResultList=hibernateTemplate.find("from Usertable u where u."+key+"=?",value);if(queryResultList.size()>0){return queryResultList.get(0);}else{return null;}}public void insert(Usertable usertable){hibernateTemplate.save(usertable);}public void update(Usertable usertable){hibernateTemplate.update(usertable);}}
5、之后是控制层Controll的逻辑业务包service下的UsertableService.java,通过与dao层的交互,完成取出用户表、判断用户是否存在、取出该用户名对应的密码、保存用户、修改该用户对应的密码的动作。是dao与action的中间层,完成彼此的交互。

package service;import java.util.List;import model.Usertable;import dao.UsertableDao;public class UsertableService {//配置Daoprivate UsertableDao usertableDao;public UsertableDao getUsertableDao() {return usertableDao;}public void setUsertableDao(UsertableDao usertableDao) {this.usertableDao = usertableDao;}//各个业务public List<Usertable> getUsertable() {return usertableDao.findAllbyHQL("from Usertable");}public boolean isUserExist(String username){if(usertableDao.findOnebyKey("username", username)==null){return false;}else{return true;}}public String getPasswordByUsername(String username){return usertableDao.findOnebyKey("username", username).getPassword();}public void saveUser(String username,String password){Usertable usertable=new Usertable();usertable.setUsername(username);usertable.setPassword(password);usertableDao.insert(usertable);}public void modifyPasswordByUsername(String username,String newpassword){Usertable usertable=usertableDao.findOnebyKey("username", username);usertable.setUsername(username);usertable.setPassword(newpassword);usertableDao.update(usertable);}}
6、接着是Controll层的网页参数处理包Action下的webAction.java,配合struts.xml完成index.jsp传递过来的参数的处理,同时把结果推到result.jsp。其实这里也就是struts.xml功能。记得,由于登陆、注册、修改密码,是公用一个result.jsp页面,因此每次输出之前,都要清空一下输出的信息。

package action;import java.util.List;import service.UsertableService;import model.Usertable;//这是Struts2必要的支持  import com.opensymphony.xwork2.ActionSupport;//防止报序列号警告  @SuppressWarnings("serial")// Struts2必须继承这个类public class webAction extends ActionSupport {// 与页面交互的参数private List<Usertable> usertableList;private String username;private String password;private String msg;private String newpassword;// 底层服务private UsertableService usertableService;// 实现方法// 得到整张用户信息表public String query() {msg = "";usertableList = usertableService.getUsertable();return "success";}// 用户登录public String login() {usertableList = null;if (!usertableService.isUserExist(username)) {msg = "用户名不存在!";} else {if (password.equals(usertableService.getPasswordByUsername(username))) {msg = "登录成功!";} else {msg = "密码错误!";}}return "success";}// 用户注册public String register() {usertableList = null;if (usertableService.isUserExist(username)) {msg = "用户名已存在!";} else {usertableService.saveUser(username, password);msg = "注册成功!";}return "success";}// 修改密码public String modifyPassword() {usertableList = null;if (!usertableService.isUserExist(username)) {msg = "用户名不存在!";} else {if (password.equals(usertableService.getPasswordByUsername(username))) {usertableService.modifyPasswordByUsername(username,newpassword);msg = "修改密码成功!";} else {msg = "密码错误!";}}return "success";}// eclipse自动生成// 页面传递过来的参数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 getMsg() {return msg;}public void setMsg(String msg) {this.msg = msg;}public String getNewpassword() {return newpassword;}public void setNewpassword(String newpassword) {this.newpassword = newpassword;}public List<Usertable> getUsertableList() {return usertableList;}public void setUsertableList(List<Usertable> usertableList) {this.usertableList = usertableList;}// 底层服务public UsertableService getUsertableService() {return usertableService;}public void setUsertableService(UsertableService usertableService) {this.usertableService = usertableService;}}
7、struts.xml

这里与《【Struts2】Struts2纯手工安装、配置以及Helloworld,以最新版struts 2.3.20 GA做例子》(点击打开链接)一文中的struts.xml仅仅是多出了允许spring整合struts.xml的句子<constant name="struts.objectFactory" value="spring"/> ,其它配置动作,完全一样。

<?xml version="1.0" encoding="UTF-8"?>  <!DOCTYPE struts PUBLIC          "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"          "http://struts.apache.org/dtds/struts-2.0.dtd">  <struts>      <!--使用spring创建管理struts2的action操作 -->      <constant name="struts.objectFactory" value="spring"/>      <!-- 在action包里面 -->      <package name="action" extends="struts-default">          <!-- action为query的执行方法就是action包中的webAction.java中的query方法 -->          <!-- 查询表 -->        <action name="query" class="webAction" method="query">              <result name="success">/result.jsp</result>          </action>        <!-- 用户登录 -->        <action name="login" class="webAction" method="login">              <result name="success">/result.jsp</result>          </action>        <!-- 用户注册 -->        <action name="register" class="webAction" method="register">              <result name="success">/result.jsp</result>          </action>        <!-- 修改密码 -->        <action name="modifyPassword" class="webAction" method="modifyPassword">              <result name="success">/result.jsp</result>          </action>    </package>  </struts>   
8、最后是两个页面,index.jsp与result.jsp,只是利用了ognl表达式也就是s标签完成超级链接、表单、后台结果输出等基本操作,这在《【Struts2+Hibernate4】按照MVC思想使用Hibernate查询数据库,并且在前台使用OGNL表达式输出》(点击打开链接)已经说过了。这里不再赘述。

index.jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%><%@ taglib prefix="s" uri="/struts-tags"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>查询</title></head><body><a href="./query">打印整张用户表</a><br />用户登录:<s:form action="./login" method="post"><s:textfield name="username" label="用户名"></s:textfield><s:password name="password" label="密码"></s:password><s:submit value="登陆"></s:submit></s:form>用户注册:<s:form action="./register" method="post"><s:textfield name="username" label="用户名"></s:textfield><s:password name="password" label="密码"></s:password><s:submit value="注册"></s:submit></s:form>修改密码:<s:form action="./modifyPassword" method="post"><s:textfield name="username" label="用户名"></s:textfield><s:password name="password" label="密码"></s:password><s:password name="newpassword" label="新密码"></s:password><s:submit value="修改"></s:submit></s:form></body></html>
result.jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8"    pageEncoding="UTF-8"%><%@ taglib prefix="s" uri="/struts-tags"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>结果</title></head><body><s:if test="usertableList">          userTable表如下:          <table border="1"><s:iterator value="usertableList" id="usertable"><tr><td>${usertable.id}</td><td>${usertable.username}</td><td>${usertable.password}</td></tr></s:iterator></table></s:if><s:if test="#msg==null">        ${msg}</s:if><s:a href="index.jsp">返回</s:a></body></html>

六、总结

至此,把这个SSHTest挂载到Tomcat里面运行就OK了。你的c:\log.txt里面不应该有任何错误。

这种分层的结构就是,你写任何表,都要为其创建实体,都要为其写相应的方法,在applicationContext.xml中注入,如果出现新的逻辑业务则要在service创建新的方法,之后才能再webAction中调用此方面,当然,webAction中的代码就会因此而短了不少。整个团队也划分出写各层的认识。基本上dao方法还可以多次复用。

规范各个程序猿编码,无论工程之后写到如何庞大,分层依然清晰。但是,代码的开发量,写一个功能其实多出了很多代码的。

比如这样的功能,根本就是用Servlet能够轻松完成的,详见《【Servlet】根据MVC思想设计用户登陆、用户注册、修改密码系统》(点击打开链接)。

无论怎么都好,这就是Struts2.3.20 GA+Hibernate3.6.10 Final+Spring3.0.5+JDBC+Log4j的整合例子,让网上那些天花龙凤资料见鬼去吧!简直要命,我搞这个工程,折腾了很久。

同时附上一份Eclipse for Javaee源码给大家参考:http://download.csdn.net/detail/yongh701/8685407

18 1
原创粉丝点击