Home Struts Struts-hibernate Developing Struts Hibernate Plugin
Questions:Ask|Latest

 
 

Share on Google+Share on Google+

Developing Struts Hibernate Plugin

Advertisement
In this section we will develop java code for Struts Hibernate Plugin.

Developing Struts Hibernate Plugin

     

In this section we will develop java code for Struts Hibernate Plugin. Our Hibernate Plugin will create Hibernate Session factory and cache it in the servlet context. This strategy enhances the performance of the application.

Source Code Of Hibernate Struts Plugin:

package roseindia.net.plugin;

import java.net.URL;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.ActionServlet;
import org.apache.struts.action.PlugIn;
import org.apache.struts.config.ModuleConfig;
import org.hibernate.HibernateException;


public class HibernatePlugIn implements PlugIn {
 private String _configFilePath = "/hibernate.cfg.xml";

  /**
 * the key under which the <code>SessionFactory </code> instance is stored
 * in the <code>ServletContext</code>.
 */
  public static final String SESSION_FACTORY_KEY = SessionFactory.class.getName();

  private SessionFactory _factory = null;

 public void destroy() {
 try{
 _factory.close();
 }catch(HibernateException e){
  System.out.println("Unable to close Hibernate Session Factory: " + e.getMessage());
 }
 
 }

 public void init(ActionServlet servlet, 
ModuleConfig config
throws ServletException {
 System.out.println("*************************************");
 System.out.println("**** Initilizing HibernatePlugIn **********");
  Configuration configuration = null;
  URL configFileURL = null;
  ServletContext context = null;

 try{
  configFileURL = 
HibernatePlugIn.
class.getResource(_configFilePath);
  context = servlet.getServletContext();
  configuration = (new Configuration()).configure(configFileURL);
  _factory = configuration.buildSessionFactory();
  //Set the factory into session
  context.setAttribute(SESSION_FACTORY_KEY, _factory);

 }catch(HibernateException e){
  System.out.println("Error while initializing hibernate: " + e.getMessage());
 }
 System.out.println("**************** *********************");

 }

  /**
 * Setter for property configFilePath.
 @param configFilePath New value of property configFilePath.
 */
  public void setConfigFilePath(String configFilePath) {
  if ((configFilePath == null|| (configFilePath.trim().length() == 0)) {
  throw new IllegalArgumentException("configFilePath cannot be blank or null.");
  }
  
  System.out.println("Setting ' configFilePath' to '"  + configFilePath + "'...");
  _configFilePath = configFilePath;
  }


/*(SessionFactory) servletContext.getAttribute
(HibernatePlugIn.SESSION_FACTORY_KEY);
*/
  
}


In our plugin class we have define a variable _configFilePath to hold the name of Hibernate Configuration file.

private String _configFilePath = "/hibernate.cfg.xml";

Following code define the key to store the session factory instance in the Servlet context.

public static final String SESSION_FACTORY_KEY = SessionFactory.class.getName();

The init() is called on the startup of the Struts Application. On startup the session factory is initialized and cached in the Servlet context.

configFileURL = HibernatePlugIn.class.getResource(_configFilePath);
context = servlet.getServletContext();
configuration = (new Configuration()).configure(configFileURL);
_factory = configuration.buildSessionFactory();
//Set the factory into session
context.setAttribute(SESSION_FACTORY_KEY, _factory);

Changes to be done in struts-config.xml file

Configuring Hibernate with Struts is very simple work it requires you to have hibernate.cfg.xml in your WEB-INF/classes directory, and to add the following line to the struts-config.xml file.

<plug-in className="roseindia.net.plugin.HibernatePlugIn"></plug-in>

Testing the Plugin

Build your application and deploy on the tomcat server. Start tomcat server and observe the console output. It should display the following line:

 
 log4j:WARN Please initialize the log4j system properly.
*************************************
**** Initilizing HibernatePlugIn **********
*************************************
Aug 7, 2006 10:09:53 AM org.apache.struts.tiles.TilesPlugin initD
  

This means you have successfully configured your Struts Hibernate Plugin with struts application.

     

Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: January 19, 2008

Related Tutorials

Ask Questions?    Discuss: Developing Struts Hibernate Plugin   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
Rakesh
July 31, 2012
classnotfoundexception - help required..

java.lang.ClassNotFoundExceptionplugin.HibernatePlugIn at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1711) at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1556) at org.apache.struts.util.RequestUtils.applicationClass(RequestUtils.java:119) at org.apache.struts.util.RequestUtils.applicationInstance(RequestUtils.java:145) at org.apache.struts.action.ActionServlet.initModulePlugIns(ActionServlet.java:816) at org.apache.struts.action.ActionServlet.init(ActionServlet.java:332) at javax.servlet.GenericServlet.init(GenericServlet.java:160) at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1266) at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1185) at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1080) at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:5026) at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5313) at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1595) at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1585) at java.util.concurrent.FutureTask$Sync.innerRun(Unknown Source) at java.util.concurrent.FutureTask.run(Unknown Source) at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source) at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
DMCA.com