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
Dinesh Rajput

Dinesh Rajput is the chief editor of a website Dineshonjava, a technical blog dedicated to the Spring and Java technologies. It has a series of articles related to Java technologies. Dinesh has been a Spring enthusiast since 2008 and is a Pivotal Certified Spring Professional, an author of a book Spring 5 Design Pattern, and a blogger. He has more than 10 years of experience with different aspects of Spring and Java design and development. His core expertise lies in the latest version of Spring Framework, Spring Boot, Spring Security, creating REST APIs, Microservice Architecture, Reactive Pattern, Spring AOP, Design Patterns, Struts, Hibernate, Web Services, Spring Batch, Cassandra, MongoDB, and Web Application Design and Architecture. He is currently working as a technology manager at a leading product and web development company. He worked as a developer and tech lead at the Bennett, Coleman & Co. Ltd and was the first developer in his previous company, Paytm. Dinesh is passionate about the latest Java technologies and loves to write technical blogs related to it. He is a very active member of the Java and Spring community on different forums. When it comes to the Spring Framework and Java, Dinesh tops the list!

Share
Published by
Dinesh Rajput

Recent Posts

Strategy Design Patterns using Lambda

Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…

2 years ago

Decorator Pattern using Lambda

Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…

2 years ago

Delegating pattern using lambda

Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…

2 years ago

Spring Vs Django- Know The Difference Between The Two

Technology has emerged a lot in the last decade, and now we have artificial intelligence;…

2 years ago

TOP 20 MongoDB INTERVIEW QUESTIONS 2022

Managing a database is becoming increasingly complex now due to the vast amount of data…

2 years ago

Scheduler @Scheduled Annotation Spring Boot

Overview In this article, we will explore Spring Scheduler how we could use it by…

3 years ago