Spring MVC InternalResourceViewResolver Configuration Example

InternalResourceViewResolver in Spring MVC is used to mapping the logical view names to actual view files under a particular directory of the web application. This logical view name is returned by the handler method of controller. In Spring MVC InternalResourceViewResolver is used to resolve “internal resource view” (i.e. JSP under the WEB-INF directory) based on a predefined URL pattern.

How to Resolve View
In Spring MVC, InternalResourceViewResolver allow us to add some predefined prefix and suffix to the view name (prefix + view name + suffix), and generate the final view page URL as below.

prefix + logical view name + suffix = /WEB-INF/view/MyPage.jsp

Configuring InternalResourceViewResolver in Spring MVC
To configure InternalResourceViewResolver, you can declare a bean of this type in the web application context either with XML configuration or with Java Configuration as below.

In XML Configuration

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

In Java Configuration

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

InternalResourceViewResolver resolves view names into view objects of type JstlView by default if the JSTL library jstl.jar available into classpath of the application. So you do not need to explicitly add the viewClass property. But you can override this property by adding viewClass property of InternalResourceViewResolver. In below code we add other view class TilesView instead of JstlView class based on tiles.

In XML Configuration

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
 <property name="suffix" value=".jsp"/>
 <property name="prefix" value="/WEB-INF/view/"/>
 <property name="viewClass" value="org.springframework.web.servlet.view.tiles2.TilesView" />
</bean>

In Java Configuration

@Bean
public ViewResolver viewResolver(){
   InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
   viewResolver.setPrefix("/WEB-INF/view/");
   viewResolver.setSuffix(".jsp");
   viewResolver.setViewClass(TilesView.class);
   return viewResolver;
}

What’s internal resource views?
In Spring MVC or any web application, for good practice, it’s always recommended to put the entire views or JSP files under “WEB-INF” folder, to protect it from direct access via manual entered URL. Those views under “WEB-INF” folder are named as internal resource views, as it’s only accessible by the servlet or Spring’s controllers class.

Summary

  1.  Controller return the logical view name to the DispatcherServlet.
  2.  DispatcherServlet has to delegate control to a view template so the information is rendered.
  3.  This view template decides that which view should be rendered based on returned logical view name.
  4.  These view templates are one or more view resolver beans declared in the web application context.
  5.  These beans have to implement the ViewResolver interface for DispatcherServlet to auto-detect them.
  6.  Spring MVC comes with several ViewResolver implementations.

Previous
Next