HowToDoInJava

  • Java 8
  • Regex
  • Concurrency
  • Best Practices
  • Spring Boot
  • JUnit5
  • Interview Questions
  • Dark Mode

Spring MVC InternalResourceViewResolver Configuration Example

By Lokesh Gupta | Filed Under: Spring MVC

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

TwitterFacebookLinkedinRedditPocket

About Lokesh Gupta

A family guy with fun loving nature. Love computers, programming and solving everyday problems. Find me on Facebook and Twitter.

1
Leave a Reply

This comment form is under antispam protection
1 Comment threads
0 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
1 Comment authors
This comment form is under antispam protection
  Subscribe  
newest oldest most voted
Notify of
Muramuzi Denis

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

Vote Up0Vote Down  Reply
3 years ago

Search Tutorials

Spring MVC Tutorial

  • MVC – Introduction
  • MVC – Hello World
  • MVC – JSTL
  • MVC – @RequestMapping
  • MVC – Custom Validator
  • MVC – JSR-303 Validation
  • MVC – Dropdown
  • MVC – Submit Form
  • MVC – MessageSourceAware
  • MVC – XmlViewResolver
  • MVC – i18n and i10n
  • MVC – Interceptor
  • MVC – HandlerInterceptor
  • MVC – Multi File Upload (Ajax)
  • MVC – Multi File Upload
  • MVC – File Download
  • MVC – Interview Questions
  • InternalResourceViewResolver
  • ResourceBundleViewResolver
  • SimpleMappingExceptionResolver
  • annotation-config vs component-scan
  • ContextLoaderListener vs DispatcherServlet

Popular Tutorials

  • Java 8 Tutorial
  • Core Java Tutorial
  • Collections in Java
  • Java Concurrency
  • Spring Boot Tutorial
  • Spring AOP Tutorial
  • Spring MVC Tutorial
  • Spring Security Tutorial
  • Hibernate Tutorial
  • Python Tutorial
  • Jersey Tutorial
  • Maven Tutorial
  • Log4j Tutorial
  • Regex Tutorial

Meta Links

  • Advertise
  • Contact Us
  • Privacy policy
  • About Me

Recommended Reading

  • 10 Life Lessons
  • Secure Hash Algorithms
  • How Web Servers work?
  • How Java I/O Works Internally?
  • Best Way to Learn Java
  • Java Best Practices Guide
  • Microservices Tutorial
  • REST API Tutorial
  • How to Start New Blog

Copyright © 2016 · HowToDoInjava.com · All Rights Reserved. | Sitemap

wpDiscuz