Spring View Resolver

Please see this tutorial for an introduction to Spring MVC. In This tutorial we look at the Controllers and View Resolvers.


The DispatcherServlet receives request and then passes on the request to the appropriate Controller. The base Controller interface receives HttpServletRequest and HttpServletResponse. Controllers are like struts Action. The implementation of Controller should be a thread safe class that can handle multiple Http Requests. The Controller implementations are generally JavaBeans. The Controller returns a ModelAndView instance. Spring comes bundled with Some Controllers that provide basic functionalities.

  1. AbstractUrlViewController-This returns a view name based on the request URL. MultiActionController – Allows multiple request types to be handled by the same class. When the class returns a Map the configured RequestToViewNameTranslator will be used to get the view name. When return type is void the handler method is responsible for writing directly to the response. A string return value indicates the name of the view that is returned.
  2. ParameterizableViewController-This Controller always returns a named view. The view can be configured. An example of a use for this controller is JSP where the request for reaches this controller and then is sent to the JSP. The view technology is therefore not exposed to the user.
  3. ServletForwardingController-This Controller forwards to a named servlet. i.e. it forwards to the ‘servlet-name’ configured in web.xml and not to the URL path mapping. This allows using an existing Servlet via the Spring DispatcherServlet.
  4. ServletWrappingController-Wraps an existing Servlet which is managed completely by the spring environment.

View Resolution

The Handler returns an object of type ModelAndView. This object contains both the Model and the View. The model is a Map and the view can be a String view name which is resolved by a View Resolver or an actual view which can be rendered. The following are the important ViewResolver classes

  • ViewResolver-View Resolvers get a org.springframework.web.servlet.View based on a view name.
  • ContentNegotiatingViewResolver-This is an implementation of a view resolver based on the request file name or Accept header. This class delegates view resolution to other view resolvers that are configured. The class uses the MediaType from the request to determine a view. The class first determines the MediaType, then asks each ViewResolver to return a view. The class then compares the all returned views’ content type and the one that is most compatible with the request MediaType is chosen.
  • BeanNameViewResolver-This resolver implemenatation resolves a view name as a bean name registered in the application context. In other words, the view is registered as a bean and this resolver gets the name of that bean
  • UrlBasedViewResolver-This directly resolves a view name to a URL without any explicit mapping. The view names can be the URL themselves or a prefix or suffix can be added to get the URL from the view name. To redirect a URL use the ‘redirect:’ prefix. To forward a URL use the ‘forward:’ prefix.
  • InternalResourceViewResolver-This is a subclass of UrlBasedViewResolver and supports an InternalResourceView. An InternalResourceView is a wrapper for JSP or some other resource that reside in the same web application. It exposes the model objects as request attributes. If JSTL API is present then the resolver resolves the view name to a JSTLView. An InternalResourceViewResolver needs to be last, if a chain of view resolvers is used, since this resolves the view name irrespective of whether the actual resource is present or not.
  • FreeMarkerViewResolver-This is a subclass of UrlBasedViewResolver that supports FreeMarkerView and its subclasses. The View Class generated can be specified by the ‘viewClass’ property.
  • VelocityViewResolver-This is a subclass of UrlBasedViewResolver that supports VelocityView and its subclasses.
  • JasperReportsViewResolver-Supports AbstractJasperReportsView by resolving the view name to the URL of the report file.
  • XsltViewResolver– Supports XsltView by resolving the view name to the URL of the XSLT Stylesheet.

1 thought on “Spring View Resolver”

  1. The Handler returns an object of type ModelAndView..? How?…controller returns an object of type
    ModelAndView I guess…


Leave a Comment