使用Struts的PlugIn技術把HibernateSessionFactory
(
package zy
import net
import net
import net
/**
* Configures and provides access to Hibernate sessions
* current thread of execution
* pattern
*/
public class HibernateSessionFactory {
/**
* Location of hibernate
* NOTICE: Location should be on the classpath as Hibernate uses
* #resourceAsStream style lookup for its configuration file
* is place the config file in a Java package
* is the default Java package
* Examples: <br>
* <code>CONFIG_FILE_LOCATION =
* CONFIG_FILE_LOCATION =
*/
private static String CONFIG_FILE_LOCATION =
/** 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
/**
* Returns the ThreadLocal Session instance
* the <code>SessionFactory</code> if needed
*
* @return Session
* @throws HibernateException
*/
public Session currentSession() throws HibernateException {
Session session = (Session) threadLocal
if (session == null) {
if (sessionFactory == null) {
try {
nfigure(CONFIG_FILE_LOCATION);
sessionFactory = cfg
}
catch (Exception e) {
System
e
}
}
session = sessionFactory
threadLocal
}
return session;
}
/**
* Close the single hibernate session instance
*
* @throws HibernateException
*/
public void closeSession() throws HibernateException {
Session session = (Session) threadLocal
threadLocal
if (session != null) {
session
}
}
/**
* Default constructor
*/
public HibernateSessionFactory() {
}
}
(
package zy
/*
* Created on Oct
*
* To change the template for this generated file go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
import javax
import org
import org
import onfig
import javax
import javax
import zy
/**
* @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 =
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
//Put hibernate cleanup code here
System
}
//This method will be called at the time of application startup
public void init(ActionServlet actionServlet
ServletException {
System
System
//Uncomment next two lines if you want to throw UnavailableException from your servlet
// if(true)
// throw new ServletException(
System
bindFactoryToJNDI();
}
private void bindFactoryToJNDI() {
try {
Context ctx = new InitialContext();
ctx
System
}
catch (Exception e) {
e
}
}
public String getName() {
return name;
}
public void setName(String string) {
name = string;
}
}
(
<?xml version=
<!DOCTYPE struts
<struts
<form
<form
</form
<action
<action name=
</action
<plug
<set
</plug
<plug
<set
</plug
<plug
<plug
</struts
這一部分就是你的嵌入代碼
(
package ntroller;
import org
import javax
public class UserActionForm extends ActionForm {
private String password;
private String username;
public String getPassword() {
return password;
}
public void setPassword(String password) {
this
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this
}
public ActionErrors validate(ActionMapping actionMapping
/**@todo: finish this method
return null;
}
public void reset(ActionMapping actionMapping
}
}
(
package ntroller;
import org
import javax
import javax
import javax
import net
import net
From:http://tw.wingwit.com/Article/program/Java/ky/201311/28279.html