Abstract: SSH = Struts + Spring + Hibernate

SSH is not a framework, but the integration of multiple frameworks (Struts + Spring + Hibernate). It is a popular open source integration framework for Web application, which is used to build flexible and easily extended multi-layer Web application.

The system integrating SSH framework is divided into four layers: presentation layer, business logic layer, data persistence layer and domain module layer to help developers build Web applications with clear structure, good reusability and easy maintenance in a short period of time. Among them, Struts is used as the overall infrastructure of the system to be responsible for the separation of MVC. In the model part of Struts framework, business jump is controlled. Hibernate framework is used to provide support for the persistence layer.

The SSH framework’s system is based on MVC. Struts is a very good MVC framework, the main technology is Servlet and JSP. Struts MVC design pattern can make our logic become very clear, let us write the program hierarchy. The development based on Struts can simplify the development difficulty and improve the development efficiency.

Spring provides a consistent way to manage business objects and encourages the injection of good habits for programming interfaces rather than classes to maximize the decoupling of our products.

Hibernate is designed to persist data and provides fully object-oriented database operations. Hibernate wraps JDBC in a very lightweight way, making it easy to work with relational databases.

In the Struts+Spring+Hibernate system, the call flow between objects is as follows:

Struts – > Spring – > Hibernate

JSP – > Action – > Service – > DAO – > Hibernate

Such as:

1.Spring’s configuration file bean.xml

2. The struts.xml configuration file for Struts

3. One of the Hibernate configuration files:

SpringThe role and benefits of the framework:

The Spring framework provides a container that manages the components of an application, and IoC and AOP mechanisms to decouple components, improve the flexibility of the application structure, and enhance the maintainability and extensibility of the system.

In the integrated development of SSH, Spring is used to manage Service, DAO and other components, and IoC mechanism is used to realize Action and Service, and the low coupling call between Service and DAO is made. The use of AOP mechanism to achieve transaction management, as well as the common function of the cut into.

The function is integration, the benefit is decoupling.

Concurrent processing of operations in Hibernate (optimistic locks and pessimistic locks)

The Hibernate framework can use the locking mechanism to solve the problem of operation concurrency.

A. pessimistic locking

When the data query comes out, add a lock to the data, lock. This other users to delete, change the operation is not allowed. When the holding transaction ends, the lock is automatically released.

Hibernate uses database locking mechanism to achieve pessimistic lock control.

Disadvantages: open concurrent user operations simultaneously, one by one processing. When a user takes a long time to process, the efficiency is low.

B. optimistic locking

Concurrent update submissions are allowed, but the fastest will succeed and the slower ones will fail.

Appends a field value to the record, using the field value as the version. When the first submitter commits, it will automatically raise the value of the version field, so that other users will find that the version is lower than the current version recorded in the database. Therefore, an exception will be thrown to indicate that the version failed.

Features: Allow the user to process simultaneously, but only one succeeds, the other fails, and prompt in an exception way.

SSH workflow

A. Start the server and load the project and web.xml.

Instantate Lisener,Filter and other components, create Spring container and Struts2 control.

B. The client sends the request, and all the requests enter the Struts2 controller. The controller handles each request separately according to the type of request.

The action request, *. The action goes to struts.xml to find the configuration.

For other requests, *.jsp calls the request resource directly, generating the response information.

C. struts2 controller calls an Action object according to the configuration.

Integration One: Give the Action to the Spring container

(The Action object is provided by the struts2-spring-plugin.jar plug-in

StrutsSpringObjectFactory responsible to the Spring container for)

Integration method two: Put the Action outside the Spring container

(The Action object is provided by the struts2-spring-plugin.jar plug-in

StrutsSpringObjectFactory responsible for creating, and then to looking for matching with the Action attribute of the Bean in the Spring container object, give the Action object injection. (Name matching rules are adopted by default)

D. Struts2 controller performs component processing such as DefaultStack interceptor, Action object, Result, etc.

E. When executing the Execute business method of Action, if using Service or DAO, use Spring’s IOC mechanism to call.

F. Execution Result to generate response information and perform subsequent interceptor processing

G. Output the response information.

This article is shared from the Huawei cloud community “SSH composite framework pattern knowledge sharing”, the original author: Jack20.

Click on the attention, the first time to understand Huawei cloud fresh technology ~