All IT Courses 50% Off
JAVA Tutorials

What are View Resolvers in Spring?

Spring MVC framework provides away with the help of which you can work with the views. It is done with the help of View Resolver, which does not need any specific view technology like JSP, Velocity, etc. The View Resolver maps the view names to the corresponding view. There are various View Resolvers available in Spring, such as InternalResourceViewResolver, XmlViewResolver, etc.

Given below are the various View Resolvers available in Spring MVC:

  • AbstractCachingViewResolver: This Resolver provides caching when we extend this.
  • XmlViewResolver: It accepts a configuration file written in XML with the same DTD as of Spring Bean factories, and the default configuration file is /WEB-INF/views.xml.
  • ResourceBundleViewResolver: It uses bean definition in a ResourceBundle, which has a specific bundle name, and the default file name is views.properties.
  • UrlBasedViewResolver: It affects the direct resolution of view names to the URLs.
  • InternalResourceViewResolver: It is a subclass of UrlBasedViewResolver that supports InternalResourceView (Servlets and JSPs) and subclasses such as JstlView and TilesView. View Class can be specified using setViewClass().
  • VelocityViewResolver / FreeMarkerViewResolver:  It is a subclass of UrlBasedViewResolver that supports Velocity View or Free Marker View.
  • ContentNegotiatingViewResolver: It resolves a view based on the request file name or Accept header.

Example of View Resolver: 

Step 1: Add the maven dependencies in pom.xml file.

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.snippets.enterprise</groupId>
  <artifactId>springexample</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>springexample Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <dependencies> 
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
        </dependency> 
  </dependencies>
  <build>
    <finalName>springexample</finalName>
  </build> 
    <properties>
        <spring.version>3.2.3.RELEASE</spring.version>
    </properties>
</project>

Step 2: Configure the application in web.xml file and mvc-dispatcher-servlet.xml file placed in the /WEB-INF/ directory.

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>Archetype Created Web Application</display-name> 
    <servlet>
        <servlet-name>mvc-dispatcher</servlet-name>
        <servlet-class>
            org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet> 
    <servlet-mapping>
        <servlet-name>mvc-dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping> 
</web-app>

mvc-dispatcher-servlet.xml

<beans xmlns="https://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="https://www.springframework.org/schema/mvc" 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-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd https://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
    <context:component-scan base-package="com.snippets.enterprise" /> 
   <bean class="com.snippets.enterprise.HelloWorldController" /> 
 <bean 
</beans>

Step 3: Create a view named helloWorld.jsp

<html>
<body>
    <h1>Spring 3.2.3 MVC view resolvers example</h1>     
    <h3> ${msg}</h3>    
</body>
</html>

Step 4: Create a controller file named HelloWorldController.java

package com.snippets.enterprise; 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; 
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController; 
public class HelloWorldController extends AbstractController{ 
    @Override
    protected ModelAndView handleRequestInternal(HttpServletRequest request,
            HttpServletResponse response) throws Exception { 
        ModelAndView model = new ModelAndView("helloWorld");
        model.addObject("msg", "hello world!"); 
        return model;
    }  
}

Step 5: InternalResourceViewResolver maps the jsp and html files in WebContent/WEB-INF/ directory. The view is named as mvc-dispatcher-servlet.xml.

<beans xmlns="https://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="https://www.springframework.org/schema/mvc" 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-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd https://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> 
    <context:component-scan base-package="com.snippets.enterprise" />      
   <bean class="com.snippets.enterprise.HelloWorldController" /> 
 <bean   class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping" /> 
    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix">
            <value>/WEB-INF/</value>
        </property>
        <property name="suffix">
            <value>.jsp</value>
        </property>
    </bean> 
</beans>

Example of  XmlViewResolver:

views.xml

<beans xmlns="https://www.springframework.org/schema/beans"
    xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">  
    <bean id="helloWorld"
        class="org.springframework.web.servlet.view.JstlView">
        <property name="url" value="/WEB-INF/helloWorld.jsp" />
    </bean> 
</beans>

mvc-dispatcher-servlet.xml

<beans xmlns="https://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="https://www.springframework.org/schema/mvc" 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-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd https://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> 
    <context:component-scan base-package="com.snippets.enterprise" />      
   <bean class="com.snippets.enterprise.HelloWorldController" />    
 <bean   class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping" /> 
    <bean class="org.springframework.web.servlet.view.XmlViewResolver">
        <property name="location">
            <value>/WEB-INF/views.xml</value>
        </property>
    </bean> 
</beans>

Example of ResourceBundleViewResolver:

views.properties

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

mvc-dispatcher-servlet.xml

<beans xmlns="https://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="https://www.springframework.org/schema/mvc" 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-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd https://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> 
    <context:component-scan base-package="com.snippets.enterprise" />      
   <bean class="com.snippets.enterprise.HelloWorldController" />    
 <bean   class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping" />   
    <bean class="org.springframework.web.servlet.view.ResourceBundleViewResolver">
        <property name="basename" value="views" />
    </bean> 
</beans>

Example of Configure multiple View Resolvers together:

mvc-dispatcher-servlet.xml

<beans xmlns="https://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="https://www.springframework.org/schema/mvc" 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-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd https://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> 
    <context:component-scan base-package="com.snippets.enterprise" />      
   <bean class="com.snippets.enterprise.HelloWorldController" />    
 <bean   class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping" />    
    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix">
            <value>/WEB-INF/</value>
        </property>
        <property name="suffix">
            <value>.jsp</value>
        </property>
        <property name="order" value="2" />
    </bean> 
    <bean class="org.springframework.web.servlet.view.XmlViewResolver">
        <property name="location">
            <value>/WEB-INF/views.xml</value>
        </property>
        <property name="order" value="1" />
    </bean> 
    <bean class="org.springframework.web.servlet.view.ResourceBundleViewResolver">
        <property name="basename" value="views" />
        <property name="order" value="0" />
    </bean> 
</beans>

Here, InternalResourceViewResolver has the lowest priority.

Facebook Comments
Tags

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Related Articles

Back to top button
Close
Close