Introduction to Spring Views and View Resolvers

Introduction: What are Spring Views and View Resolvers? Spring MVC (Model View Controller) is an important part of Spring, and Spring views and view resolvers are part of Spring MVC. Before introducing Spring views and view resolvers, let's first understand the six stages that a web request needs to go through in the Spring MVC framework: The request will first be processed by Spr

【Spring Views and View Resolvers】What are Spring views and view resolvers?

Spring MVC (Model View Controller) is an important part of Spring, and Spring views and view resolvers are part of Spring MVC. Before introducing Spring views and view resolvers, let's first understand the six stages that a web request needs to go through in the Spring MVC framework:
1.The request will first be intercepted by Spring MVC's front-end request dispatcher (Dispatcher). The interceptor is a servlet that needs to be configured in web.xml. All access requests that match the configured URL pattern will be intercepted by the interceptor. Spring provides the default dispatcher org.springframework.web.servlet.DispatcherServlet , you can decide whether you need to customize your own dispatcher according to your needs.
2.After receiving the access request, the dispatcher will find the appropriate controller according to the developer's annotation (Annotation) in the Spring configuration file or code.
3.After the dispatcher finds a suitable controller, it forwards the request to the controller for processing.
4.Usually, the controller will call the corresponding service class to process the business logic. After processing the request, the controller needs to return the processed result data model (Model) and the next view name to be displayed.
5.After the controller processing ends and the model and view names are returned, Spring will in turn call the view resolvers registered in the Spring container to find eligible views.
6.After obtaining the Spring view, Spring will display the view according to the configuration information of the view.

Through the introduction of Spring MVC above, we can find that views and view resolvers will appear in the last part of the entire request processing flow. So what exactly are views and view resolvers? In short, the view refers to the V (View) in Spring MVC, and the function of the view resolver is to find the corresponding view according to the specified rules.

【Spring Views and View Resolvers】Introduction to Common Views and View Resolvers

In development, views are usually JSPs, Velocity, and so on. Spring provides a variety of view resolvers by default. For example, we can use the most commonly used resolver InternalResourceViewResolver to find JSP views (the corresponding view class is InternalResourceView ). Usually, a view resolver can only find one or more views of a specific type. When encountering views that are not supported by Spring or we want to customize the view lookup rules, we can customize what we need by extending Spring. View resolver. Currently, all view resolvers need to implement the interface org.springframework.web.servlet.ViewResolver , which contains the method resolveViewName , which finds and returns the Spring view object by the view name. Table 1 lists commonly used Spring view resolvers.
Table 1. List of common view resolvers in Spring
view resolverdescribe
XmlViewResolverinterface ViewResolver , which finds the view implementation from the XML configuration file (the default XML configuration file is /WEB-INF/views.xml)
ResourceBundleViewResolverinterface ViewResolver for finding views from the properties file.
UrlBasedViewResolverinterface ViewResolver is used to return the corresponding view according to the requested URL path. The view needs to be the implementation of the abstract class AbstractUrlBasedView , and it also has some subclasses, such as InternalResourceView and JstlView .
InternalResourceViewResolverSubclass of UrlBasedViewResolver , typically used to find views such as JSP (class InternalResourceView ) and JSTL (class JstlView , a subclass of InternalResourceView ).
VelocityViewResolver / FreeMarkerViewResolverSubclasses of UrlBasedViewResolver are used to support Velocity (class VelocityView ) and FreeMark views (class FreeMarkerView ) respectively.
ContentNegotiatingViewResolverinterface ViewResolver , which is used to find the view according to the suffix name of the request file or the accept field in the request header.
In most projects, InternalResourceViewResolver is the most commonly used. This parser can return files with a specified suffix in a specified directory. It supports view technologies such as JSP and JSTL, but when using this view parser, you need to set the correct priority. Because the view parser will return a view even if it doesn't find the correct file, instead of returning null, the parser with a lower priority than the view parser will not be executed.
In Web development, our front-end display can be JSP, Excel, Velocity, etc. In Spring, different front-end display technologies have their corresponding Java view classes. As mentioned in Table 1, InternalResourceView can represent JSP views, FreeMarkerView stands for FreeMarker View. At present, Spring supports views developed by a variety of technologies, including JSP, JSTL, Excel, Velocity, etc. In most projects, users do not need to customize their own views.
This article is organized from: Developing Spring custom views and view resolvers
Organizer: jiankunking Source:

Copyright statement: The content of this article is contributed by Alibaba Cloud's real-name registered users. The copyright belongs to the original author. The Alibaba Cloud developer community does not own the copyright and does not assume the corresponding legal responsibility. For specific rules, please refer to the " Alibaba Cloud Developer Community User Service Agreement " and " Alibaba Cloud Developer Community Intellectual Property Protection Guidelines ". If you find any content suspected of plagiarism in this community, fill out the infringement complaint form to report it. Once verified, this community will delete the allegedly infringing content immediately.

Related Articles

Explore More Special Offers

  1. Short Message Service(SMS) & Mail Service

    50,000 email package starts as low as USD 1.99, 120 short messages start at only USD 1.00

phone Contact Us