Spring MVC 4 - InternalResourceViewResolver example

Posted on September 5, 2017

Technologies used: Java SE 1.8 | Spring 4.3.10.RELEASE | Maven 3.3.9 | Eclipse Neon.3 | Apache Tomcat  7.0.47

InternalResourceViewResolver class is used to map the string (logical view name), returned from a controller, to actual URL without any explicit mapping definition.  

The InternalResourceViewResolver prepends a prefix and appends a suffix to a string to convert the logical name to a URL. Finally, this resolver sends the URL to the RequestDispatcher to render the actual view.

Following example demonstrates the usages of the InternalResourceViewResolver in Spring MVC application.

Consider the following controller class.

package com.boraji.tutorial.spring.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

public class UserController {

   public String index() {
      // Your code goes here!
      return "index";

   public String guestHandler(Model model) {
      // Your code goes here!
      return "guest";

   public String adminHandler(Model model) {
      // Your code goes here!
      return "admin/index";

Here logical view names, returned by handler methods, are index, guest and admin/index.

To map the above logical view names to actual URL, configure the InternalResourceViewResolver in your web @Configuration class as follows.

package com.boraji.tutorial.spring.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

@ComponentScan(basePackages = { "com.boraji.tutorial.spring.controller" })
public class WebConfig extends WebMvcConfigurerAdapter {

   public InternalResourceViewResolver resolver() {
      InternalResourceViewResolver resolver = new InternalResourceViewResolver();
      return resolver;


How does the InternalResourceViewResolver work?

From the above examples, the InternalResourceViewResolver will prepend and append the prefix /WEB-INF/views/ and suffix .jsp to the logical view names (i.e. index, guest and admin/index) as follows.

(prefix) + (logical view name returned from handler method) + (suffix) ⇒ (actual URL of view)

/WEB-INF/views/    +    index    +    .jsp     ⇒     /WEB-INF/views/index.jsp
/WEB-INF/views/    +    guest    +    .jsp     ⇒     /WEB-INF/views/guest.jsp
/WEB-INF/views/    +    admin/index    +    .jsp      ⇒     /WEB-INF/views/admin/index.jsp

After adding prefix and suffix to logical view name , the InternalResourceViewResolver sends the actual URL to the RequestDispatcher for rendering the view.