Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to solve the integration problem of Struts Hibernate

2025-04-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 242

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report