Mar 5, 2013

Spring MVC View Resolvers

Spring Interview Questions and Answers Q1 - Q14 are FAQs

Q1 - Q4 Overview & DIP Q5 - Q8 DI & IoC Q9 - Q10 Bean Scopes Q11 Packages Q12 Principle OCP Q14 AOP and interceptors
Q15 - Q16 Hibernate & Transaction Manager Q17 - Q20 Hibernate & JNDI Q21 - Q22 read properties Q23 - Q24 JMS & JNDI Q25 JDBC Q26 Spring MVC Q27 - Spring MVC Resolvers

Q. What is a Spring MVC ViewResolver?
A. In Spring MVC,  ViewResolver is a strategy and factory object used to map logical view names to actual view resources. The interface for ViewResolver is shown below. The single method resolveViewName(..) maps a logical view name together with the request locale to a View instance.The interface defines the following single method.

public interface ViewResolver {
  View resolveViewName(String name, Locale loc);

The ViewResolvers can be defined in your Spring context file as shown below.

<beans ... >
  <bean class="org.springframework.web.servlet.view.
    <property name="prefix" value="/WEB-INF/views/" />
    <property name="suffix" value=".jsp" />


Q. Can you write custom resolvers?
A. Yes, you can. Spring provides the following resolvers.

  • AbstractCachingViewResolver: An abstract view resolver which takes care of caching views. Often views need preparation before they can be used, extending this view resolver provides caching of views. 
  • XmlViewResolver: An implementation of ViewResolver that accepts a configuration file written in XML with the same DTD as Spring's XML bean factories. The default configuration file is /WEB-INF/views.xml. 
  • ResourceBundleViewResolver:    An implementation of ViewResolver that uses bean definitions in a ResourceBundle, specified by the bundle basename. The bundle is typically defined in a properties file, located in the classpath. The default file name is 
  • UrlBasedViewResolver: A simple implementation of the ViewResolver interface that effects the direct resolution of symbolic view names to URLs, without an explicit mapping definition. This is appropriate if your symbolic names match the names of your view resources in a straightforward manner, without the need for arbitrary mappings. 
  • InternalResourceViewResolver: A convenience subclass of UrlBasedViewResolver that supports InternalResourceView (i.e. Servlets and JSPs), and subclasses such as JstlView and TilesView. The view class for all views generated by this resolver can be specified via setViewClass(..). See the Javadocs for the UrlBasedViewResolver class for details. 
  • VelocityViewResolver / FreeMarkerViewResolver: A convenience subclass of UrlBasedViewResolver that supports VelocityView (i.e. Velocity templates) or FreeMarkerView respectively and custom subclasses of them.

If you have specific requirements, you can define your own.Here is an example where a custom view resolver is defined to use both .jsp and .html files.

Step 1: The Spring context file.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=""
 xmlns:xsi="" xmlns:p=""
 xmlns:aop="" xmlns:jee=""
 xmlns:tx="" xmlns:util=""
 xmlns:batch="" xmlns:task=""

 <mvc:annotation-driven />

  <property name="location" value="classpath:/" />
  <property name="placeholderPrefix" value="$ap{" />
  <property name="placeholderSuffix" value="}" />

 <context:annotation-config />

 <context:component-scan base-package="com.myapp.portal.controller" />

 <bean class="com.myapp.portal.resolver.AppPortalViewResolver">
  <property name="htmlResolver" ref="htmlViewResolver" />
  <property name="jspResolver" ref="jspViewResolver" />

 <bean id="htmlViewResolver"
  <property name="prefix" value="/"></property>
  <property name="suffix" value=".html"></property>

 <bean id="jspViewResolver"
  <property name="viewClass"
  <property name="prefix" value="/WEB-INF"></property>
  <property name="suffix" value=".jsp"></property>


Step 2: The custom resolver Java class.

package com.myapp.portal.resolver;

import java.util.Locale;

import org.springframework.web.servlet.View;
import org.springframework.web.servlet.ViewResolver;

public class AppPortalViewResolver implements ViewResolver {
 private ViewResolver htmlResolver;
 private ViewResolver jspResolver;

 public void setHtmlResolver(ViewResolver htmlResolver) {
  this.htmlResolver = htmlResolver;

 public void setJspResolver(ViewResolver jspResolver) {
  this.jspResolver = jspResolver;

 public View resolveViewName(String viewName, Locale locale) throws Exception {
  if (viewName.startsWith("/pages")) {
            return jspResolver.resolveViewName(viewName, locale);
        } else {
            return htmlResolver.resolveViewName(viewName, locale);





Post a Comment

Subscribe to Post Comments [Atom]

<< Home