A way to consolidate struts and hibernate (hibernate as a struts plugin)

Source: Internet
Author: User
Tags config mysql net sessions

Put the hibernate of the package into the Web application lib, and then define the Hibernate.cfg.xml, which is the configuration information of the plug-in, as follows

<?xml version= "1.0" encoding= "UTF-8"? ><! DOCTYPE hibernate-configuration Public "-//hibernate/hibernate configuration dtd//en"                                            "http:// Hibernate.sourceforge.net/hibernate-configuration-2.0.dtd ">
</session-factory>
Import Org.apache.struts.action.plugin;import Org.apache.struts.action.actionservlet;import Org.apache.struts.config.moduleconfig;import Javax.servlet.servletexception;import Javax.servlet.ServletContext; Import Net.sf.hibernate.sessionfactory;import Net.sf.hibernate.cfg.configuration;public class HibernatePlugIn     implements PlugIn {  public void Destroy () { }  public void init (Actionservlet servlet, Moduleconfig config) throws      servletexception {    try {       ServletContext context = Servlet.getservletcontext ();      Sessionfactory SF = new Configuration (). Configure (). Buildsessionfactory ();      Context.setattribute ("Net.sf.hibernate.SessionFactory", SF);   }    catch ( Exception ex) {      ex.printstacktrace ();   } }}} corresponding, To configure plug-in information in the Struts-config.xml, probably as follows, note the path

<plug-in classname= "homepage. Hibernateplugin "> <set-property property=" ConfigFilePath "value="/web-inf/classes/hibernate.cfg.xml "/> &L T;set-property property= "Storeinservletcontext" value= "true"/> </plug-in>

You can then use Java.naming.context,java.naming.initiacontext to find

Context ct = new InitialContext ();      Sessions= (sessionfactory) ct.lookup ("Hibernate/session_factory"); Session=sessions.opensession ();

Of course, there are two other ways of integration, which I think are more flexible and easier to master


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.