[Struts]HibernatePlugIn for Struts(转贴)

这个Plugin的作用是在Struts应用程序启动时进行hibernate的初始化操作,原文HibernatePlugIn for Struts,步骤很简单:

1、在struts-config.xml里增加:

None.gif < plug-in  className ="org.haree.struts.HibernatePlugIn" >  
None.gif  
<!--  'path-to-config-file' is relative to the root of the class 
None.gif       path.  It MUST start with a '/'. The default is 
None.gif       "/hibernate.cfg.xml 
-->  
None.gif  
< set-property  property ="configFilePath"  value ="path-to-config-file"   />  
None.gif  
< set-property  property ="storeInServletContext"  value ="true-or-false"   />  
None.gif
</ plug-in >  
None.gif

2、HibernatePlugIn.java的内容

None.gif package org.haree.struts; 
None.gif
None.gifimport java.net.URL; 
None.gif
None.gifimport javax.servlet.ServletContext; 
None.gifimport javax.servlet.ServletException; 
None.gif
None.gifimport net.sf.hibernate.SessionFactory; 
None.gifimport net.sf.hibernate.cfg.Configuration; 
None.gif
None.gifimport org.apache.commons.logging.Log; 
None.gifimport org.apache.commons.logging.LogFactory; 
None.gifimport org.apache.struts.action.ActionServlet; 
None.gifimport org.apache.struts.action.PlugIn; 
None.gifimport org.apache.struts.config.ModuleConfig; 
None.gif
None.gif
None.gif
/*
None.gif * Implements the <code>PlugIn</code> interface to configure the Hibernate 
None.gif * data persistence library.  A configured 
None.gif * <code>net.sf.hibernate.SessionFactory</code> is stored in the 
None.gif * <code>ServletContext</code> of the web application unless the property 
None.gif * <code>storedInServletContext</code> is set to <code>false</code>. 
None.gif * 
None.gif * <p> 
None.gif * &lt;plugin class=&quot;net.sf.hibernate.plugins.struts.HibernatePlugIn&quot;&gt; 
None.gif *   &lt;set-property name=&quot;configFilePath&quot" 
None.gif *                value=&quot;path-to-config-file&quot;/&gt; 
None.gif *   &lt;set-property name=&quot;storedInServletContext&quot" 
None.gif *                value=&quot;true-or-false&quot;/&gt; 
None.gif * &lt;/plugin&gt; 
None.gif * 
None.gif * @author  <a href="mailto:bhandy@users.sf.net">Bradley M. Handy</a> 
None.gif * @version 1.0 
None.gif 
*/  
None.gifpublic class HibernatePlugIn implements PlugIn { 
None.gif    
None.gif    
/*
None.gif     * the key under which the <code>SessionFactory</code> instance is stored 
None.gif     * in the <code>ServletContext</code>. 
None.gif     
*/  
None.gif    public static final String SESSION_FACTORY_KEY 
None.gif            
=  SessionFactory.class.getName(); 
None.gif
None.gif    private static Log _log 
=  LogFactory.getLog(HibernatePlugIn.class); 
None.gif    
None.gif    
/*
None.gif     * indicates whether the <code>SessionFactory</code> instance will be stored 
None.gif     * in the <code>ServletContext</code>, or not. 
None.gif     
*/  
None.gif    private 
boolean  _storedInServletContext  =   true
None.gif    
None.gif    
/*
None.gif     * the path to the xml configuration file.  the path should start with a 
None.gif     * '/' character and be relative to the root of the class path. 
None.gif     * (DEFAULT:  "/hibernate.cfg.xml") 
None.gif     
*/  
None.gif    private String _configFilePath 
=   " /hibernate.cfg.xml "
None.gif
None.gif    private ActionServlet _servlet 
=   null
None.gif    private ModuleConfig _config 
=   null
None.gif    private SessionFactory _factory 
=   null
None.gif
None.gif    
/*
None.gif     * Destroys the <code>SessionFactory</code> instance. 
None.gif     
*/  
None.gif    public 
void  destroy() { 
None.gif        _servlet 
=   null
None.gif        _config 
=   null
None.gif        
None.gif        
try  { 
None.gif            _log.debug(
" Destroying SessionFactorydot.gif " ); 
None.gif            
None.gif            _factory.close(); 
None.gif            
None.gif            _log.debug(
" SessionFactory destroyeddot.gif " ); 
None.gif        } 
catch  (Exception e) { 
None.gif            _log.error(
" Unable to destroy SessionFactorydot.gif(exception ignored) "
None.gif                    e); 
None.gif        } 
None.gif    } 
None.gif    
None.gif    
/*
None.gif     * Initializes the <code>SessionFactory</code>. 
None.gif     * @param servlet the <code>ActionServlet</code> instance under which the 
None.gif     *        plugin will run. 
None.gif     * @param config the <code>ModuleConfig</code> for the module under which 
None.gif     *        the plugin will run. 
None.gif     
*/  
None.gif    public 
void  init(ActionServlet servlet, ModuleConfig config) 
None.gif    throws ServletException { 
None.gif        _servlet 
=  servlet; 
None.gif        _config 
=  config; 
None.gif        
None.gif        initHibernate(); 
None.gif    } 
None.gif    
None.gif    
/*
None.gif     * Initializes Hibernate with the config file found at 
None.gif     * <code>configFilePath</code>. 
None.gif     
*/  
None.gif    private 
void  initHibernate() throws ServletException { 
None.gif        Configuration configuration 
=   null
None.gif        URL configFileURL 
=   null
None.gif        ServletContext context 
=   null
None.gif        
None.gif        
try  { 
None.gif            configFileURL 
=  HibernatePlugIn.class.getResource(_configFilePath); 
None.gif
None.gif            context 
=  _servlet.getServletContext(); 
None.gif
None.gif            
if  (_log.isDebugEnabled()) { 
None.gif                _log.debug(
" Initializing Hibernate from  "  
None.gif                        
+  _configFilePath  +   " dot.gif " ); 
None.gif            } 
None.gif            
None.gif            configuration 
=  ( new  Configuration()).configure(configFileURL); 
None.gif            _factory 
=  configuration.buildSessionFactory(); 
None.gif            
None.gif            
if  (_storedInServletContext) { 
None.gif                _log.debug(
" Storing SessionFactory in ServletContextdot.gif " ); 
None.gif                
None.gif                context.setAttribute(SESSION_FACTORY_KEY, _factory); 
None.gif            } 
None.gif            
None.gif        } 
catch  (Throwable t) { 
None.gif            _log.error(
" Exception while initializing Hibernate. " ); 
None.gif            _log.error(
" Rethrowing exceptiondot.gif " , t); 
None.gif            
None.gif            
throw  ( new  ServletException(t)); 
None.gif        } 
None.gif    } 
None.gif    
None.gif    
/*
None.gif     * Setter for property configFilePath. 
None.gif     * @param configFilePath New value of property configFilePath. 
None.gif     
*/  
None.gif    public 
void  setConfigFilePath(String configFilePath) { 
None.gif        
if  ((configFilePath  ==   null ||  (configFilePath.trim().length()  ==   0 )) { 
None.gif            
throw   new  IllegalArgumentException( 
None.gif                    
" configFilePath cannot be blank or null. " ); 
None.gif        } 
None.gif        
None.gif        
if  (_log.isDebugEnabled()) { 
None.gif            _log.debug(
" Setting 'configFilePath' to ' "  
None.gif                    
+  configFilePath  +   " 'dot.gif " ); 
None.gif        } 
None.gif        
None.gif        _configFilePath 
=  configFilePath; 
None.gif    } 
None.gif    
None.gif    
/*
None.gif     * Setter for property storedInServletContext. 
None.gif     * @param storedInServletContext New value of property storedInServletContext. 
None.gif     
*/  
None.gif    public 
void  setStoredInServletContext(String storedInServletContext) { 
None.gif        
if  ((storedInServletContext  ==   null
None.gif                
||  (storedInServletContext.trim().length()  ==   0 )) { 
None.gif            storedInServletContext 
=   " false "
None.gif        } 
None.gif        
None.gif        
if  (_log.isDebugEnabled()) { 
None.gif            _log.debug(
" Setting 'storedInServletContext' to ' "  
None.gif                    
+  storedInServletContext  +   " 'dot.gif " ); 
None.gif        } 
None.gif        
None.gif        _storedInServletContext 
None.gif                
=   new  Boolean(storedInServletContext).booleanValue(); 
None.gif    } 
None.gif    
None.gif
None.gif

转载于:https://www.cnblogs.com/bjzhanghao/archive/2004/07/28/28070.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值