Struts+spring+hibernate Summary

Source: Internet
Author: User

To put it simply:

For Struts control

Hibernate operations database.

Spring uses a decoupled

To say in detail:

Struts plays a controlling role in the SSH framework, the core of which is the controller, the Actionservlet, and the core of Actionservlet is struts-config.xml. The main control of the processing of logical relationships.

Hibernate is a data persistence layer, a new mapping tool for objects and relationships, provides a mapping from Java classes to data tables, and provides mechanisms for querying and recovering data, which greatly reduces the complexity of data access. The direct operation of the database is converted to the operation of the persisted object.

Spring is a lightweight control inversion (IoC) and facet-oriented (AOP) container framework, interface-oriented programming, and (dependency) relationships between container control programs, rather than traditional implementations, directly manipulated by program code. This is the concept of the so-called "inversion of Control": (dependency) control is transferred from the application code to the external container, and the transfer of control is called inversion. Dependency injection, in which the dependencies between components are determined by the container at run time, the image is that the container dynamically injects some kind of dependency into the component

The main function is to decouple

The role of struts, spring and hibernate in each layer

1) Struts is responsible for the Web tier.

Actionformbean receives the data submitted by the form in the Web page, then processes it through the Action and Forward it to the corresponding page.

The <ACTION-MAPPING> is defined in Struts-config.xml, and Actionservlet is loaded.

2) Spring is responsible for business layer management, i.e. service (or manager).

1. The service provides a statistical calling interface for the action, encapsulating the DAO for the persistence layer.

2. Can write some of their own business methods.

3. A unified approach to JavaBean management

4. Declarative transaction Management

5. Integrated Hiberante

3) Hiberante, responsible for persistence layer, complete the database crud operation

Hibernate is a persistent layer that provides or/mapping.

It has a set of. hbm.xml files and POJO that correspond to the tables in the database. Then you define DAO, which is the class that deals with the database, and they use the PO.

In the Struts+spring+hibernate system,

The calling process for an object is: Jsp-> Action-> Service->dao->hibernate.

The flow of data is Actionformbean accepts the user's data, the Action takes the data out of the Actionfrombean, encapsulates it into VO or PO,

Then call the business layer of the Bean class, complete a variety of business processing and then forward. When the business layer Bean receives the PO object, it invokes the DAO interface method for persistence.

SPRING:AOP Management Transaction Control, IOC manages the coupling of various components, daotemplate as a rapid development template for regular persistence layer!

Struts: Control layer Action, page label and model data, calling the business layer

Hibernate: Responsible for database and object mapping, the DAO layer (data Access object)

Spring integrates hibernate and struts, as long as the applicationcontext.xml is well-equipped and is called directly in the struts action. Hibernate access to the database is implemented in spring, and spring calls are implemented in Stuts's action. This SSH framework is connected together ...

1 Location of SSH in development

Now the open source framework of the Java EE is not clear, the current (already, is) more popular commonly used framework is probably struts,spring,hibernate,jsf,webwork, and struts+spring+hibernate (SSH) This lightweight architecture is known as the "gold combination." Spring and hibernate are a technology that many people think will not be eliminated in the next five years, like struts, which is still widely used in today's development.

2 Why use SSH

In fact, it is not easy to build a Web application that is not trivial, even with Java. There are a lot of things to consider at the beginning of the architecture. From a high point of view, there are a lot of questions before developers: Consider how to create a user interface? Where do you handle business logic? How to persist data. And in these three layers, each layer has to be carefully considered. What technology should be used for each layer? What kind of design can be loosely coupled and can be flexibly changed? How do I replace a layer without affecting the overall architecture? How does the application do various levels of business processing (such as transactional processing)?

Architecting a Web application requires a lot of understanding. Fortunately, many developers have already encountered such problems and have established a framework for dealing with such problems. A good framework has the following: Mitigating the burden of developers dealing with complex problems ("Do not reinvent the wheel"), a good internal expansion, and a strong user community that supports it. A good architecture typically deals with a particular type of problem and can do it well (do one Thing). However, there are several layers in your program that may need to use a specific framework, and the completed UI (user interface) does not mean that you can also bring your business logic and persistent logic into your UI section. For example, you shouldn't write JDBC code as your business logic in a controller, which is not what the controller should provide. A UI controller should be delegated to other lightweight components that are outside the scope of the UI. A good framework should be able to guide how the code is distributed. More importantly, the framework frees developers from coding, allowing them to focus on the logic of the application (which is important to the customer).

They have a very good design concept and the application of the model. For example, struts belongs to the MVC framework, the key is to understand the concept of MVC and the general principle, it is easy to grasp, and hibernate belongs to the ORM system, is a durable solution, also need to have a general understanding of the concept and principle of ORM, If necessary, you can check the use of the entity bean used in EJB1 and EJB2 for the persistence layer. And spring belongs to the application framework, the core of which is the IOC container and the AOP, the two core concepts (also known as the Big model) to understand the future, coupled with a certain internal force, others are not difficult. Spring also integrates a lot of things (though 80% of these things may never be used in a project), such as the encapsulation of JDBC, its own MVC, the simple access to dynamic language, etc., which you choose to study according to your project situation, and then look at his documents when you use them. A project should be able to grasp.

3 Understanding of SSH

In the SSH framework, struts is used to solve the display, request control part of MVC, Spring is primarily responsible for accessing the database DAO class transaction control and its well-known IOC ideas in the business class appropriate use, hibernate mainly as a data access layer component. Due to spring's good support for Hibernate, the DAO class is mostly done by spring, and hibernate is more concerned with the configuration on the O/R mapping file, such as cascading relationships, deferred loading, etc. to make it more efficient.

4 Harvest and problems

Application of three objects of 4.1 actionform,po,vo

The most discussed is the use of Actionform,po,vo three objects, I tend to point out that: in the SSH framework, PO and VO can not distinguish between the business layer and the persistence layer can use Hibernate-generated PO objects, I temporarily divided the object into Actionform and PO two to analyze, action should be actionform and PO of the demarcation Point, PO can not penetrate the business layer, the breakthrough action to reach the page display layer, the same actionform can not break through the action to the backstage business, Persistence layer. (Reason: Po is a persistent object, and when the page is reached, it is removed from the session to become stateless (tentatively understood as a disconnected state) object, and Hibernate persistent object is stateful (including the database primary key), the stateless object to the background in the call Hibernate Save method Error, Be sure to convert the stateless object to a persistent state object before it can be saved) in action the two objects should be transformed, the method of conversion at present I have not found any very good method (welcome Master to Enlighten), the most common is to use Get (), set () method, You can also use the properties provided by struts to copy methods Beanutils class, but this seems to support only a single class of conversions, not for the collection object, we need to expand ourselves.

4.2 Spring Transaction Management

In configuring spring transaction management, it is better to configure the transaction control on the business class, rather than in the DAO class (which is a workaround if you need to ensure that multiple atomic transaction operations fail to rollback simultaneously);

4.3 Action How to Get business class

How to get business class in action: Write a parent action that obtains an instance of the business class in the parent class through spring's webapplicationcontent. The concrete action in struts inherits the parent class and obtains an instance of the business class directly by calling the parent class's GetService ().

4.4 Understanding AOP Ideas

In-depth understanding of the idea of AOP, I feel for the moment is as far as possible interface programming, whether it is a domain object or business class or DAO class are designed interface, in the various laws we try to pass the interface of the object, which for us to reuse these methods, the extension is very beneficial.

4.5 Pagination Processing level

Struts+spring+hibernate Summary

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.