Main Tutorials

Spring MVC InternalResourceViewResolver example

In Spring MVC, InternalResourceViewResolver is used to resolve “internal resource view” (in simple, it’s final output, jsp or htmp page) based on a predefined URL pattern. In additional, it allow you to add some predefined prefix or suffix to the view name (prefix + view name + suffix), and generate the final view page URL.

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.

Following example show you how InternalResourceViewResolver works :

1. Controller

A controller class to return a view, named “WelcomePage“.


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

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

2. InternalResourceViewResolver

Register InternalResourceViewResolver bean in the Spring’s bean configuration file.


<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
	http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

   <bean 
   class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping" />
	
	<!-- Register the bean -->
	<bean class="com.mkyong.common.controller.WelcomeController" />

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

</beans>

Now, Spring will resolve the view’s name “WelcomePage” in the following way :

prefix + view name + suffix = /WEB-INF/pages/WelcomPage.jsp

Download Source Code

Reference

  1. InternalResourceViewResolver documentation

About Author

author image
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

Subscribe
Notify of
13 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
Somail
6 years ago

I am Placing My .jsp file inside WebContent >Views folder …
In Dispatcher Servlet i configure that then also it showing 404 Error…
please tell me how to resolve..

Bharath Kumar
8 years ago

Hello, The explanation is very simple and easy for beginners like me. I have a doubt what is the url to run the program??

ludin daniel
9 years ago

Can somebody explain to me why the URL must end with welcome.htm? I get the part with the servlet mapping (on *.htm), but where does the “welcome” part come from, since the file is named WelcomePage as well as the view from the controller? Is there an implicit mapping?

ludin daniel
9 years ago
Reply to  ludin daniel

I am giving the answer myself: there is in fact an implicit mapping convention as described here:
https://mkyong.com/spring-mvc/spring-mvc-controllerclassnamehandlermapping-example/

I had not proceeded so far yet. Maybe it would make sense to add this link to the related posts.
And thanks a lot for your fine tutorials.

lemohewe
9 years ago

Could you explain the difference in path values /WEB-INF/pages/ and WEB-INF/pages/ Why is ‘/’ so important?

nagkumar
9 years ago

After deploying the war, to test this code, one needs to visit the URL http://localhost:8080/SpringMVC/welcome.htm

As only *.htm extensions are mapped to be handled by mvc-dispatcher

venkatesh
10 years ago

HI,

I have a question. Internal View resolver has to be registered inorder to resolve a jsp page with prefix.

But how to do that if I have some thousands of jsp pages.

I want to keep them in folder like

WEB-INF/jsp/test1
/test2
/test3
/test4 etc

or

WEB-INF/test1
/test2
/test3
/test4

foobar
10 years ago
Reply to  venkatesh

Hi,

just place your .jsp page in subfolder and write name with subfolders name.

ModelAndView model = new ModelAndView(“subfolderName/WelcomePage”);

Stab
11 years ago

Thank you for the tutorial.

Can I you the same method to set *.jsf file or xhtml file formats in suffix?

leon
11 years ago

great article.
Thanks.

vikas gupta
12 years ago

its really nice article, provides the overview with clear picture about its concept.

Raj
13 years ago

I really appreciate these tutorials…i am really impressed.
you made learning spring very simple.
Great articles. great presentation.
Thank you.