Webwork+Hibrnate开发
【IT168技术文档】
4.同样HibernateSessionImp.java也是一个接口,供AbstractAction实现,从而使Webwork自动调用其中的setHibernateSession()方法,将HibernateSession实例注入到AbstractAction中
5.让我们来看看AbstractAction.javapublic interface HibernateSessionImp ...{
public void setHibernateSession(HibernateSession hs);
}
二.拦截器public abstract class AbstractAction extends ActionSupport implements HibernateSessionImp...{
![]()
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);
![]()
}
![]()
}
拦截器监视Action的一举一动,并随时截获异常信息,返回给页面.若无异常,则调用HibernateSession的disposeSession()方法,关闭一个Session.public class HibernateInterceptor implements Interceptor ...{
![]()
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;
}
}
}
}
好了,所有具体的代码告一段落,在以上文件加入我们到的开发所定义的包之后,还需要在配置文件中加以定义.
0
相关文章
