关于SSH框架整合

SSH架构作为目前主流的项目架构之一,即使用Struts2、Spring 、Hibernate这三个框架来搭建项目的主架构。由于Hibernate和Struts2没有直接联系,所以将Struts2和Hibernate对象交给Spring容器管理,增强系统的灵活性,简化配置,提高性能。其实就是要实现Spring与Struts2、Spring与Hibernate的整合,由Spring创建Action、由Spring完成持久化操作。下面主要介绍一下整合的过程
一、添加struts的支持

1.添加jar包:spring-struts.jar、struts2-core.jar、 xwork-core.jar等

2.添加配置文件:struts.xml

3.web.xml添加核心控制器:<filter>、<filter-mapping>

4.添加login.jsp,取消默认布局

5.添加了UserAction,并在配置文件中配置该action
二、添加spring支持

1.添加Spring相关jar包

2.添加配置文件:applicationContext.xml

3.在web.xml添加<context-param>指定spring配置文件的位置并通过监听器加载
三、添加hibernate支持

1.添加jar包:Hibernate.jar等

2.添加配置文件(借助MyEclipse Datebase Explorer):hibernate.cfg.xml

3.添加实体类和实体类的映射文件(Hibernate Reverse Engineering)
四、整合spring和hibernate

1.在spring配置文件中加载hibernate的配置文件,得到sessionFactory。把sessionFactory注入给Dao

2.添加dao层,Dao实现类必须继承HibernateDaoSupport

3.添加service层,注入dao

4.修改action层,注入service
五、整合spring和struts2

1.添加整合的jar包:spring-struts.jar

2.声明struts的action由spring提供,在struts.xml中添加

<constant name=”struts.objectFactory” value=”spring”></constant>

3.在spring配置文件中创建action类的bean。切记scope=”prototype”

4.在struts.xml中把action的class属性改为spring配置文件中的action bean的id
六、SSH整合的改进

1、创建多个源文件夹,把配置文件统一放到src/config文件夹里

2、添加IBaseDao,BaseDao,BaseService,BaseAction类

处理异常:普通Dao实现类调用BaseDao类的方法时很可能会出现异常,所以需要异常处理。

try-catch方法处理 或者 建立三个自定义异常,把异常往上抛:HibernateDaoSupportException继承DAOException、DAOException继承ServiceException。最后抛给Action,由Action处理异常。

