Friday, 8 September 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"
    class="org.springframework.web.servlet.view.InternalResourceViewResolver"
    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:

@Bean
  public ViewResolver viewResolver() {
    InternalResourceViewResolver irv = new InternalResourceViewResolver();
    irv.setPrefix("/WEB-INF/");
    irv.setSuffix(".jsp");

    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;
    }
    setViewClass(viewClass);
  }

  /**
   * This resolver requires InternalResourceView.
   */
  @Override
  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.

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. 
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.

Related Posts