Home Java javaTutorial What is the principle and process of ssh framework

What is the principle and process of ssh framework

Jul 25, 2019 pm 02:41 PM
java ssh framework principle process

What is the principle and process of ssh framework

##SSH (Struts2 Spring Hibernate) framework project, the architecture is mainly divided into three levels:

( 1) Struts2: Responsible for the web layer

(2) Spring: Business layer management

(3) Hibernate: Responsible for data persistence

Struts2 working principle:

1. Initialize a request to the servlet container.

2. The request is filtered by the filter configured in web.xml, and the FilterDispatcher (the core of the struts2 controller) asks the ActionMapper whether it needs to call an Action to handle the request. If ActionMapper decides that an Action needs to be called, FilterDispatcher hands over request processing to ActionProxy.

3.ActionProxy finds the Action class that needs to be called through the configuration file Struts.xml configuration file.

4.ActionProxy creates an ActionInvocation instance, and ActionInvocation calls Action through proxy mode. But before calling, ActionInvocation will load all Action-related Interceptors (interceptors) according to the configuration.

5. After the Action is executed, ActionInvocation is responsible for finding the corresponding return result based on the configuration in struts.xml.

That is, the developer sends an http request. This request is filtered by the filter of web.xml to see if an action needs to be called. If so, the method to implement the request is found in Struts.xml, and then returned The result of the operation.

Related recommendations: "

java Development Tutorial"

Hibernate working principle:

1. Read and parse the configuration file

2. Read and parse mapping information, create SessionFactory

3. Open Sesssion

4. Create transaction Transation

5. Persistence operation

6. Submit transaction

7. Close Session

8. Close SesstionFactory

That is, xxx.hbm.xml is loaded during Hibernate initialization managed by spring Then read the parsing mapping information and create a SessionFactory. Then open the session, operate the thing through the session and submit it. Finally close the session and close the SessionFactory.

Three states of beans in Hibernate

Hibernate objects are divided into three states: transient state (new or session-independent when instantiated), persistent state (session-associated) and free state (Once associated with session). Among them, the persistent objects are PO, and the transient and managed objects can be used as VO. (PO should not be used directly as the v layer) Therefore, attention should be paid to the transformation of the three states during use.

For example:


In a series of data operations, the save or saveorupdate operation can convert transient or free objects into persistent objects, while delete or session close , flush and other operations will convert the persistent state object associated with the session into a free state.

The state and life cycle of Hibernate objects

The object initialized using the new operator is a transient (Transient) (there is no behavior associated with the database table, as long as the application no longer refers to it) The status of these objects will be lost and recycled by the garbage collection mechanism);

Transient objects will be converted into persistent objects through save() and saveOrUpdate, and will be managed uniformly by the session. The operations of persistent objects are the same as Database synchronization;

The persistent object will be converted into a free state after evict(), close(), clear(), etc. At this time, although it is persisted, it is not in the session cache. Through lock(), update( ), saveOrUpdate will be converted into persistent state. Objects obtained through get(), load(), find(), and iterate() are directly persistent.

How Spring works:

In the SSH framework, spring plays the role of managing the container. We all know that Hibernate is used as the persistence layer, because it provides a good encapsulation of JDBC, and programmers do not need to write a large number of SQL statements when interacting with the database. Struts is used as the application layer, which is responsible for calling the business logic serivce layer.

So the process of the SSH framework is roughly: Jsp page----Struts------Service (business logic processing class)---Hibernate (left to right)

struts Responsible for controlling the Service (business logic processing class), thereby controlling the life cycle of the Service. In this way, the dependence between layers is very strong and is coupled.

At this time, using the spring framework plays the role of controlling the Action object (in Strus) and the Service class. The relationship between the two is loose. Spring's Ioc mechanism (inversion of control and dependency injection ) is used here.

Inversion of control: The container controls the (dependency) relationship between programs, instead of the traditional implementation, which is directly controlled by the program code.

Dependency injection: The dependencies between components are determined by the container during runtime, and the container dynamically injects certain dependencies into the components.

The second benefit of using Spring (AOP application):

Transaction processing:

