Get the App
SLTechnology News&Howtos  ›  Development  › 

How to solve the integration problem of Struts Hibernate

Shulou Source: shulou.com Published: 2022-06-02 08:00:13 10月05日 Update

This article mainly introduces "how to solve the integration problem of Struts Hibernate". In the daily operation, I believe that many people have doubts about how to solve the integration problem of Struts Hibernate. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubt of "how to solve the integration problem of Struts Hibernate"! Next, please follow the editor to study!

Put all the packages used by Hibernate into the lib of the web application, and then define hibernate.cfg.xml, that is, the configuration information of the plug-in, as follows

"http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd"> falseproperty > trueproperty > net.sf.hibernate.dialect.MySQLDialectproperty > org.gjt.mm.mysql.Driverproperty > jdbc:mysql:///testproperty > rootproperty > property > 20property > hibernate/session_factoryproperty > session-factory > hibernate-configuration >

The message is very clear, that is, the configuration database and other information, we use jndi to find SessionFactory, the following is the plug-in

HibernatePlugIn.java 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 ();}

Accordingly, configure the plug-in information in struts-config.xml, roughly as follows. Note the path.

Plug-in >

You can use java.naming.Context,java.naming.InitiaContext to find it later.

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.

At this point, the study on "how to solve the integration problem of Struts Hibernate" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Tags: Problems integration information learning plug-ins configuration methods more programs help utility clarity and then that is applications data databases articles methods Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Information Shulou Technology Xiaomi Apple vpn