?? examplugin.java
字號:
package cn.hxex.exam.struts;
import java.util.List;
import javax.servlet.ServletException;
import org.apache.struts.action.ActionServlet;
import org.apache.struts.action.PlugIn;
import org.apache.struts.config.ModuleConfig;
import org.hibernate.SessionFactory;
import cn.hxex.exam.cache.Cache;
import cn.hxex.exam.cache.CacheFactory;
import cn.hxex.exam.dao.DAOFactory;
import cn.hxex.exam.dao.RoleDAO;
import cn.hxex.exam.model.Role;
import cn.hxex.exam.persistence.HibernateUtil;
/**
* 自定義的Struts插件
* 用于實現Web應用的初始化工作
*
* @author galaxy
*
*/
public class ExamPlugIn implements PlugIn
{
/**
* 初始化<code>SessionFactory</code>.
* @param servlet 插件運行環境中<code>ActionServlet</code>對象的實例
* @param config Struts的模塊配置參數<code>ModuleConfig</code>的實例
*/
public void init(ActionServlet servlet, ModuleConfig config)
throws ServletException
{
Cache cache = CacheFactory.getCache();
// 得到SessionFactory對象的實例,用于初始化Hibernate
SessionFactory sf = HibernateUtil.getSessionFactory();
try
{
// 開始一個新的事物
sf.getCurrentSession().beginTransaction();
// 得到操作用戶角色的DAO
RoleDAO dao = DAOFactory.getDao(RoleDAO.class);
// 得到所有的用戶角色對象
List<Role> roles = dao.findAll();
// 緩存所有的用戶角色信息
for (Role role : roles)
{
cache.put(role.getName(), role);
}
// 關閉當前的事物
sf.getCurrentSession().getTransaction().commit();
}
catch (Throwable ex)
{
ex.printStackTrace();
sf.getCurrentSession().getTransaction().rollback();
}
}
/**
* 銷毀SessionFactory的實例。
*/
public void destroy()
{
HibernateUtil.shutdown();
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -