熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java開源技術 >> 正文

Struts中用PlugIn擴展Hibernate的例子

2022-06-13   來源: Java開源技術 

  使用Struts的PlugIn技術把HibernateSessionFactory過程如下
  
  ) 創建HibernateSessionFactoryjava代碼如下
  package zyprotdutil;
  
  import netsfhibernateHibernateException;
  import netsfhibernateSession;
  import netsfhibernatecfgConfiguration;
  
  /**
  * Configures and provides access to Hibernate sessions tied to the
  * current thread of execution Follows the Thread Local Session
  * pattern see {@link }
  */
  public class HibernateSessionFactory {
  
  /**
  * Location of hibernatecfgxml file
  * NOTICE: Location should be on the classpath as Hibernate uses
  * #resourceAsStream style lookup for its configuration file That
  * is place the config file in a Java package the default location
  * is the default Java package<br><br>
  * Examples: <br>
  * <code>CONFIG_FILE_LOCATION = /nfxml
  * CONFIG_FILE_LOCATION = /com/foo/bar/nfxml</code>
  */
  private static String CONFIG_FILE_LOCATION = /hibernatecfgxml;
  
  /** Holds a single instance of Session */
  private final ThreadLocal threadLocal = new ThreadLocal();
  
  /** The single instance of hibernate configuration */
  private final Configuration cfg = new Configuration();
  
  /** The single instance of hibernate SessionFactory */
  private netsfhibernateSessionFactory sessionFactory;
  
  /**
  * Returns the ThreadLocal Session instance Lazy initialize
  * the <code>SessionFactory</code> if needed
  *
  * @return Session
  * @throws HibernateException
  */
  public Session currentSession() throws HibernateException {
  Session session = (Session) threadLocalget();
  
  if (session == null) {
  if (sessionFactory == null) {
  try {
  nfigure(CONFIG_FILE_LOCATION);
  sessionFactory = cfgbuildSessionFactory();
  }
  catch (Exception e) {
  Systemerrprintln(%%%% Error Creating SessionFactory %%%%);
  eprintStackTrace();
  }
  }
  session = sessionFactoryopenSession();
  threadLocalset(session);
  }
  
  return session;
  }
  
  /**
  * Close the single hibernate session instance
  *
  * @throws HibernateException
  */
  public void closeSession() throws HibernateException {
  Session session = (Session) threadLocalget();
  threadLocalset(null);
  
  if (session != null) {
  sessionclose();
  }
  }
  
  /**
  * Default constructor
  */
  public HibernateSessionFactory() {
  }
  
  }
  
  ) 創建HibernatePlugInjava代碼如下
  package zyprotdplugin;
  
  /*
  * Created on Oct
  *
  * To change the template for this generated file go to
  * Window>Preferences>Java>Code Generation>Code and Comments
  */
  import javaxservletServletException;
  
  import orgapachestrutsactionActionServlet;
  import orgapachestrutsactionPlugIn;
  import onfigModuleConfig;
  
  import javaxnamingContext;
  import javaxnamingInitialContext;
  
  import zyprotdutilHibernateSessionFactory;
  
  /**
  * @author sunil
  *
  *  This class will initialize hibernate and bind SessionFactory in JNDI at the
  *  time of application and startup and unbind it from JNDI at the time of application
  * shutdown
  */
  public class HibernatePlugin
  implements PlugIn {
  
  private static final String jndi_hibernate = jndi_hibernate_factory;
  private  HibernateSessionFactory hsf;
  private String name;
  
  public HibernatePlugin() {
  hsf=new HibernateSessionFactory();
  }
  
  // This method will be called at the time of application shutdown
  public void destroy() {
  Systemoutprintln(Entering HibernatePlugIndestroy());
  //Put hibernate cleanup code here
  Systemoutprintln(Exiting HibernatePlugIndestroy());
  }
  
  //This method will be called at the time of application startup
  public void init(ActionServlet actionServlet ModuleConfig config) throws
  ServletException {
  Systemoutprintln(Entering HibernatePlugIninit());
  Systemoutprintln(Value of init parameter + getName());
  //Uncomment next two lines if you want to throw UnavailableException from your servlet
  //    if(true)
  //      throw new ServletException(Error configuring HibernatePlugIn);
  Systemoutprintln(Exiting HibernatePlugIninit());
  bindFactoryToJNDI();
  }
  
  private void bindFactoryToJNDI() {
  
  try {
  Context ctx = new InitialContext();
  ctxbind(thisjndi_hibernatehsf);
  Systemoutprintln(bindind the hibernate factory to JNDI successfully);
  }
  catch (Exception e) {
  eprintStackTrace();
  }
  }
  
  public String getName() {
  
  return name;
  }
  
  public void setName(String string) {
  name = string;
  }
  }
  
  ) 配置Strutsconfigxml如下
  <?xml version= encoding=UTF?>
  <!DOCTYPE strutsconfig PUBLIC //Apache Software Foundation//DTD Struts Configuration //EN config__dtd>
  <strutsconfig>
  <formbeans>
  <formbean name=userActionForm type=ntrollerUserActionForm />
  </formbeans>
  <actionmappings>
  <action name=userActionForm path=/act/log/login scope=request type=ntrollerLoginAction />
  </actionmappings>
  <plugin className=orgapachestrutstilesTilesPlugin>
  <setproperty property=definitionsconfig value=/WEBINF/tilesdefsxml />
  </plugin>
  <plugin className=orgapachestrutsvalidatorValidatorPlugIn>
  <setproperty property=pathnames value=/WEBINF/validatorrulesxml/WEBINF/validationxml />
  </plugin>
  <plugin className=zyprotdpluginHibernatePlugin />
  <plugin className=zyprotdpluginHibernateSessionFactoryPlugIn />
  </strutsconfig>
  
  這一部分就是你的嵌入代碼
  
  )創建ActionForm代碼如下
  package ntroller;
  
  import orgapachestrutsaction*;
  import javaxservlethttp*;
  
  public class UserActionForm extends ActionForm {
  private String password;
  private String username;
  public String getPassword() {
  return password;
  }
  public void setPassword(String password) {
  thispassword = password;
  }
  public String getUsername() {
  return username;
  }
  public void setUsername(String username) {
  thisusername = username;
  }
  public ActionErrors validate(ActionMapping actionMapping HttpServletRequest httpServletRequest) {
  /**@todo: finish this method this is just the skeleton*/
  return null;
  }
  public void reset(ActionMapping actionMapping HttpServletRequest httpServletRequest) {
  }
  }
  
  )創建Action
  
  package ntroller;
  
  import orgapachestrutsaction*;
  import javaxservlethttp*;
  import javaxnamingContext;
  import javaxnamingInitialContext;
  import netsfhibernateSessionFactory;
  import netsfhibernate
From:http://tw.wingwit.com/Article/program/Java/ky/201311/28279.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.