簡體   English   中英

如何在Struts2中實現休眠攔截器

[英]How to imlement hibernate interceptor in Struts2

在我的Struts2應用程序中,我想使用Hibernate攔截器審核所有CURD操作,請幫助我如何實現。

您可以通過擴展EmptyInterceptor來創建攔截器,請閱讀文檔:

第十二章攔截器和事件

該文檔提供了有關如何使用攔截器實施審核的示例。

另外,這里還有另一個類似功能的示例Hibernate攔截器示例–審核日志

您可以通過實現Interceptor或擴展EmptyInterceptor來創建XXXDAO類。 如果使用Interceptor接口,則使用下面的覆蓋方法將數據保存在單獨的表中。

 public boolean onLoad(Object o, Serializable srlzbl, Object[] os, String[] strings, Type[] types) throws CallbackException {
    throw new UnsupportedOperationException("Not supported yet.");
}

public boolean onFlushDirty(Object o, Serializable srlzbl, Object[] os, Object[] os1, String[] strings, Type[] types) throws CallbackException {
    throw new UnsupportedOperationException("Not supported yet.");
}

public boolean onSave(Object o, Serializable srlzbl, Object[] os, String[] strings, Type[] types) throws CallbackException {
    throw new UnsupportedOperationException("Not supported yet.");
}

public void onDelete(Object o, Serializable srlzbl, Object[] os, String[] strings, Type[] types) throws CallbackException {
    throw new UnsupportedOperationException("Not supported yet.");
}

public void onCollectionRecreate(Object o, Serializable srlzbl) throws CallbackException {
    throw new UnsupportedOperationException("Not supported yet.");
}

public void onCollectionRemove(Object o, Serializable srlzbl) throws CallbackException {
    throw new UnsupportedOperationException("Not supported yet.");
}

public void onCollectionUpdate(Object o, Serializable srlzbl) throws CallbackException {
    throw new UnsupportedOperationException("Not supported yet.");
}

public void preFlush(Iterator itrtr) throws CallbackException {
    throw new UnsupportedOperationException("Not supported yet.");
}

public void postFlush(Iterator itrtr) throws CallbackException {
    throw new UnsupportedOperationException("Not supported yet.");
}

public Boolean isTransient(Object o) {
    throw new UnsupportedOperationException("Not supported yet.");
}

public int[] findDirty(Object o, Serializable srlzbl, Object[] os, Object[] os1, String[] strings, Type[] types) {
    throw new UnsupportedOperationException("Not supported yet.");
}

public Object instantiate(String string, EntityMode em, Serializable srlzbl) throws CallbackException {
    throw new UnsupportedOperationException("Not supported yet.");
}

public String getEntityName(Object o) throws CallbackException {
    throw new UnsupportedOperationException("Not supported yet.");
}

public Object getEntity(String string, Serializable srlzbl) throws CallbackException {
    throw new UnsupportedOperationException("Not supported yet.");
}

public void afterTransactionBegin(Transaction t) {
    throw new UnsupportedOperationException("Not supported yet.");
}

public void beforeTransactionCompletion(Transaction t) {
    throw new UnsupportedOperationException("Not supported yet.");
}

public void afterTransactionCompletion(Transaction t) {
    throw new UnsupportedOperationException("Not supported yet.");
}


public String onPrepareStatement(String string) {
    throw new UnsupportedOperationException("Not supported yet.");
}

如果擴展EmptyInterceptor不需要覆蓋所有方法,則必需的方法可以在您的dao類中覆蓋。

例如,我必須在我的Audit表中為save()方法存儲fieldNamefieldValuefieldTypeclassName

//Audit save Pojo 
public class AuditSave{
  private String className;
  private String fieldName
  private String fieldValue
  private String fieldType
  //setter's and getter's
}
//AuditDAO class
public class AuditDao exteds EmptyInterceptor{

   public boolean onSave(Object o, Serializable srlzbl, Object[] os, String[] strings,
   Type[] types) throws CallbackException {
   Session session = HibernateUtil.getSessionFactory().openSesson();
   String className = o.getClass().getName();
    try{
       Transaction tx = session.beginTransaction();
       for(int i = 0;i < os.length ;i++){
          AuditSave auditSave = new AuditSave();
          auditSave.setClassName(className);
          auditSave.setFieldName((String)strings[i]);
          auditSave.setFieldValue((String)os[i]);
          auditSave.setFieldType(types[i].toString());

          session.save(auditSave);
          tx.commit();
       }catch(Exception e){
          tx.rollback();
          e.printStackTra;
       }
   if(session.isOpen())
          session.close();
   }
       return true;
    }
 // same as update,delete

對於刪除,請使用onDelete()方法;對於更新,請使用findDirty()方法

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM