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 integrate Struts+Hibernate+Spring

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces how Struts+Hibernate+Spring is integrated. It has certain reference value. Interested friends can refer to it. I hope you will gain a lot after reading this article. Let Xiaobian take you to understand it together.

1. Import spring support into the project. The imported Jar packages include:

◆ Spring 2.0 Core Libraries

◆Spring 2.0 ORM/DAO/Hibernate3 Libraries

◆ Spring 2.0 AOP Libraries

◆ Spring 2.0 Web Libraries

2. Configure dataSource and SessionFactory in the Spring configuration file, integrate hibernate configuration with Spring configuration (you can delete hibernate.cfg.xml file);

3. Import the Jar package to be used by the database source, such as the JAR package (commons-pool.jar) used by DBCP;

Modify all DAO hibernate implementations, because Spring provides a HibernateDAOSupport class that simplifies database operations. Use All DAO classes inherit from this class;

5. Inject DAO into Biz by dependency injection, and then inject Biz into Action by dependency injection, and configure it accordingly in Spring configuration file;

Integrating Spring with Struts:

1)Configure Action in the spring configuration file: inject Biz into Action;

2)Modify Struts configuration file: Modify the type attribute of Action to: org.springframework.web.struts.DelegatingActionProxy;

3)Configure the listener and initialization parameters of the web application in the web.xml file:

contextConfigLocation

/WEB-INF/applicationContext.xml /WEB-INF/applicationContext-beans.xml

org.springframework.web.context.ContextLoaderListener

7. In order to solve the Chinese garbled code problem in the application, we can not develop our own filter class. Spring provides us with one, which only needs to be configured:

characterEncodingFilter

org.springframework.web.filter.Charact[color=brown][/color]erEncodingFilter

8. In order to solve the problem of hibernate delay loading, use the filter provided in Spring to solve it, which can make Session

Close after the request interpretation is completed. The configuration method is as follows:

hibernate session manager filter

org.springframework.orm.hibernate3.support.OpenSessionInViewFilter

9. Because OpenSessionInViewFilter will set the flush mode of the retrieved session to FlushMode.NEVER when getSession. InvalidDataAccessApiUsageException: Write operations are not allowed in read-only mode (FlushMode.NEVER/MANUAL): Turn your Session into FlushMode.COMMIT/AUTO or remove 'readOnly' marker from transaction definition. So you need to use the transaction declaration of spring to make the method transaction-controlled:

hibImpl.*.* (..)) "/>

10. Deploy the application and start the server. If an exception is found: java.lang.NoSuchMethodError: org.objectweb.asm.ClassVisitor.visit(IILjava/lang/String;Ljava/lang/String;Ljava/lang/String;)V This is caused by a conflict in the Jar package during integration. "Web application/WEB-INF/lib/asm-2.2.3.jar" should be deleted.

Thank you for reading this article carefully. I hope that the article "Struts+Hibernate+Spring How to Integrate" shared by Xiaobian will help everyone. At the same time, I hope that everyone will support you a lot and pay attention to the industry information channel. More relevant knowledge is waiting for you to learn!

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: 0

*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