Spring MVC InternalResourceViewResolver Configuration Example

<div dir&equals;"ltr" style&equals;"text-align&colon; justify&semi;" trbidi&equals;"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&period; This logical view name is returned by the handler method of controller&period; In Spring MVC InternalResourceViewResolver is used to resolve &&num;8220&semi;internal resource view&&num;8221&semi; &lpar;i&period;e&period; JSP under the WEB-INF directory&rpar; based on a predefined URL pattern&period;<&sol;p>&NewLine;<div align&equals;"center" id&equals;"ads-id"><&sol;div>&NewLine;<p><b>How to Resolve View<&sol;b><br &sol;>&NewLine;In Spring MVC&comma; InternalResourceViewResolver allow us to add some predefined prefix and suffix to the view name &lpar;prefix &plus; view name &plus; suffix&rpar;&comma; and generate the final view page URL as below&period;<&sol;p>&NewLine;<p><b><i>prefix &plus; logical view name &plus; suffix &equals; &sol;WEB-INF&sol;view&sol;MyPage&period;jsp<&sol;i><&sol;b><&sol;p>&NewLine;<div style&equals;"background-color&colon; &num;f2f9fc&semi; border&colon;1px solid &num;c9e6f2&semi;border-radius&colon;3px&semi;padding&colon;16px&semi;line-height&colon;1&period;45&semi;"><b>Popular Tutorials<&sol;b><&sol;p>&NewLine;<ul style&equals;"text-align&colon; left&semi;">&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;2012&sol;06&sol;spring-30-baby-step-to-learn&period;html"><em><strong>Spring Tutorial<&sol;strong> <&sol;em><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;2012&sol;12&sol;spring-web-mvc-framework-chapter-38&period;html"><strong><em>Spring MVC Web Tutorial <&sol;em><&sol;strong><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;2016&sol;06&sol;introduction-to-spring-boot-a-spring-boot-complete-guide&period;html"><em><strong>Spring Boot Tutorial<&sol;strong> <&sol;em><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;2013&sol;02&sol;spring-security-take-baby-step-to-secure&period;html"><em>Spring Security Tutorial<&sol;em><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;2012&sol;07&sol;introduction-to-aop-in-spring&period;html"><em>Spring AOP Tutorial<&sol;em><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;2012&sol;12&sol;using-spring-jdbc-framework-chapter-32&period;html"><em>Spring JDBC Tutorial<&sol;em><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;2017&sol;01&sol;spring-hateoas-hypermedia-driven-restful-web-service&period;html"><em><strong>Spring HATEOAS <&sol;strong><&sol;em><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;2017&sol;01&sol;microservices-with-spring-boot&period;html"><em><strong>Microservices with Spring Boot<&sol;strong><&sol;em><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;2013&sol;06&sol;jax-rs-web-service-tutorial&period;html"><strong><em>REST Webservice<&sol;em> <&sol;strong><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;2013&sol;01&sol;core-java-baby-step-to-be-best-java-ian&period;html"><em><strong>Core Java <&sol;strong><&sol;em><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;2012&sol;03&sol;hibernate-3-on-baby-steps&period;html"><em><strong>Hibernate Tutorial<&sol;strong><&sol;em><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;2012&sol;12&sol;spring-batch-process-with-example&period;html"><strong><em>Spring Batch<&sol;em> <&sol;strong><&sol;a><&sol;b><&sol;li>&NewLine;<&sol;ul>&NewLine;<&sol;div>&NewLine;<p>&NewLine;<b>Configuring <i>InternalResourceViewResolver <&sol;i>in Spring MVC<&sol;b><br &sol;>&NewLine;To configure InternalResourceViewResolver&comma; you can declare a bean of this type in the web application context either with XML configuration or with Java Configuration as below&period;<&sol;p>&NewLine;<p><b>In XML Configuration<&sol;b><&sol;p>&NewLine;<pre class&equals;"highlight">&lt&semi;bean class&equals;"org&period;springframework&period;web&period;servlet&period;view&period;InternalResourceViewResolver"&gt&semi;&NewLine; &lt&semi;property name&equals;"suffix" value&equals;"&period;jsp"&sol;&gt&semi;&NewLine; &lt&semi;property name&equals;"prefix" value&equals;"&sol;WEB-INF&sol;view&sol;"&sol;&gt&semi;&NewLine;&lt&semi;&sol;bean&gt&semi;&NewLine;<&sol;pre>&NewLine;<p>&NewLine;<b>In Java Configuration<&sol;b><&sol;p>&NewLine;<pre class&equals;"highlight">&commat;Bean&NewLine;public ViewResolver viewResolver&lpar;&rpar;&lbrace;&NewLine; InternalResourceViewResolver viewResolver &equals; new InternalResourceViewResolver&lpar;&rpar;&semi;&NewLine; viewResolver&period;setPrefix&lpar;"&sol;WEB-INF&sol;view&sol;"&rpar;&semi;&NewLine; viewResolver&period;setSuffix&lpar;"&period;jsp"&rpar;&semi;&NewLine; return viewResolver&semi;&NewLine;&rcub;&NewLine;<&sol;pre>&NewLine;<p>&NewLine;InternalResourceViewResolver resolves view names into view objects of type JstlView by default if the JSTL library jstl&period;jar available into classpath of the application&period; So you do not need to explicitly add the viewClass property&period; But you can override this property by adding viewClass property of InternalResourceViewResolver&period; In below code we add other view class TilesView instead of JstlView class based on tiles&period;<&sol;p>&NewLine;<p><b>In XML Configuration<&sol;b><&sol;p>&NewLine;<pre class&equals;"highlight">&lt&semi;bean class&equals;"org&period;springframework&period;web&period;servlet&period;view&period;InternalResourceViewResolver"&gt&semi;&NewLine; &lt&semi;property name&equals;"suffix" value&equals;"&period;jsp"&sol;&gt&semi;&NewLine; &lt&semi;property name&equals;"prefix" value&equals;"&sol;WEB-INF&sol;view&sol;"&sol;&gt&semi;&NewLine; &lt&semi;property name&equals;"viewClass" value&equals;"org&period;springframework&period;web&period;servlet&period;view&period;tiles2&period;TilesView" &sol;&gt&semi;&NewLine;&lt&semi;&sol;bean&gt&semi;&NewLine;<&sol;pre>&NewLine;<p>&NewLine;<b>In Java Configuration<&sol;b><&sol;p>&NewLine;<pre class&equals;"highlight">&commat;Bean&NewLine;public ViewResolver viewResolver&lpar;&rpar;&lbrace;&NewLine; InternalResourceViewResolver viewResolver &equals; new InternalResourceViewResolver&lpar;&rpar;&semi;&NewLine; viewResolver&period;setPrefix&lpar;"&sol;WEB-INF&sol;view&sol;"&rpar;&semi;&NewLine; viewResolver&period;setSuffix&lpar;"&period;jsp"&rpar;&semi;&NewLine; viewResolver&period;setViewClass&lpar;TilesView&period;class&rpar;&semi;&NewLine; return viewResolver&semi;&NewLine;&rcub;&NewLine;<&sol;pre>&NewLine;<p>&NewLine;<b><i>What’s internal resource views&quest;<&sol;i><&sol;b><br &sol;>&NewLine;In Spring MVC or any web application&comma; for good practice&comma; it’s always recommended to put the entire views or JSP files under &OpenCurlyDoubleQuote;WEB-INF” folder&comma; to protect it from direct access via manual entered URL&period; Those views under &OpenCurlyDoubleQuote;WEB-INF” folder are named as internal resource views&comma; as it’s only accessible by the servlet or Spring’s controllers class&period;<&sol;p>&NewLine;<p><b>Summary <&sol;b><&sol;p>&NewLine;<ol style&equals;"text-align&colon; left&semi;">&NewLine;<li>&nbsp&semi;Controller return the logical view name to the DispatcherServlet&period;<&sol;li>&NewLine;<li>&nbsp&semi;DispatcherServlet has to delegate control to a view template so the information is rendered&period;<&sol;li>&NewLine;<li>&nbsp&semi;This view template decides that which view should be rendered based on returned logical view name&period;<&sol;li>&NewLine;<li>&nbsp&semi;These view templates are one or more view resolver beans declared in the web application context&period;<&sol;li>&NewLine;<li>&nbsp&semi;These beans have to implement the ViewResolver interface for DispatcherServlet to auto-detect them&period;<&sol;li>&NewLine;<li>&nbsp&semi;Spring MVC comes with several ViewResolver implementations&period;<&sol;li>&NewLine;<&sol;ol>&NewLine;<p><&sol;p>&NewLine;<div style&equals;"background-color&colon; &num;f2f9fc&semi; border&colon;1px solid &num;c9e6f2&semi;border-radius&colon;3px&semi;padding&colon;16px&semi;line-height&colon;1&period;45&semi;"><b>Spring MVC Related Posts<&sol;b><&sol;p>&NewLine;<ol style&equals;"text-align&colon; left&semi;">&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;2012&sol;12&sol;spring-web-mvc-framework-chapter-38&period;html">Spring MVC Web Tutorial<&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;2017&sol;02&sol;spring-mvc-interview-questions-and-answers&period;html">Spring MVC Interview Questions<&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;2013&sol;07&sol;introduction-to-mvc&period;html">MVC Design Pattern<&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;2017&sol;02&sol;what-is-dispatcherservlet-in-spring-and-its-uses&period;html">Spring MVC DispatcherServlet <&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;2017&sol;02&sol;difference-between-applicationcontext-webapplicationcontext-in-spring-mvc&period;html">Spring MVC WebApplicationContext and Root Application Context<&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;2012&sol;07&sol;stereotype-annotations-in-spring&period;html">Spring MVC &commat;Controller Annotation<&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;2016&sol;03&sol;requestmapping-annotation-in-spring-mvc&period;html">Spring MVC &commat;RequestMapping Annotation<&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;2017&sol;02&sol;requestparam-annotation-in-spring-mvc-with-example&period;html">Spring MVC &commat;RequestParam Annotation<&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;2017&sol;02&sol;difference-between-applicationcontext-webapplicationcontext-in-spring-mvc&period;html">Spring MVC ContextLoaderListener<&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;2017&sol;02&sol;requestparam-vs-pathvariable-annotations-in-spring-mvc&period;html">Spring MVC &commat;RequestParam and &commat;PathVariable annotations<&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;2012&sol;12&sol;spring-30-mvc-hello-world-example&period;html">Spring MVC Hello World Example<&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;2012&sol;12&sol;spring-exception-handling-example&period;html">Spring MVC Exception Handling Example<&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;2012&sol;12&sol;spring-mvc-with-hibernate-crud-example&period;html">Spring MVC with Hibernate CRUD Example<&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;2012&sol;12&sol;spring-3-mvc-tiles-plugin-with-example&period;html">Spring MVC Tiles Plugin with Example<&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;2012&sol;12&sol;spring-3-mvc-and-interceptor-with&period;html">Spring MVC Interceptor with example<&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;2013&sol;01&sol;integration-spring-mvc3-and-mongodb&period;html">Spring MVC with MongoDB CRUD Example<&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;2012&sol;12&sol;spring-3-mvc-internationalization&period;html">Spring MVC Internationalization &&num;038&semi; Localization with Example<&sol;a><&sol;b><&sol;li>&NewLine;<&sol;ol>&NewLine;<&sol;div>&NewLine;<&sol;div>&NewLine;<div class&equals;"wp-post-navigation"> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <div class&equals;"wp-post-navigation-pre"> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;spring-mvc-interview-questions-and-answers&sol;">Previous<&sol;a> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <&sol;div> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <div class&equals;"wp-post-navigation-next"> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;spring-aop-interview-questions-and-answers&sol;">Next<&sol;a> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <&sol;div> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;<&sol;div>&NewLine;<script type&equals;"text&sol;javascript">&NewLine;jQuery&lpar;document&rpar;&period;ready&lpar;function&lpar;&dollar;&rpar; &lbrace;&NewLine; &dollar;&period;post&lpar;'https&colon;&sol;&sol;dineshonjava&period;com&sol;wp-admin&sol;admin-ajax&period;php'&comma; &lbrace;action&colon; 'mts&lowbar;view&lowbar;count'&comma; id&colon; '73'&rcub;&rpar;&semi;&NewLine;&rcub;&rpar;&semi;&NewLine;<&sol;script>

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…

4 years ago

Decorator Pattern using Lambda

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

4 years ago

Delegating pattern using lambda

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

4 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;…

4 years ago

TOP 20 MongoDB INTERVIEW QUESTIONS 2022

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

4 years ago

Scheduler @Scheduled Annotation Spring Boot

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

4 years ago