Spring MVC (a) The principles of spring MVC

Source: Internet
Author: User

1. The purpose of Spring MVC

Build Web applications that are flexible and loosely coupled to the spring framework.

2. How do I handle request in Spring MVC?

Whenever a user clicks a link or submits a form in a Web browser, the request begins to work.

From the beginning of the browser to getting the response back, request will leave some information at each station that has gone through, and will get some information.

dipatcherservlet--Front Controller servlet, responsible for sending request to the controller of spring MVC;

Handler mapping--Processor mapping, there will be multiple controllers in the application, Dipatcherservlet need to know which controller to send, so queries one or more handlermapping to determine the next station. Handler mapping will make decisions based on the URL information carried by the request;

controller--Controller, request to the controller, you will get the information submitted by the user, and then process the information. In practice, the well-designed controller itself deals with very little work, and instead delegates the business logic to one or more service objects for processing. The last thing the controller does is pack the model data ~ ~

The model--model is the information that the controller generates when it completes the logic processing, which needs to be returned to the user and displayed on the browser.

View resolver--, when the original model is created, it needs to be formatted with the view as necessary. The view resolver is based on the resulting logical view name matching a view, telling Dipatcherservlet. Dipatcherservlet know which view renders the results, the request's task is basically complete, and the last station to complete the implementation of the view.

view--view, request here to deliver the data model, complete the task. View uses the model data to render the output, which is passed to the client through the response object.

3. Traditional way to configure Dipatcherservlet in a Web application

 <!--configuration springmvc Dispatcherservlet (center Controller)-<servlet> <servlet -name>springmvc</servlet-name> <servlet-class  >        Org.springframework.web.servlet.dispatcherservlet</servlet-class  > <init-param> <!--sources labeled folder you need to create a new spring folder--<param-name>contextconfiglocatio N</param-name> <param-value>classpath*:spring/spring-mvc.xml</param-value> </init-p aram> <load-on-startup>1</load-on-startup> <async-supported>true  </async-supported> </servlet> <servlet-mapping> <servlet-name>springmvc& Lt;/servlet-name> <url-pattern>/</url-pattern> </servlet-mapping>  

4, the new method is based on the Servlet 3 specification and Spring 3.1, you can use Java to configure Dipatcherservlet in the servlet container, instead of using Web. Xml. (It's okay to use it at the same time.)

Understanding the Abstractannotationconfigdipatcherservletinitializer classes that are extended (inherited) automatically configures the application context for Dipatcherservlet and spring. The application context for spring is in the context of the servlet of the application.

There are three methods in this class:

String[] Getservletmappings (), getrootconficlasses (), Getservletconfigclasses (). (The latter two return classes, that is, Xxx.class)

5, two application contexts (based on Java configuration)

When Dipatcherservlet is started, it creates a spring application context and loads the bean that is declared in the configuration file or configuration class.

In spring Web applications, there is usually another application context, which is created by the Contextloaderlistener (Servlet listener).

We want Dipatcherservlet to load the bean that contains the Web component, such as the controller, attempt to parse it, and process and map, while contextloaderlistener loads the other beans in the app. These beans are typically the middle-tier and data-tier components that drive the application backend.

In fact, Abstractannotationconfigdipatcherservletinitializer will create both Dipatcherservlet and Contextloaderlistener.

6. Enable Spring MVC

One, XML configuration, using <mvc:annotation-driven> enable note-driven spring MVC;

Second, Java-based configuration, that is, create a Java class to configure.

Spring MVC (a) The principles of spring MVC

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.