📄 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 + -