3、配置c3p0数据源和创建SessionFactory

  • 添加c3p0.jar或者druid.jar
  • 添加jdbc.properties属性文件;在spring配置文件中加载属性文件;DataSource采用诸如 {jdbc.url}的方式获取属性值。</li><li>创建SessionFactory的Bean:引用数据源,配置Hibernate参数、加载持久化类</li></ul><p><strong>4、配置声明式事务</strong></p><p>事务的开启、提交、回滚、异常处理,是否只读都交给Spring容器来处理。</p><p>步骤:1.配置事务管理类&nbsp;2.配置事务属性&nbsp;3.配置事务AOP切入点</p><p>切入点一般在service的方法上,将事务切入到其中,实现事务管理</p><p><strong>5、拆分Spring配置文件</strong></p><ul><li>在web.xml中使用通配符指定多个配置文件的位置,并通过监听器加载</li><li>在applicationContext.xml配置文件中引用其他配置文件,配置位置具体。</li></ul><p><strong>6、拆分struts的配置文件</strong></p><ul><li><del></del>在struts.xml中定义全局结果,并引入其他的配置文件&lt;include file="struts/struts-*.xml" /&gt;</li><li>在struts-*.xml定义具体的Action,并继承主配置文件struts.xml定义的包名</li></ul><p><strong>7、日志功能添加</strong></p><p>日志级别由低到高:debug info warn error fatal 默认级别及以上级别的日志才会输出</p><p>日志记录地址:可以记录到控制台,也可以记录到项目所在位置</p><p>基本步骤:</p><p style="padding-left: 30px;">1.添加log4j.properties属性文件,并修改记录的位置和项目各层对应的记录文件名</p><p style="padding-left: 30px;">2.web.xml添加有关Log4j的配置属性,比如webAppRootKey参数</p><p style="padding-left: 30px;">3.使用SLF4J做日志,因为SLF4J是基于占位符的方法,独立于任何一个特定的日志实现</p><p style="padding-left: 30px;">&nbsp; &nbsp;在BaseAction类中 &nbsp;定义一个Logger类型的对象,从而可以调用slf4j的方法</p><p style="padding-left: 30px;">4.在UserAction &nbsp;添加记录日志的功能</p><p style="padding-left: 30px;">如:log.info("{} login success!!!",user.getUsername</p><p style="padding-left: 30px;">&nbsp; &nbsp; &nbsp; &nbsp;log.error("method login bug:{}",e);</p><p><strong>8、添加防止内存泄漏的监听器</strong></p><p>在服务器运行过程中,Spring不停的运行的计划任务和OpenSessionInViewFilter,使得Tomcat反复</p><p>加载对象而产生框架并用时可能产生的内存泄漏,则使用IntrospectorCleanupListener解决内存泄漏</p><p>在web.xml添加 &nbsp;使用监听器类 &nbsp;org.springframework.web.util.IntrospectorCleanupListener</p><p><strong>9、添加spring的中文过滤器</strong></p><p>解决中文乱码问题:使用过滤器类 org.springframework.web.filter.CharacterEncodingFilter</p><p><strong>10、添加hibernate的延迟加载过滤器</strong></p><p>在web.xml中添加,使用org.springframework.orm.hibernate3.support.OpenSessionInViewFilter<span style="color: #ff0000;"><br> 七、 Ajax提交表单+登录验证+增删改查</span></p><p><strong>1、模版与回调机制</strong></p><ul><li>模版模式设计思想:比如,HibernateTemplate提供常用方法的封装,简化编程。HibernateCallback提供对原始SessionAPI的调用,解决HibernateTemplate灵活性不足的问题(如:使用查询缓存执行查询)。两者互相配合,兼顾开发效率和灵活性,形成一种模版。</li><li><span style="line-height: 28.5px;">软件模块之间的接口按调用形式可以分为三类:同步调用、回调和异步调用。</span></li></ul><p>同步调用:一种阻塞式调用,调用方要等待对方执行完毕才返回,它是一种单向调用;</p><p>回<span xml:lang="EN-US" lang="EN-US">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr></span>&nbsp; 调:一种双向调用模式,被调用方在接口被调用时也会调用对方的接口;就是A类中调用B类中的某个方法C,然后B类中反过来调用A类中的方法D。D这个方法就叫回调方法</p><p>异步调用:一种类似消息或事件的机制,服务方在收到某种讯息或发生某种事件时,会主动通知客户方。调用方无需等待被调用方执行完毕才返回,而是让操作继续进行下去。</p><p style="line-height: 28.5px;" align="left">&nbsp; &nbsp;回调和异步调用的关系非常紧密:使用回调来实现异步消息的注册,通过异步调用来实现消息的通知。</p><p style="line-height: 28.5px;" align="left"><strong>2、简化Hibernate编码</strong></p><p style="line-height: 28.5px;" align="left">BaseDao实现类继承HibernateDaoSupport接口,并得到HibernateTemplate对象,从而调用相关方法save/saveOrUpadate/update/delete/find进行持久化操作。其他Dao类需继承BaseDao实现类</p><p style="line-height: 28.5px;" align="left"><strong>3、Ajax实现步骤</strong></p><p style="line-height: 28.5px;" align="left">创建XMLHttpRequest 对象-&gt;设置回调函数返回结果-&gt;初始化XMLHttpRequest组件-&gt;发送请求</p><p style="line-height: 28.5px;" align="left">通常使用jQuery实现AJAX,就不需要上述复杂步骤 ,使用 .ajax()或 . p o s t ( ) .get()方法即可

    . a j a x ( u r l :" " , t y p e :" " , d a t a :" " , d a t a T y p e :" " , s u c c e s s : f u n c t i o n ( d a t a ) ) ; < / p >< p s t y l e =" l i n e h e i g h t : 28.5 p x ; " a l i g n =" l e f t "> (function(){ .post({url:"地址",data:"数据",dataType:"返回类型",success:function(data){}});});</p><p style="line-height: 28.5px;" align="left"> (function(){ .get({url:"地址",data:"数据",dataType:"返回类型",success:function(data){}});});</p><p style="line-height: 28.5px;" align="left"><strong>4、使struts2支持json<br> </strong></p><p style="line-height: 28.5px; padding-left: 30px;" align="left">1.添加jar包</p><p style="line-height: 28.5px; padding-left: 30px;" align="left">2.修改struts.xml中extends="json-default"</p><p style="line-height: 28.5px; padding-left: 30px;" align="left">3.告诉struts我们定义的map的名字:在struts-admin.xml中添加。</p><p style="line-height: 28.5px; padding-left: 30px;" align="left">&lt;result type="json"&gt;&lt;param name="root"&gt;resultMap&lt;/param&gt;&lt;/result&gt;</p><p style="line-height: 28.5px;" align="left"><strong>5、使用页面公共文件taglib.jsp</strong></p><p style="line-height: 28.5px;" align="left">&nbsp; &nbsp; &nbsp; &nbsp;该文件引入了jstl、struts标签、定义了一些常用变量,值是js、css文件路径、项目绝对路径等。</p><p style="line-height: 28.5px;" align="left"><strong>6、使用jQuery Validate 插件实现验证</strong></p><p style="line-height: 28.5px; padding-left: 30px;" align="left">1.添加 jquery.validate.js文件,并将地址定义到taglib.jsp文件里</p><p style="line-height: 28.5px; padding-left: 30px;" align="left">2.jsp页面引入: {validate_js }

    3.将校验规则写到 js 代码中: ("#loginForm").validate({&nbsp;rules:{规则},messages:{中文提示}&nbsp;});</p><p style="line-height: 28.5px; padding-left: 30px;" align="left">4.验证通过才进行ajax请求&nbsp;if( (“#loginForm”).valid()){ajax代码}

    7、添加增删改查功能

    主要修改service层、action层、jsp页面和struts配置文件。

    取消默认跳转的行为:javascript:void(0); 或 #

    查询所有:将登录后的页面存到WEB-INF目录下面,通过转发查看页面 userList.jsp

    删除:service层、action层在添加delete方法,在jsp页面通过ajax异步提交操作

    成功,返回location.reload();失败,返回alter(date.retmsg);

    增加:service层、action层在添加add方法,先转发到增加页面,再Ajax操作

    转发,location.href=”toAdd.action”;提交,var options .ajaxSubmit(options);

    修改:service层、action层在添加update方法,先转发到更新页面,再Ajax操作

    if(id==null||id==”“){//add }else{//update }  成功,location.href(查看所有用户)
    八、分页+多表查询

    1.添加form表单,用于多表查询

    2.在dao层添加getUsersByPager()方法(注意这个方法的参数和返回值,利用到Pager.java)

    3.在service层添加getUsersByPager()方法

    4.在UserAction中修改了相关代码  getUsersByPager

    5.在userList.jsp中修改迭代处的代码

    <s:hidden name=”当前页、每页显示数”></s:hidden>

    添加分页显示代码  、跳转函数function jump(p,s){}

    6.添加了分页和多表查询功能

    将分页代码统一放到page.jsp下,通过include标签引入到查询页面
    九、注解与权限

    权限(认证+授权):

    1.先创建表(一定要创建外键)

    2.利用MyEclipse的hibernate reverse engineering生成实体类和映射文件

    3.对生成的内容稍作修改

    4.在applicationContext.xml中引入映射文件

    5.在dao层和service层添加代码

    6.在BaseService中注入dao

    7.在BaseAction中注入service

    8.修改相应的spring配置文件

    权限展示:

    1.拷贝后台模板

    2.login.jsp中:window.location.href=”${ctx}/main.html”;

    3.在UserAction中:putToSession(“loginRoleId”, tbuser.getTbRole().getId());

    4.在dao和service层中:public List<TbRight> getRightsByRoleId(int roleId) throws DAOException;

    5.新建RoleAction.java,添加代码(防止死循环)

    6.拷贝dtree插件,在left.jsp中引入

    7.在center.html中修改部分代码,src=”left.jsp”

    8.在tb_right表中添加right_url,使权限菜单链接生效

    注解:

    1.利用MyEclipse生成注解类,稍作修改

    2.在applicationContext.xml添加

    <context:annotation-config/>

    <context:component-scan base-package=”com.hfxt”/>

    对sessionFoactoy修改class属性

    <bean id=”sessionFactory”

    class=”org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean”>

    把对配置文件的引入改为对注解类的引入

    3.添加了dao,service,action的注解以及注入

    4.删除了相关的配置文件,并在web.xml修改spring配置文件的引入位置

    5.在applicationContext.xml添加:default-autowire=”byName”

    6.添加jar包

    7.对struts的注解

    7.1 添加jar包

    7.2 根据struts-t40.xml完成对UserAction.java的注解,然后删掉struts-t40.xml。最后删掉struts.xml对struts-t40.xml的引入

猜你喜欢

转载自blog.csdn.net/w252064/article/details/80060452