In the past JDBCTemplate, the transaction was submitted successfully and exception handling was completed through Try/Catch. The Spring container integrates TransactionTemplate, which encapsulates all transaction processing functions, including transaction rollback when exceptions occur, data submission when operations succeed, and other complex business functions. This is all managed by the Spring container, which greatly reduces the amount of code for programmers and provides good management and control over transactions. Hibernate also has transaction management. Transaction management in hibernate is accomplished by creating and maintaining Session through SessionFactory. Spring has also integrated the SessionFactory configuration, and there is no need to set the SessionaFactory through hibernate.cfg.xml.

In this way, you can make good use of Sping's powerful transaction management functions.

Avoids the need to obtain a Session instance to start the transaction/submit/rollback the transaction and the cumbersome Try/Catch operation every time the data is operated.

These are good applications of the AOP (aspect-oriented programming) mechanism in Spring. On the one hand, it makes the development of business logic clearer and professional division of labor easier.

On the other hand, the application of Spirng AOP isolation reduces the coupling of the program, allowing us to combine various aspects in different applications, which greatly improves code reuse.

The above is the detailed content of What is the principle and process of ssh framework. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Perfect Number in Java Perfect Number in Java Aug 30, 2024 pm 04:28 PM

Guide to Perfect Number in Java. Here we discuss the Definition, How to check Perfect number in Java?, examples with code implementation.

Weka in Java Weka in Java Aug 30, 2024 pm 04:28 PM

Guide to Weka in Java. Here we discuss the Introduction, how to use weka java, the type of platform, and advantages with examples.

Smith Number in Java Smith Number in Java Aug 30, 2024 pm 04:28 PM

Guide to Smith Number in Java. Here we discuss the Definition, How to check smith number in Java? example with code implementation.

Java Spring Interview Questions Java Spring Interview Questions Aug 30, 2024 pm 04:29 PM

In this article, we have kept the most asked Java Spring Interview Questions with their detailed answers. So that you can crack the interview.

Break or return from Java 8 stream forEach? Break or return from Java 8 stream forEach? Feb 07, 2025 pm 12:09 PM

Java 8 introduces the Stream API, providing a powerful and expressive way to process data collections. However, a common question when using Stream is: How to break or return from a forEach operation? Traditional loops allow for early interruption or return, but Stream's forEach method does not directly support this method. This article will explain the reasons and explore alternative methods for implementing premature termination in Stream processing systems. Further reading: Java Stream API improvements Understand Stream forEach The forEach method is a terminal operation that performs one operation on each element in the Stream. Its design intention is

TimeStamp to Date in Java TimeStamp to Date in Java Aug 30, 2024 pm 04:28 PM

Guide to TimeStamp to Date in Java. Here we also discuss the introduction and how to convert timestamp to date in java along with examples.

Java Program to Find the Volume of Capsule Java Program to Find the Volume of Capsule Feb 07, 2025 am 11:37 AM

Capsules are three-dimensional geometric figures, composed of a cylinder and a hemisphere at both ends. The volume of the capsule can be calculated by adding the volume of the cylinder and the volume of the hemisphere at both ends. This tutorial will discuss how to calculate the volume of a given capsule in Java using different methods. Capsule volume formula The formula for capsule volume is as follows: Capsule volume = Cylindrical volume Volume Two hemisphere volume in, r: The radius of the hemisphere. h: The height of the cylinder (excluding the hemisphere). Example 1 enter Radius = 5 units Height = 10 units Output Volume = 1570.8 cubic units explain Calculate volume using formula: Volume = π × r2 × h (4

Create the Future: Java Programming for Absolute Beginners Create the Future: Java Programming for Absolute Beginners Oct 13, 2024 pm 01:32 PM

Java is a popular programming language that can be learned by both beginners and experienced developers. This tutorial starts with basic concepts and progresses through advanced topics. After installing the Java Development Kit, you can practice programming by creating a simple "Hello, World!" program. After you understand the code, use the command prompt to compile and run the program, and "Hello, World!" will be output on the console. Learning Java starts your programming journey, and as your mastery deepens, you can create more complex applications.

See all articles