`
sqe_james
  • 浏览: 262289 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

使用Struts1.x PlugIn 接口扩展应用

阅读更多

Struts 1.x 提供一个新的 PlugIn 接口,可以动态增减 ActionServlet 的功能。如果需要在应用启动或关闭时完成某些操作,可以创建自己的 PlugIn 类。

 

1. 实现自己的 PlugIn

实现 PlugIn 接口必须实现如下两个方法。

  • void destroy();
  • void init(ActionServlet servlet,ModuleConfig config) throws ServletException;

应用启动时调用 init 方法,而应用关闭时则调用 destroy 方法。

e.g. SessionFactoryPlugIn 的实现类:

 

public class SessionFactoryPlugIn implements PlugIn  {   
  //Hibernate的配置文件   
  private String configFile;   
   //应用关闭时,销毁资源   
  public void destroy()   {   
       System.out.println("系统销毁SeesionFactory");   
     }   
  
   //应用启动时,完成SessionFactory的初始化   
  public void init(ActionServlet actionServlet,ModuleConfig config)
     throws ServletException   {   
        System.out.println("系统以 " + getConfigFile() + " 为配置文件初始化SessionFactory");   
    }   
       
     //获取PlugIn配置文件的方法   
   public String getConfigFile()   {   
           return configFile;   
     }   
  
     //负责加载PlugIn配置属性的方法   
   public void SetConfigFile(String configFile)   {   
           this.configFile = configFile;   
        }   
}
 

在上面的 PlugIn 中,并没有真正初始化 SessionFactory ,仅在控制台打印出字符串来标识创建动作。另外,还提供了configFile 属性的 setter getter 方法,这两个方法负责访问 PlugIn 元素的 configFile 属性。

 

2. struts-config.xml 文件的配置

   为了让 ActionServlet 知道要 挂上 这个 PlugIn ,您须如下设定:

<plug-in className="cn.janwer.xxxPlugIn">
    <set-property property="xxx" value="abcde"/>
</plugin>

Tiles、Validator 等都是利用这种方式来扩充 Struts 的功能。

 

3. 实践应用

在下面示例中,系统使用 Hibernate 作为持久层,在启动时创建 SessionFactory 实例,并将该 SessionFactory 存在 Application ,在应用关闭时销毁 SessionFactory

 

如下在启动时加载权限配置信息:

  public class SqePlugIn implements PlugIn {   
        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();   
     }   
 }
 
下面是配置信息:
 <!-- 加载权限校验配置信息类 -->
<plug-in className="cn.janwer.struts.SqePlugIn"/>

如果您在某个 Action 中必须使用到 挂上 的资源,您可以由 ActionField 成员 servlet (ActionServlet 实例) 来取得 ServletContext ,并得到所须的资源,例如:
public ActionForward execute(ActionMapping mapping,
                              Actionform form,
                              HttpServletRequest request,
                              HttpServletResponse response)
                               throws Exception {
    .......
    ServletContext context = servlet.getServletContext();
    SomeService service =
         (SomeService) context.getAttribute("service");
     ......
}
 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics