Struts+spring+hibernate Web Application Execution Process

Source: Internet
Author: User
Tags event listener what parameter

There are two ways to integrate STRUTS1 and spring are action and spring bean mapping; one is to give the action to spring initialization

The first way: access the. Do Url->tomcat receive the request-〉 into the struts configuration file find the corresponding action-〉 find the corresponding action component (action that Class)-〉 this class to the request a series of processing-〉 This method returns the value of the-〉 response output by calling the method of the injection instance of a service provided by spring

The second way: Access. Do Url->tomcat receive request-〉 to the struts configuration file to find the corresponding action-〉 cannot find the corresponding action component (action that class)-〉 Go to the Spring config file to find this action mapping, find class-〉 This class to request a series of processing-〉 call Spring provides a service injection instance method, by this method return value-〉 response output

(Struts1 for a long time, maybe a bad leak)

The integration of hibernate and spring is complicated and it is recommended to speak to someone


Rough talk struts--struts from the execution order


1. Configuration in Web. xml
The configuration of struts in Web. XML is usually generated automatically, so there is no need to repeat them here. Only one thing to say is to configure <LOAD-ON-STARTUP&GT for this servlet for Actionservlet, and to make it smaller, so that this Actionservlet system core controller can start earlier.
2. User's request
When a user submits a form, the form's Action property should usually end with a. do. Of course, if you do not write, the system will default (add) to the. Do end. The reason for the. Do end is because, in Web. XML, we are usually configured to intercept requests with the. Do end.
3. Position action
Actionservlet intercepts the user's request and does not handle it by itself, it is entrusted to requestprocessor for processing. Requestprocessor uses the request URI to compare the path property of the action in the Struts-config.xml file to determine which action will handle the request.
4. Filling Actionform
After determining which action to handle the request, the system checks the action's Name property. Then, based on the value of the Name property, in this file, find the corresponding actionform to accept the contents of the form submitted by the user. There is usually a method in Actionform that actionerrors validate (Actionmappingmapping,httpservletrequest request) needs to be rewritten to verify that the user's input is legitimate. Now, however, struts is no longer advocating this method of verification.
5. Validation of forms
With support from Jakarta-oro.jar and Commons-validator.jar, struts has a very powerful checksum capability. This kind of verification involves a lot of content, do not elaborate here. If the error is verified, the error is sent to the page specified by the input property of the action to process.
6.Action processing
If the validation passes, then the action does some business logic processing, in fact the action is the business logic processor. After processing, the result of the processing is placed in the request (or session), and then forwarded to the JSP page specified by global forwarding or the local forwarding specified in the configuration of the action.
7. Display of results
The results of action processing are displayed in the JSP page.

A rough talk on spring--from the execution order, spring


1.Spring Loading (instantiation)
If you do not integrate with struts, the spring load is configured in Web application XML, and there are two options in XML, one Contextloaderlistener and the other contextloaderservlet.
If you are consolidating with struts, the load for spring is configured in Struts-config.xml.
2. Container Post processor
After the container is instantiated, it is possible to perform some operations on the spring container, which is implemented by implementing the interface Beanfactorypostprocessor interface, which has a method voidpostprocessorbeanfactory ( Configurablelistablebeanfactorybeanfactory). There are two very useful implementation classes for this interface, which spring has already done, which are propertyplaceholderconfigurer (property Placeholder configurator) and Propertyoverrideconfigurer (another property placeholder Configurator )。
3. Interacting with the spring container
After the processing of the container is finished, we can interact with the spring container, we can implement the Factorybean,factorybeanaware interface and other methods to obtain the Beanfactory instance, and then use it.
4. Configuring Beans and Dependencies
This includes configuring dependencies, injecting attribute values, injecting field values, injecting method return values, and bean inheritance. These configuration procedures also include the configuration of the forced initialization bean (depends-on), automatic Assembly (Autowire), dependency checking (Dependency-check), which are the Bean's properties (not child elements).
5. Managed--bean after the bean in the container is instantiated
The Bean post processor must implement the Beanpostprocessor interface, which has two methods, one object Postprocessbeforeinitialization (objectbean,string name) Throws Beansexception and Object postprocessafterinitialization (objectbean,string name) throws Beansexception, The previous method is to process the bean before instantiating it, and the next method is to process the bean after it is instantiated.
6. Management of the Bean life cycle
There are two opportunities for managing the Bean lifecycle: one, after injecting the dependency, and two before the bean is about to be destroyed.
There are two ways to manage after injecting a dependency: one, using the Init-method property, and the Initialingbean interface, which has only one void Afterpropertiesset () Throwsexception method.
There are two ways to manage a bean before it is destroyed: one, using the Destroy-method property, and the Disposablebean interface, which has only one void destroy () throws exception method.
It is to be explained here that the method is implemented if the attribute is also provided. Executes the method of the interface, and then executes the specified method of the configured property.
If both the Bean post processor and the Bean's lifecycle are managed, the order of execution is: Object postprocessbeforeinitialization (objectbean,string name) throws Beansexception,void afterpropertiesset () Throwsexception,init-method,object postprocessafterinitialization ( Objectbean,string name) throws Beansexception.
7. Create a Bean instance
There are three ways to create a bean instance:
1. Call the constructor to create a bean instance.
2.BeanFactory calls a static factory method of a class to create a bean.
3.BeanFactory invokes the instance factory method to create the bean.
8. Dependency Injection
The order of injection is to instantiate the bean first (it needs the container to inject it into other beans), instantiate the bean that is being injected into the bean, and then execute the injection. Of course, this order can be changed, and you can change that order by forcing the bean to be instantiated. Forcing the instantiation of the bean has been said earlier.
There are two ways to inject, one is to set the value to inject, the other is to construct the injection.

