A Guide to the ViewResolver in Spring MVC – Spring MVC中的ViewResolver指南

最后修改: 2016年 2月 3日

1. Overview


All MVC frameworks provide a way of working with views.


Spring does that via the view resolvers, which enable you to render models in the browser without tying the implementation to a specific view technology.


The ViewResolver maps view names to actual views.


And the Spring framework comes with quite a few view resolvers e.g. InternalResourceViewResolver, BeanNameViewResolver, and a few others.


This is a simple tutorial showing how to set up the most common view resolvers and how to use multiple ViewResolver in the same configuration.


2. The Spring Web Configuration

2.Spring Web的配置

Let’s start with the web configuration; we’ll annotate it with @EnableWebMvc, @Configuration and @ComponentScan:


public class WebConfig implements WebMvcConfigurer {
    // All web configuration will go here

It’s here that we’ll set up our view resolver in the configuration.


3. Add an InternalResourceViewResolver


This ViewResolver allows us to set properties such as prefix or suffix to the view name to generate the final view page URL:


public ViewResolver internalResourceViewResolver() {
    InternalResourceViewResolver bean = new InternalResourceViewResolver();
    return bean;

For such simplicity of the example, we don’t need a controller to process the request.

对于 这种简单性的例子,我们不需要控制器来处理请求。

We only need a simple jsp page, placed in the /WEB-INF/view folder as defined in the configuration:


        <h1>This is the body of the sample view</h1>

4. Add a BeanNameViewResolver


This is an implementation of ViewResovler that interprets a view name as a bean name in the current application context. Each such View can be defined as a bean in XML or Java configurations.


First, we add the BeanNameViewResolver to the previous configuration:


public BeanNameViewResolver beanNameViewResolver(){
    return new BeanNameViewResolver();

Once the ViewResolver is defined we need to define beans of the type View so that it can be executed by DispatcherServlet to render the view:


public View sample() {
    return new JstlView("/WEB-INF/view/sample.jsp");

Here is the corresponding handler method from the controller class:


public String showForm() {
    return "sample";

From the controller method, the view name is returned as “sample” which means the view from this handler method resolves to JstlView class with /WEB-INF/view/sample.jsp URL.

从控制器方法中,视图名称被返回为”sample” ,这意味着从这个处理方法中的视图被解析为JstlView类的/WEB-INF/view/sample.jsp URL。

5. Chaining ViewResolvers and Define an Order Priority


Spring MVC also supports multiple view resolvers.

Spring MVC还支持多个视图解析器

This allow you to override specific views in some circumstances. We can simply chain view resolvers by adding more than one resolver to the configuration.


Once we’ve done that, we’ll need to define an order for these resolvers. The order property is used to define which is the order of invocations in the chain. The higher the order property (largest order number), the later the view resolver is positioned in the chain.


To define the order we can add the follow line of code to the configuration of the our view resolvers:



Be careful on the order priority as the InternalResourceViewResolver should have a higher order – because it’s intended to represent a very explicit mapping. And if other resolvers have a higher order, then the InternalResourceViewResolver might never be invoked.

请注意顺序优先级,因为 InternalResourceViewResolver 应该有更高的顺序–因为它的目的是代表一个非常明确的映射。如果其他解析器有更高的顺序,那么InternalResourceViewResolver可能永远不会被调用。

6. Using Spring Boot

6.使用Spring Boot

When working with Spring Boot, the WebMvcAutoConfiguration automatically configures InternalResourceViewResolver and BeanNameViewResolver beans in our application context

在使用Spring Boot时,WebMvcAutoConfiguration会自动配置InternalResourceViewResolverBeanNameViewResolverbeans在我们的应用上下文

Also, adding the corresponding starter for the templating engine takes away much of the manual configuration we have to do otherwise.


For example, by adding spring-boot-starter-thymeleaf dependency to our pom.xml, Thymeleaf gets enabled, and no extra configuration is necessary:



This starter dependency configures ThymeleafViewResolver bean with the name thymeleafViewResolver in our application context. We can override the auto-configured ThymeleafViewResolver by providing a bean of the same name.


Thymeleaf view resolver works by surrounding the view name with a prefix and suffix. The default values of prefix and suffix are ‘classpath:/templates/’ and ‘.html’, respectively.

Thymeleaf视图解析器通过用前缀和后缀包围视图名称来工作。前缀和后缀的默认值分别为 “classpath:/templates/”和”.html”。

Spring Boot also provides an option to change the default value of prefix and suffix by setting spring.thymeleaf.prefix and spring.thymeleaf.suffix properties respectively.

Spring Boot还提供了一个选项,通过分别设置spring.thymeleaf.prefixspring.thymeleaf.suffix属性来改变前缀和后缀的默认值。

Similarly, we have starter dependencies for groovy-templates, freemarker, and mustache template engines which we can use to get the corresponding view resolvers auto-configured using Spring Boot.

同样地,我们有groovy-templatesfreemarkermustache模板引擎的启动依赖,我们可以使用Spring Boot获得相应的视图解析器自动配置。

DispatcherServlet uses all the view resolvers it finds in the application context and tries each one until it gets a result and hence the ordering of these view resolvers becomes very important if we plan to add our own.


7. Conclusion


In this tutorial we configured a chain of view resolvers using Java configuration. By playing with the order priority we can set the order of their invocation.


The implementation of this simple tutorial can be found in the github project.