<div dir="ltr" style="text-align: justify;" trbidi="on">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 &#8220;internal resource view&#8221; (i.e. JSP under the WEB-INF directory) based on a predefined URL pattern.</p>
<div align="center" id="ads-id"></div>
<p><b>How to Resolve View</b><br />
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.</p>
<p><b><i>prefix + logical view name + suffix = /WEB-INF/view/MyPage.jsp</i></b></p>
<div style="background-color: #f2f9fc; border:1px solid #c9e6f2;border-radius:3px;padding:16px;line-height:1.45;"><b>Popular Tutorials</b></p>
<ul style="text-align: left;">
<li><b><a href="https://dineshonjava.com/2012/06/spring-30-baby-step-to-learn.html"><em><strong>Spring Tutorial</strong> </em></a></b></li>
<li><b><a href="https://dineshonjava.com/2012/12/spring-web-mvc-framework-chapter-38.html"><strong><em>Spring MVC Web Tutorial </em></strong></a></b></li>
<li><b><a href="https://dineshonjava.com/2016/06/introduction-to-spring-boot-a-spring-boot-complete-guide.html"><em><strong>Spring Boot Tutorial</strong> </em></a></b></li>
<li><b><a href="https://dineshonjava.com/2013/02/spring-security-take-baby-step-to-secure.html"><em>Spring Security Tutorial</em></a></b></li>
<li><b><a href="https://dineshonjava.com/2012/07/introduction-to-aop-in-spring.html"><em>Spring AOP Tutorial</em></a></b></li>
<li><b><a href="https://dineshonjava.com/2012/12/using-spring-jdbc-framework-chapter-32.html"><em>Spring JDBC Tutorial</em></a></b></li>
<li><b><a href="https://dineshonjava.com/2017/01/spring-hateoas-hypermedia-driven-restful-web-service.html"><em><strong>Spring HATEOAS </strong></em></a></b></li>
<li><b><a href="https://dineshonjava.com/2017/01/microservices-with-spring-boot.html"><em><strong>Microservices with Spring Boot</strong></em></a></b></li>
<li><b><a href="https://dineshonjava.com/2013/06/jax-rs-web-service-tutorial.html"><strong><em>REST Webservice</em> </strong></a></b></li>
<li><b><a href="https://dineshonjava.com/2013/01/core-java-baby-step-to-be-best-java-ian.html"><em><strong>Core Java </strong></em></a></b></li>
<li><b><a href="https://dineshonjava.com/2012/03/hibernate-3-on-baby-steps.html"><em><strong>Hibernate Tutorial</strong></em></a></b></li>
<li><b><a href="https://dineshonjava.com/2012/12/spring-batch-process-with-example.html"><strong><em>Spring Batch</em> </strong></a></b></li>
</ul>
</div>
<p>
<b>Configuring <i>InternalResourceViewResolver </i>in Spring MVC</b><br />
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.</p>
<p><b>In XML Configuration</b></p>
<pre class="highlight"><;bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">;
 <;property name="suffix" value=".jsp"/>;
 <;property name="prefix" value="/WEB-INF/view/"/>;
