使用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 executionFollows 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 instanceLazy 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 HibernateSessionFactoryhsf;
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(bindindthehibernatefactorytoJNDIsuccessfully);
}
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