Introduction and development of Spring Custom view and view parser

Source: Internet
Author: User

Introduction to the Spring View and view parsers

What is Spring view and view parser

Spring MVC (Model view Controller) is an important part of spring, and the spring view and the view parser are part of Spring MVC. Before introducing the spring view and the view parser, let's take a look at the six stages that a WEB request requires in the Spring MVC framework:

The request is first intercepted by the front-end request distributor (Dispatcher) of Spring MVC. The Interceptor is a Servlet that needs to be configured in Web.xml, and all access requests that match the configured URL style will be intercepted by the interceptor. Spring provides the default distributor Org.springframework.web.servlet.DispatcherServlet, and you can decide whether or not you want to customize your own distributor as needed.

After receiving the access request, the Distributor finds the appropriate controller based on the developer's Annotations (Annotation) in the Spring configuration file or code.

The Distributor, after locating the appropriate controller, forwards the request to the Controller for processing.

Typically, the controller invokes the corresponding service class to handle the business logic, and after the request is processed, the controller returns the processed result data model and the next view name that needs to be displayed.

After the controller processing finishes and returns the model and view name, spring calls the view parser registered in the spring container in turn to find a qualified view.

After you get the spring view, Spring displays the view based on the configuration information for that view.

Figure 1.Spring MVC Processing process

As described in Spring MVC above, we can see that the view and view parser will appear in the last part of the entire request processing process. So what exactly is the view and view parser? In short, the view refers to V in Spring MVC, and the view parser's function is to find the appropriate view based on the specified rules.

Introduction to common views and view parsers

In development, the view is usually JSP, Velocity, and so on. Spring defaults to providing a variety of view parsers, for example, we can use the most common parser internalresourceviewresolver to find the JSP view (the corresponding view class is Internalresourceview). Typically, a view parser can only find one or more specific types of views, and when you encounter a view that is not supported by spring or when we want to customize the view lookup rules, we are able to customize the view parser that we want by extending spring. Currently, the view parser needs to implement Interface Org.springframework.web.servlet.ViewResolver, which contains method Resolveviewname, which finds and returns the Spring view object through the view name. Table 1 lists the commonly used Spring view parsers.

Table 1.Spring common View parser list

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.