<;/bean>;
</pre>
<p>
<b>In Java Configuration</b></p>
<pre class="highlight">@Bean
public ViewResolver viewResolver(){
 InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
 viewResolver.setPrefix("/WEB-INF/view/");
 viewResolver.setSuffix(".jsp");
 return viewResolver;
}
</pre>
<p>
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.</p>
<p><b>In XML Configuration</b></p>
<pre class="highlight"><;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>;
</pre>
<p>
<b>In Java Configuration</b></p>
<pre class="highlight">@Bean
public ViewResolver viewResolver(){
 InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
 viewResolver.setPrefix("/WEB-INF/view/");
 viewResolver.setSuffix(".jsp");
 viewResolver.setViewClass(TilesView.class);
 return viewResolver;
}
</pre>
<p>
<b><i>What’s internal resource views?</i></b><br />
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.</p>
<p><b>Summary </b></p>
<ol style="text-align: left;">
<li> ;Controller return the logical view name to the DispatcherServlet.</li>
<li> ;DispatcherServlet has to delegate control to a view template so the information is rendered.</li>
<li> ;This view template decides that which view should be rendered based on returned logical view name.</li>
<li> ;These view templates are one or more view resolver beans declared in the web application context.</li>
<li> ;These beans have to implement the ViewResolver interface for DispatcherServlet to auto-detect them.</li>
<li> ;Spring MVC comes with several ViewResolver implementations.</li>
</ol>
<p></p>
<div style="background-color: #f2f9fc; border:1px solid #c9e6f2;border-radius:3px;padding:16px;line-height:1.45;"><b>Spring MVC Related Posts</b></p>
<ol style="text-align: left;">
<li><b><a href="https://dineshonjava.com/2012/12/spring-web-mvc-framework-chapter-38.html">Spring MVC Web Tutorial</a></b></li>
<li><b><a href="https://dineshonjava.com/2017/02/spring-mvc-interview-questions-and-answers.html">Spring MVC Interview Questions</a></b></li>
<li><b><a href="https://dineshonjava.com/2013/07/introduction-to-mvc.html">MVC Design Pattern</a></b></li>
<li><b><a href="https://dineshonjava.com/2017/02/what-is-dispatcherservlet-in-spring-and-its-uses.html">Spring MVC DispatcherServlet </a></b></li>
<li><b><a href="https://dineshonjava.com/2017/02/difference-between-applicationcontext-webapplicationcontext-in-spring-mvc.html">Spring MVC WebApplicationContext and Root Application Context</a></b></li>
<li><b><a href="https://dineshonjava.com/2012/07/stereotype-annotations-in-spring.html">Spring MVC @Controller Annotation</a></b></li>
<li><b><a href="https://dineshonjava.com/2016/03/requestmapping-annotation-in-spring-mvc.html">Spring MVC @RequestMapping Annotation</a></b></li>
<li><b><a href="https://dineshonjava.com/2017/02/requestparam-annotation-in-spring-mvc-with-example.html">Spring MVC @RequestParam Annotation</a></b></li>
<li><b><a href="https://dineshonjava.com/2017/02/difference-between-applicationcontext-webapplicationcontext-in-spring-mvc.html">Spring MVC ContextLoaderListener</a></b></li>
<li><b><a href="https://dineshonjava.com/2017/02/requestparam-vs-pathvariable-annotations-in-spring-mvc.html">Spring MVC @RequestParam and @PathVariable annotations</a></b></li>
<li><b><a href="https://dineshonjava.com/2012/12/spring-30-mvc-hello-world-example.html">Spring MVC Hello World Example</a></b></li>
<li><b><a href="https://dineshonjava.com/2012/12/spring-exception-handling-example.html">Spring MVC Exception Handling Example</a></b></li>
<li><b><a href="https://dineshonjava.com/2012/12/spring-mvc-with-hibernate-crud-example.html">Spring MVC with Hibernate CRUD Example</a></b></li>
<li><b><a href="https://dineshonjava.com/2012/12/spring-3-mvc-tiles-plugin-with-example.html">Spring MVC Tiles Plugin with Example</a></b></li>
<li><b><a href="https://dineshonjava.com/2012/12/spring-3-mvc-and-interceptor-with.html">Spring MVC Interceptor with example</a></b></li>
<li><b><a href="https://dineshonjava.com/2013/01/integration-spring-mvc3-and-mongodb.html">Spring MVC with MongoDB CRUD Example</a></b></li>
<li><b><a href="https://dineshonjava.com/2012/12/spring-3-mvc-internationalization.html">Spring MVC Internationalization &#038; Localization with Example</a></b></li>
</ol>
</div>
</div>
<div class="wp-post-navigation"> 
									 <div class="wp-post-navigation-pre"> 
									 <a href="https://dineshonjava.com/spring-mvc-interview-questions-and-answers/">Previous</a> 
									 </div> 
									 <div class="wp-post-navigation-next"> 
									 <a href="https://dineshonjava.com/spring-aop-interview-questions-and-answers/">Next</a> 
									 </div> 
									</div>
<script type="text/javascript">
jQuery(document).ready(function($) {
 $.post('https://dineshonjava.com/wp-admin/admin-ajax.php', {action: 'mts_view_count', id: '73'});
});
</script>
Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…
Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…
Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…
Technology has emerged a lot in the last decade, and now we have artificial intelligence;…
Managing a database is becoming increasingly complex now due to the vast amount of data…
Overview In this article, we will explore Spring Scheduler how we could use it by…