Sunday, August 27, 2017

What does the InternalResourceViewResolver do in Spring MVC?

The InternalResourceViewResolver is an implementation of ViewResolver in Spring MVC framework which resolves logical view name e.g. "hello" to internal physical resources e.g. Servlet and JSP files e.g. jsp files placed under WEB-INF folder. It is a subclass of UrlBasedViewResolver, which uses "prefix" and "suffix" to convert a logical view name returned from Spring controller to map to actual, physical views. For example, if a user tries to access /home URL and HomeController returns "home" then DispatcherServlet will consult InternalResourceViewResolver and it will use prefix and suffix to find the actual physical view which is integral to a web application. For example, if prefix is "/WEB-INF/views/" and suffix is ".jsp" then "home" will be resolved to "/WEB-INF/home.jsp" by InternalResourceViewResolver.

It's also a  best practice to put JSP files inside WEB-INF directory, to hide them from direct access (e.g. via a manually entered URL). Only controllers will be able to access them then.

How to configure InternalResorveViewResolver in Spring MVC

You can configure this ViewResolver either using Java Configuration or XML configuration as shown below:

Configuring ViewResolver using XML in Spring
Here is some XML snippet to configure a view resolve in Spring, you can use this if you are working on a Spring project which is using XML based confirmation:

<bean id="viewResolver"
    prefix="/WEB-INF/" suffix=".jsp" /> 

Configuring ViewResolver using Java Configuration
From Spring 3.0 you can also configure view resolver using Java i.e. without XML. You can use following code to configure internal resource view resolver in your spring project:

  public ViewResolver viewResolver() {
    InternalResourceViewResolver irv = new InternalResourceViewResolver();

    return irv;


You can see that both the XML and Java offers a simple approach to configure internal resource view resolver in Spring.

Important points about InteralResourceViewResolver in Spring MVC

Here are some of the important things to know about this useful class from Spring MVC framework. This will help you to understand the flow of your project better:

1) When chaining ViewResolvers, an InternalResourceViewResolver always needs to be last, as it will attempt to resolve any view name, no matter whether the underlying resource actually exists.

2) The InternalResourceViewResolver is also the default view resolver of DispatcherServlet class, which acts as the front controller in Spring MVC framework.

3) By default, InternalResourceViewResolver returns InternalResourceView (i.e. Servlets and JSP) but it can be configured to return JstlView by using the viewClass attribute as shown below:

   * Sets the default setViewClass view class to requiredViewClass: by default
   * InternalResourceView, or JstlView if the JSTL API is present.
  public InternalResourceViewResolver() {
    Class viewClass = requiredViewClass();
    if (viewClass.equals(InternalResourceView.class) && jstlPresent) {
      viewClass = JstlView.class;

   * This resolver requires InternalResourceView.
  protected Class requiredViewClass() {
    return InternalResourceView.class;

The advantage of using JstlView is that JSTL tags will get the Locale and any message source configured in Spring. This is particularly important when you are using JSTL tags for formatting for displaying messages.

JSTL's formatting tags need a Locale to properly format locale-specific values e.g. currency and dates. It's message tags can use a Spring message source and a Locale to properly choose the message to render in HTML depending upon Locale. See Spring in Action by Craig Walls for more details on JstlView class.

4. The InteralResourceViewResolver is one of the several built-in view resolvers provided by Spring framework, some of the most useful ones are listed below:
  • BeanNameViewResolver - resolves views as beans in the Spring application context whose ID is the same as the view name. For example, if you have a bean with id = "home" and a controller return a logical view name as "home" then this bean will be resolved by BeanNameViewResolver.
  • FreeMarkerViewResolver - resolver views as FreeMarker templates
  • JasperReportsViewResolver - resolves views as JasperReports definitions
  • XsltViewResolver - resolves views to be rendered as the result of an XSLT transformation. 
You can see Bryan Hassen's Introduction to Spring MVC 4 to learn more about different types of view resolvers in Spring and how to use them as per your need.

What does the InternalResourceViewResolver do in Spring MVC?

5. The most important benefit of using ViewResolver in Spring MVC is that it decouples request-handling logic in the controller from the view-rendering of a view. In short, the controller doesn't know anything about which view technology is used to render the view.

It just returns a logical name which could resolve to a JSP, FreeMarker template, Apache tiles or any other view technology. It also means you can change the view layer without changing controller as long as logical view name is same.

That's all about what does InternalResourceViewResolver do in Spring MVC or what is the role of InternalResourceViewResolver. It's one of the useful class from Spring MVC and as a Java Spring developer, you should be familiar with it. The concept of view resolution in Spring MVC is also very important from both Spring interview as well as Spring certification point of view. If you are preparing for Spring certification, I suggest you go through some questions shared by David Mayer's Spring Mock exams to test your knowledge of view resolution concept in Spring MVC.

Further Reading
Spring Framework 5: Beginner to Guru
Spring Master Class - Beginner to Expert
Introduction to Spring MVC 4 By Bryan Hansen

Other Spring related articles you may like to explore this blog
  • 23 Spring MVC Interview questions for 2 to 3 years experienced (list)
  • How Spring MVC works internally? (answer)
  • What is the use of DispatcherServlet in Spring MVC? (answer)
  • How to enable Spring security in Java application? (answer)
  • Does Spring certification help in Job and Career? (article)
  • How to prepare for Spring Certification? (guide)
  • Difference between @Autowired and @Injection annotations in Spring? (answer)
  • 5 Spring and Hibernate online courses for Java developers (list)

Thanks for reading this article. If you like this article then please share with your friends and colleagues. If you have any questions or feedback then please drop a comment and I'll try to answer it as soon as possible.

P.S. - If you want to learn how to develop RESTful Web Services using Spring Framework, check out Eugen Paraschiv's REST with Spring course. He has recently launched the certification version of the course, which is full of exercises and examples to further cement the real world concepts you will learn from the course.

No comments :

Post a Comment