Working principles of struts2, hibernate, and spring [concise and easy to understand]

Source: Internet
Author: User

Simply put, these three methods encapsulate some common functional methods to normalize these methods, avoiding repeated and fragmented compilation of similar code in jsp development. In other words, struts2 is responsible for processing customer requests and forwarding them to specific action methods for execution. This process uses struts2's Loopers, filters, interceptors, and other functions. The struts2 tag function is equivalent to the jsp tag. The advantage of the struts2 tag is that it uses the operating mechanism of the struts2 framework to make it easier to display data on pages. What about hibernate? It is responsible for the logical implementation (CRUD operation) in the action method, including dealing with the database-how to connect to the database, and how to map the data in the data table to a common java class. So what does spring use? Let's take a look at the following and you will understand it at the end. The following describes their respective functional roles: struts2 focuses on the controller function. When the client sends an address request, Struts (through the configuration file) reflects the request code and performs page call or forwarding. For example, the/login request may be a login request, so Struts2 should reply a login. the jsp page is used to log on to the client. However, the/login is not displayed in the address bar. jsp style. The specific address is hidden. Only the request address style is displayed. Struts2 labels are similar to jstl labels. They are essentially the same. They are used for convenient coding at the view layer and better integration with controllers. It is not necessary to use the Struts2 label to use its controller function. You can select other tags, such as jsp or jstl labels. The tag is just a fur. In fact, spring also has its own set of labels. How can we use so many labels. Familiar with using a label. The hibernate framework is mainly responsible for connecting to the database and converting each data table into a class. each row of data in the table is equivalent to an object of the class. hibernate reads data through objects. When talking about spring, we have to say that control reversal. For control reversal, please search for "java design mode ". However, control reversal cannot be said to be a function of spring, but it is only his design philosophy. That is to say, spring is developed by the design modes of "control reversal" and "aspect programming. Spring packages have many classes and methods, such as methods for operating the underlying database functions like hibernate, and complex business operations. This depends on what interfaces and methods are called. Because of its special design pattern, it can be flexibly integrated with other frameworks, mainly through configuration files. The integration of spring and hibernate actually replaces hibernate because it encapsulates the hibernate method. If the three elements are integrated, spring will guide the global architecture. The integration of spring and strut2 only provides container management for the action class of struts2. That is to say, when an action class is to be instantiated, the spring container is responsible for generating the action object according to spring configuration. When will the action class be instantiated? That is to say, when to generate an action Class Object (Instance )? The answer is when the client requests. The purpose of a customer request is to call the method in action to obtain data or operation data. The methods defined in the action class are generally instance methods. Therefore, you need to obtain the action instance to call these methods. For each action class, there are two instantiation Methods: singleton and prototype. Singleton means that spring only instantiates the action class once, that is, new once, and obtains an object of this class. In the future, every client request will call the method of this object to respond to the customer. In addition, a web Container such as tomcat creates a thread for each request. There is no critical resource problem if there is no class variable in the action class instantiated in singleton mode, on the contrary, the class variables defined in the action class may become critical resources. If an action class is instantiated as a prototype in spring configuration, each new request is instantiated and a thread is created for the request. That is to say, in this new thread, use this new instance to respond to the customer's request. Similarly, if the action instance method has an action to operate on action variables, it may cause a thread problem, that is, access to critical resources. In fact, we generally define the action methods that respond to user requests as a form without parameters, but this does not prevent internal communication between the methods and the outside world. This is another way, that is, you can access the instance variable of action in the action method to obtain the information that the user gives it, or you can call other action instance methods to obtain information, of course, it can also be to call class variables and class methods to obtain information, but this requires processing thread problems, because class variables and class methods are executed by all threads.

Related Article

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.