| |
使用struts的plugin技术把hibernatesessionfactory,过程如下: (1) 创建hibernatesessionfactory.java,代码如下: package zy.pro.td.util; import net.sf.hibernate.hibernateexception; import net.sf.hibernate.session; import net.sf.hibernate.cfg.configuration; /** * configures and provides access to hibernate sessions, tied to the * current thread of execution. follows the thread local session * pattern, see {@link http://hibernate.org/42.html}. */ public class hibernatesessionfactory { /** * location of hibernate.cfg.xml 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 = "/hibernate.conf.xml". * config_file_location = "/com/foo/bar/myhiberstuff.conf.xml".</code> */ private static string config_file_location = "/hibernate.cfg.xml"; /** 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 net.sf.hibernate.sessionfactory 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) threadlocal.get(); if (session == null) { if (sessionfactory == null) { try { cfg.configure(config_file_location); sessionfactory = cfg.buildsessionfactory(); } catch (exception e) { system.err.println("%%%% error creating sessionfactory %%%%"); e.printstacktrace(); } } session = sessionfactory.opensession(); threadlocal.set(session); } return session; } /** * close the single hibernate session instance. * * @throws hibernateexception */ public void closesession() throws hibernateexception { session session = (session) threadlocal.get(); threadlocal.set(null); if (session != null) { session.close(); } } /** * default constructor. */ public hibernatesessionfactory() { } } (2) 创建hibernateplugin.java,代码如下: package zy.pro.td.plugin; /* * created on oct 4, 2004 * * to change the template for this generated file go to * window>preferences>java>code generation>code and comments */ import javax.servlet.servletexception; import org.apache.struts.action.actionservlet; import org.apache.struts.action.plugin; import org.apache.struts.config.moduleconfig; import javax.naming.context; import javax.naming.initialcontext; import zy.pro.td.util.hibernatesessionfactory; /** * @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() { system.out.println("entering hibernateplugin.destroy()"); //put hibernate cleanup code here system.out.println("exiting hibernateplugin.destroy()"); } //this method will be called at the time of application startup public void init(actionservlet actionservlet, moduleconfig config) throws servletexception { system.out.println("entering hibernateplugin.init()"); system.out.println("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"); system.out.println("exiting hibernateplugin.init()"); bindfactorytojndi(); } private void bindfactorytojndi() { try { context ctx = new initialcontext(); ctx.bind(this.jndi_hibernate,hsf); system.out.println("bindind the hibernate factory to jndi successfully"); } catch (exception e) { e.printstacktrace(); } } public string getname() { return name; } public void setname(string string) { name = string; } } (3) 配置struts-config.xml,如下: <?xml version="1.0" encoding="utf-8"?> <!doctype struts-config public "-//apache software foundation//dtd struts configuration 1.1//en" "http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd"> <struts-config> <form-beans> <form-bean name="useractionform" type="zy.pro.td.controller.useractionform" /> </form-beans> <action-mappings> <action name="useractionform" path="/act/log/login" scope="request" type="zy.pro.td.controller.loginaction" /> </action-mappings> <plug-in classname="org.apache.struts.tiles.tilesplugin"> <set-property property="definitions-config" value="/web-inf/tiles-defs.xml" /> </plug-in> <plug-in classname="org.apache.struts.validator.validatorplugin"> <set-property property="pathnames" value="/web-inf/validator-rules.xml,/web-inf/validation.xml" /> </plug-in> <plug-in classname="zy.pro.td.plugin.hibernateplugin" /> <plug-in classname="zy.pro.td.plugin.hibernatesessionfactoryplugin" /> </struts-config> 这一部分就是你的嵌入代码 (4)创建actionform,代码如下: package zy.pro.td.controller; import org.apache.struts.action.*; import javax.servlet.http.*; public class useractionform extends actionform { private string password; private string username; public string getpassword() { return password; } public void setpassword(string password) { this.password = password; } public string getusername() { return username; } public void setusername(string username) { this.username = 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) { } } (5)创建action package zy.pro.td.controller; import org.apache.struts.action.*; import javax.servlet.http.*; import javax.naming.context; import javax.naming.initialcontext; import net.sf.hibernate.sessionfactory; import net.sf.hibernate
|
|