Rough talk hibernate--from the execution order hibernate


1. Instantiate a Configuration object

Hibernate locates the Hibernate.cfg.xml configuration file under Web-inf and instantiates the configuration object according to the information configured in the Hibernate.cfg.xml configuration file. If the event system is used, the event listener should be registered after instantiating the configuration object. What is worth mentioning here is the problem of configuration. Typically, you configure a mapping file for each persisted class, which includes basic mappings and relational mappings. In Hibernate.cfg.xml, the <mapping resource= "Xxxx.hbm.xml"/> Sub-elements under the <session-factory/> element are used to list all the persisted class mapping files. Causes configuration information for these persisted classes to be loaded when the configuration is instantiated.

2. Instantiating Sessionfactory objects

Usually the object of the configuration calls the Configure () method first, or a return value, which invokes the Buildsessionfactory () method to instantiate the Sessionfactory object.

3. Instantiating a Session object

With the Sessionfactory object, it can Invoke method Opensession () or opensession (interceptor it) to instantiate a session object. If you are using the Opensession () method without an interceptor, use Opensession (Interceptor it) if you are using an interceptor. The interceptor here is an event framework, it implements the class of the Interceptor interface, through the Interceptor interface, you can before the data into the database, the final check of the data, if the data does not meet the requirements, you can modify the data, so as to avoid illegal data into the database. In fact, a global interceptor can also be enabled through configuration. The event system described in 1 is a more powerful event framework that can replace interceptors or be used as a complement to interceptors, see the article "[Reading summary]hibernate's event Framework]."

4. Start a transaction

Before accessing the database, you should set the start point of the transaction, telling the system where to start the operation of the database, so that the system can successfully commit the transaction when it is scoped.

5. Accessing the database

The operation of the database does not depend on the data in the database to increase, delete, change, check and other operations. Hibernate query system is very powerful, it includes HQL query, conditional query and SQL query and so on.

The query for hibernate here is not in detail, just about Hibernate's data filtering. Data filtering is not a general method of data query, but a whole filtering method. Data can also be filtered by filtering the data. Filters are very similar to the "WHERE" constraint clauses defined in a mapping file on classes and collections, except that filters can take parameters, and the application can decide at run time whether a given filter is enabled and what parameter values to use. The "where" property of the mapping file will remain in effect and the parameters cannot be passed in dynamically. The usage of the filter is similar to the view, where the difference is trying to complete the definition in the database, and the filter also needs to determine the parameter values in the application. Hibernate filters work better than normal query statements. That is, in the case of the use of filters, the data query, the filter works first, filter out some data and then let the query statement to check.

6. Commit a transaction

A transaction may or may not be successful when it is committed. If the commit fails, it is rolled back, and all operations since the start of the transaction are canceled when the rollback, which reflects the atomicity of the transaction.

7. Close session

Finally, close the session object that you just instantiated.

Struts+spring+hibernate Web Application Execution Process

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.