The new Certification Class of Learn Spring Security is out:


1. Overview

All MVC frameworks provide a way to work 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, XmlViewResolver, ResourceBundleViewResolver 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

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

public class WebConfig extends WebMvcConfigurerAdapter {
    // 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 ResourceBundleViewResolver

As the name of this resolver suggest a ResourceBundleViewResolver uses bean definitions in a ResourceBundle.

First, we add the ResourceBundleViewResolver to the previous configuration:

public ViewResolver resourceBundleViewResolver() {
    ResourceBundleViewResolver bean = new ResourceBundleViewResolver();
    return bean;

The bundle is typically defined in a properties file, located in the classpath. Below is the file:


We can use the simple jsp page defined in the above example for this configuration as well.

5. Add an XmlViewResolver

This implementation of ViewResolver accepts a configuration file written in XML with the same DTD as Spring’s XML bean factories:

public ViewResolver xmlViewResolver() {
    XmlViewResolver bean = new XmlViewResolver();
    bean.setLocation(new ClassPathResource("views.xml"));
    return bean;

Below is the configuration file, views.xml:

<beans xmlns=""
    <bean id="xmlConfig" class="org.springframework.web.servlet.view.JstlView">
        <property name="url" value="/WEB-INF/view/xmlSample.jsp" />

As for the previous examples we can use our simple jsp page defined previously.

6. Chaining ViewResolvers and Define an Order Priority

Spring MVC also supports multiple view resolvers.

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.

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 – this is an Eclipse based project, so it should be easy to import and run as it is.

Go deeper into Spring Security with the course:


  • Bhargav Patel

    Why we need view resolver in spring ? I am asking you because in other java frame work we don’t need any view resolver or similar to it. like Servlet

    • Well Bhargav – different frameworks make different abstractions available – in this case, Spring does come with a lot of flexibility and one of this is the ability to control how views are resolved.
      Hope that clears things up – at least a bit.