webwork+hibernate 框架
来源:互联网 发布:七彩云南淘宝店真的吗 编辑:程序博客网 时间:2024/05/04 11:34
spring + webwork + hibernate 的项目开发习惯了,突然接触到只用webwork + hibernate 的项目,没有了spring对整个框架的管理,很是不习惯,于是就从借鉴了一下Webwork与Hibernate结合开发的通用模块了,感觉整个结构也比较清晰,就随手整理了过来。
通用类:
HibernateSessionFactory.java
HibernateSessionFactoryImp.java
HibernateSession.java
HibernateSessionImp.java
AbstractAction.java
拦截器:
HibernateInterceptor.java
一.通用类
1.HibernateSessionFactory.java中主要封装了Hibernate的SessionFactory实例.
private static final Log LOG = LogFactory.getLog(SessionFactoryService.class);
private SessionFactory sf;
//返回SessionFactory实例
public HibernateSessionFactory getSessionFactory() ...{
return sf;
}
//初始化生成SessionFactory
public void init() ...{
try ...{
sf = new Configuration().configure().buildSessionFactory();
}
catch (Exception e) ...{
LOG.error("error generate SessionFactory", e);
throw new RuntimeException( e.getMessage() );
}
}
//关闭SessionFactory
public void dispose() ...{
try ...{
sf.close();
}
catch (Exception e) ...{
LOG.error("error closing HibernateSession", e);
}
}
public String getDialect() ...{
return Environment.getProperties().getProperty(Environment.DIALECT);
}
}
2.HibernateSessionFactoryImp.java是一个Interface,供HibernateSession实现,从而让webwork拦截器自动调用其中的方法.
public void setSessionFactoryService(SessionFactoryService sfs);
}
3. HibernateSession.java封装了一个Session实例,并且实现了HibernateSessionFactoryImp接口从而在加入拦截器申明以后(后文会介绍)会通过webwork的拦截器自动调用其中的setHibernateSessionFactory()方法,从而自动将HibernateSessionFactory实例注入到了HibernateSession中,供HibernateSession调用
private static final Log LOG = LogFactory.getLog(SessionService.class);
private Session session;
private Transaction transaction;
private HibernateSessionFactory hsf;
private boolean rollBackOnly;
//返回一个Session实例,并开始一个事务
public Session getSession() throws HibernateException ...{
if (session==null) ...{
session = hsf.getSessionFactory().openSession();
transaction = session.beginTransaction();
}
return session;
}
//注入HibernateSessionFactory实例
public void setHibernateSessionFactory(HibernateSessionFactory hsf) ...{
this.hsf = hsf;
}
//关闭一个Session
public void disposeSession() throws HibernateException ...{
LOG.debug("disposing");
if (session==null) return;
if (rollBackOnly) ...{
try ...{
LOG.debug("rolling back");
if (transaction!=null) transaction.rollback();
}
catch (HibernateException e) ...{
LOG.error("error during rollback", e);
throw e;
}
finally ...{
session.close();
session=null;
transaction=null;
}
}
else ...{
try ...{
LOG.debug("committing");
if (transaction!=null) transaction.commit();
}
catch (HibernateException e) ...{
LOG.error("error during commit", e);
transaction.rollback();
throw e;
}
finally ...{
session.close();
session=null;
transaction=null;
}
}
}
//事务回滚
public boolean isRollBackOnly() ...{
return rollBackOnly;
}
public void setRollBackOnly(boolean rollBackOnly) ...{
this.rollBackOnly = rollBackOnly;
}
}
4.同样HibernateSessionImp.java也是一个接口,供AbstractAction实现,从而使Webwork自动调用其中的
public void setHibernateSession(HibernateSession hs);
}
5.让我们来看看AbstractAction.java
private static final Log LOG = LogFactory.getLog(AbstractAction.class);
private HibernateSession hs;
//重写Webwork的execute()方法
public String execute() throws Exception ...{
// 如果有错返回到INPUT所指定的页面
if ( hasErrors() ) ...{
LOG.debug("action not executed, field or action errors");
LOG.debug( "Field errors: " + getFieldErrors() );
LOG.debug( "Action errors: " + getActionErrors() );
return INPUT;
}
LOG.debug("executing action");
return go();
}
protected abstract String go() throws HibernateException;
//注入HibernateSession实例
public void setSessionService(SessionService ss) ...{
this.ss = ss;
}
public SessionService getSessionService() ...{
return ss;
}
//返回Session实例
protected Session getSession() throws HibernateException ...{
return ss.getSession();
}
protected void setRollbackOnly() ...{
ss.setRollBackOnly(true);
}
/** *//**
* 将需要保存的对象暂存在Webwork的Session中(此Session非彼Session)
*/
protected Object get(String name) ...{
return ActionContext.getContext().getSession().get(name);
}
/** *//**
* 从Webwork的Session(此Session非彼Session)中取出暂存的对象
*/
protected void set(String name, Object value) ...{
ActionContext.getContext().getSession().put(name, value);
}
}
二.拦截器
HibernateInterceptor.java
private static final Log LOG = LogFactory.getLog(HibernateInterceptor.class);
public void destroy() ...{}
public void init() ...{}
public String intercept(ActionInvocation invocation) throws Exception ...{
Action action = invocation.getAction();
if ( !(action instanceof AbstractAction) ) return invocation.invoke();
HibernateSession hs = ( (AbstractAction) action ).getHibernateSession();
try ...{
return invocation.invoke();
}
catch (Exception e) ...{
ss.setRollBackOnly(true);
if (e instanceof HibernateException) ...{
LOG.error("HibernateException in execute()", e);
return Action.ERROR;
}
else ...{
LOG.error("Exception in execute()", e);
throw e;
}
}
finally ...{
try ...{
hs.disposeSession();
}
catch (HibernateException e) ...{
LOG.error("HibernateException in dispose()", e);
return Action.ERROR;
}
}
}
}
拦截器监视Action的一举一动,并随时截获异常信息,返回给页面.若无异常,则调用HibernateSession的disposeSession()方法,关闭一个Session.
原文:http://www.nihaoblog.com/933_12896.html
- webwork+hibernate 框架
- webwork 框架
- 如何实现webwork+spring+hibernate框架的结合应用
- webwork+spring+hibernate整合
- webwork+spring+hibernate整合
- webwork+spring+hibernate整合
- Spring+Hibernate+WebWork配置
- 新浪旧事-webwork、webwork+spring和webwork+spring+hibernate
- 新浪旧事-webwork、webwork+spring和webwork+spring+hibernate
- WebWork框架小例子
- 求解spring+hibernate+webwork框架搭建出现空指针异常原因
- WebWork+Spring+Hibernate的烦恼
- 项目小结(hibernate + spring + webwork)
- Webwork+hibernate+spring分页实现
- webwork+spring+hibernate的整合
- webwork+spring+hibernate的整合
- webwork+spring+hibernate的整合
- WebWork教程-验证框架
- 身份证号码相关处理单元及例程
- .NET中窗体间相互访问的几种方式
- COBOL语言初级教程(1)--COBOL简介
- 四书之“大学”学习日记4
- 缓冲区溢出深入理解
- webwork+hibernate 框架
- FCKeditor 上传修改,添加对文件的类型以及大小的限制(ASP.NET C#)
- 如何让ror支持中文
- 不可不知的人生定律!!!
- .NET平台下WEB应用程序的部署(安装数据库和自动配置)
- 如何在console输入密码的时候不在屏幕上显示?
- LCC编译器的源程序分析(15)结构类型成员的声明
- 命运是每一天生活的积累
- 不要忽略c#中的using和as操作符