JavaWeb study notes (XXIII) to teach you how to integrate SSH project

Before the Spring framework has learned, Struts2 framework, Hibernate framework, which correspond to the three framework Java Web development in three-tier structure, in order to better enhance the development efficiency can integrate these three core Spring framework. Although Struts2 framework because of its performance and other reasons out of date, but we can through the integration of thinking to understand them.

A, Spring and Struts2 integration

Spring and Struts2 integration is relatively simple, you need to configure before Action struts.xml configuration to Spring configuration file, but need to pay attention a problem here. Struts2 Action frame is class-based, i.e., multi-mode embodiment. This needs attention.

  1. Related to the introduction of the jar package
    here after the spring and struts2 related to the introduction of the jar package, we also need to introduce an integrated spring and struts2 jar package.

Here Insert Picture Description

  1. web.xml configuration file
    First, in struts2 framework, interceptor filter needs to be configured in web.xml.
    Here Insert Picture DescriptionSecond, SSH is a Web project, we hope that in the process of starting the server configuration file to load the spring. Here configure a listener for loading spring configuration file.
    Here Insert Picture Description
    Here Insert Picture Description
  2. Modify the configuration file
    if the release is not in accordance with the previous integration configuration file, there will be a problem. Action will generate an instance in spring container, in addition, also in the time of the visit, in struts2 also generate an instance. Here, we hope to use to manage ioc container spring instance of an object. So struts.xml files make the following changes. We modify the contents of the latter class attribute, no write path category, but to write a value corresponding to the id of the spring container.

Here Insert Picture DescriptionIn addition, it should also be noted that, in the spring configuration file, the struts need to be arranged in a plurality Example action mode.
Here Insert Picture Description

  1. After completing the above steps, and achieve the integration framework struts2 spring frame.

Two integration, Spring and Hibernate Framework

The two main frame fusion of two points (1) on the issue SessionFactory to generate code for the example of many, spring container can not be directly generated. (2) the hibernate core profile database configuration into the spring configuration file.

  1. Import the relevant jar package
    in addition to the original frame to import a jar, but also need to import the following package:

Here Insert Picture Description

  1. Change the data source

The data source hibernate core profile spring placed in the configuration file.
Here Insert Picture Description

  1. Spring SessionFactory handed over management of
    the Spring before SessionFactory handed over the management, take a look at how the SessionFactory is created.
    Here Insert Picture DescriptionAs can be seen from the figure, it is difficult to use Spring configuration file to instantiate. To solve this problem, Spring to create the SessionFactory is encapsulated. org.springframework.orm.hibernate5.LocalSessionFactoryBeanThis class can be instantiated to complete the SessionFactory.
    Here Insert Picture Description
  2. Completion of the above steps, realize the integration of these two frameworks.
    Because I'm using IDEA software development, when using hibernate framework, a common recommendation entity class configuration files are placed in the same package entity class, but when the project is built using maven in IDEA in, java folder xml file is automatically masked. Therefore, when you start the project, it will throw an exception entity class can not find the configuration file. The entity class configuration files are placed in the resources folder, project start properly.
Published 66 original articles · won praise 26 · views 10000 +

Guess you like

Origin blog.csdn.net/Time__Lc/article/details/96473387