by BehindJava

What are the different types of ViewResolver in Spring MVC

Home » springboot » What are the different types of ViewResolver in Spring MVC

In this blog, we are going to learn about the different types of ViewResolver in Spring MVC.

ViewResolver in Spring MVC

ViewResolver is a spring framework interface used to set the view’s extension and location. ViewResolver is an interface that can be implemented by objects that can resolve views by name.

Because the controller will update the model and call the appropriate view, this view resolver will aid in determining where the views are located.

ViewResolver Types

  1. InternalResourceViewResolver
  2. XMLViewResolver
  3. ResourceBundleViewResolver
  4. InterResourceViewResolver: (This ViewResolver allows us to set properties such as prefix or suffix to the view name to generate the final view page URL)
  5. XMLViewResolver: This ViewResolver implementation accepts a configuration file written in XML with the same DTD as Spring’s XML bean factories:

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

    Below is the configuration file, views.xml:

    <beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-4.2.xsd"> 
    <bean id="xmlConfig" class="org.springframework.web.servlet.view.JstlView"> 
    <property name="url" value="/WEB-INF/view/xmlSample.jsp" /> 
    </bean> 
    </beans> 
  6. ResourceBundleViewResolver: As the name implies, this resolver uses the bean definitions in a ResourceBundle.

To begin, we will add the ResourceBundleViewResolver to the previous configuration:

@Bean 
public ViewResolver resourceBundleViewResolver() { 
 ResourceBundleViewResolver bean = new ResourceBundleViewResolver(); 
 bean.setBasename("views"); 
 return bean; 
} 

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

sample.(class)=org.springframework.web.servlet.view.JstlView 
sample.url=/WEB-INF/view/sample.jsp 

Multiple View Resolver

Spring MVC also supports multiple view resolvers.

This allows 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:

bean.setOrder(0);

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.