Spring MVC InternalResourceViewResolver Configuration Example

Learn to configure InternalResourceViewResolver in Spring MVC application which helps in resolving view names based of ViewResolver class implemetation and prefix and suffix attributes.

1. What is a view resolver?

In Spring MVC based application, the last step of request processing is to return the logical view name. Here DispatcherServlet has to delegate control to a view template so the information is rendered. This view template decides that which view should be rendered based on returned logical view name.

These view templates are one or more view resolver beans declared in the web application context. These beans have to implement the ViewResolver interface for DispatcherServlet to auto-detect them. Spring MVC comes with several ViewResolver implementations.

In this example, we will look at such a view resolver template i.e. InternalResourceViewResolver.

2. Spring InternalResourceViewResolver

In most of Spring MVC applications, views are mapped to a template’s name and location directly. InternalResourceViewResolver helps in mapping the logical view names to directly view files under a certain pre-configured directory.

2.1. InternalResourceViewResolver Configuration

To register InternalResourceViewResolver, you can declare a bean of this type in the web application context.

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

Similar configuration can be written in Java config in given below manner. This is valid configuration for Spring boot as well.

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

     return viewResolve;
 }

2.2. Mapping resolution example

After above configuration, InternalResourceViewResolver will resolves the view names ‘home’ and ‘admin/home’ etc. in the following way –

Logical View NameActual View File
home/WEB-INF/jsp/home.jsp
admin/home/WEB-INF/jsp/admin/home.jsp
report/main/WEB-INF/jsp/report/main.jsp

2.3. Mapping different view types

By default, InternalResourceViewResolver resolves view names into view objects of type JstlView if the JSTL library (i.e., jstl.jar) is present in the classpath. So, you can omit the viewClass property if your views are JSP templates with JSTL tags.

Otherwise you will need to provide a matching viewClass i.e. org.springframework.web.servlet.view.tiles2.TilesView if your view is based on tiles.

3. How to return view name from Controller

A controller class should return view name in String form or instance of ModelAndView class. For example, in given Controller class, it is return view name as "home".

public class HomepageController extends AbstractController{
	
	@Override
	protected ModelAndView handleRequestInternal(HttpServletRequest request,
						HttpServletResponse response) throws Exception 
	{

		ModelAndView model = new ModelAndView("home");
		
		return model;
	}
}

Drop me your queries in comments – related to Spring MVC InternalResourceViewResolver configuration and example.

Happy Learning !!

Was this post helpful?

Join 7000+ Fellow Programmers

Subscribe to get new post notifications, industry updates, best practices, and much more. Directly into your inbox, for free.

1 thought on “Spring MVC InternalResourceViewResolver Configuration Example”

  1. i want to have my jsp files in more than one folder under WEB-INF for example views, pages, jsps so that my prefix values have such format

    My question is, How can i configure this in my xml

Comments are closed.

HowToDoInJava

A blog about Java and its related technologies, the best practices, algorithms, interview questions, scripting languages, and Python.