struts2+hibernate+Spring分层开发

web.xml中要加Spring的listener,struts2的filter的配置。
UI  struts2:      jsp   struts.xml   Action
Spring               Action Spring:applicationContent-actions.xml   Service   Spring:applicationContent-services.xml  DAO Entity 
DAL hibernate:   DAO Entity  .hbm.xml  数据库表
----------------------------------------------------------------------------------
DAL数据访问层-Hibernate
Hibernate根据数据库表,逆向工程生成实体类,*DAO类,*.hbm.xml.
数据库表---.hbm.xml---实体类
DAO类 extends BaseHibenateDAO得到session对象访问数据库表。
-------------------------------------------------------------------------------
BL业务逻辑层
自定义的一些Service类。(一般在其上加接口层,各Service类分别实现各接口)。各类有一些DAO类的私有变量、供UI层Action类调用的方法,实现时与实体数据库相关时调用DAO类私有变量的方法。Service类中不用显示的实例化DAO类变量--Spring。
-------------------------------------------------------------------------------
UI层-Struts2
Struts2以WebWork为核心,采用拦截器的机制来处理用户的请求,使得业务逻辑控制器能够与Servlet API完全脱离开.
浏览器发送一个请求。核心控制器FilterDispatcher根据请求决定调用合适的Action。
 
Action类:extends BaseAction extends ActionSupport, 有一些实体类/Service接口的私有变量,有set-getters,execute()方法。Action类中不用显示的实例化Service类(接口)的私有变量-Spring。
BaseAction类ActionSupport类均为struts2类库里自带,有HttpServletRequest/Response ,HttpSession ,ServletContext, PageContext .Page等类型的私有变量及get-setters。
 
*.jsp:
通过<%@ taglib prefix="s" uri="/struts-tags" %>引入struts2的标签库..jar.
通过<s:property value="serviceId"/>引用bean
通过<a href="...action?para='..'"/>、<form action="./..action" >、jQuery.post('./..action', params, function(){;}, 'json');请求。
 
struts.xml:
实现页面导航.<struts>下的<action>定义action名字与处理它的Action类,<action>下的<result>定义处理成功(等结果)后转向的jsp或action。还可设置全局的拦截器。
Struts2采用拦截器的机制来处理用户的请求,核心控制器FilterDispatcher根据请求查struts.xml调用匹配的Action。
默认执行Action类的execute()方法,return SUCCESS。
Action类里的私有变量可自动被赋值为页面请求携带的参数,页面通过<s:property value="..privateName"/>可直接调
用Action里的私有变量。
 
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Spring
spring是一个框架,他的最大特点是依赖注入DI-控制反转IoC。它相当于是struts和Hibernate的一个容器,管理着struts和Hibernate。
Spring的配置文件applicationContext.xml:
applicationContent-actions.xml:<beans>下许多<bean>:各Action类用到哪些Service类的实例  UI-BL
applicationContent-service.xml:各Service类的实例用到哪些DAO实例,各DAO实例属于的DAO类。BL-DAL
Service类中不用显示的实例化DAO类变量,Action类中不用显示的实例化Service类(接口)的私有变量,SpringIoC会把符合依赖关系的对象通过JavaBean属性(setter方法)或者构造函数传递给需要的对象。
 
 
-----------??????--------------------------------------------------------------------------------------------------------
Spring框架
为Hibernate延迟加载与DAO模式的整合提供了一种方便的解决方法。以一个Web应用为例,Spring提供了
OpenSessionInViewFilter和OpenSessionInViewInterceptor。
生成DAO类:extends HibernateDaoSupport,  initDao ,save, delete, findBy.. merge, attachDirty, attachClean, getFromApplicationContext方法.
 
 
事务处理?
1。DAO类的方法加事务处理:
public void save(Servicedecbyuser transientInstance) {
log.debug("saving Servicedecbyuser instance");
Transaction tx=null;
try {
tx=getSession().beginTransaction();
getSession().save(transientInstance);
tx.commit();
log.debug("save successful");
} catch (RuntimeException re) {
log.error("save failed", re);
throw re;
}finally{
getSession().close();
}
}
未加事务处理的原DAO,事务是未提交的:
public void save(Tags transientInstance) {
log.debug("saving Tags instance");
try {
getSession().save(transientInstance);
log.debug("save successful");
} catch (RuntimeException re) {
log.error("save failed", re);
throw re;
}
}
2。Spring接管事务,how?
原文地址:https://www.cnblogs.com/huapox/p/3251445.html