Springs

Spring MVC InternalResourceViewResolver example

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.

The Spring InternalResourceViewResolver is used to resolve the provided URI to actual URI. The following example shows how to use the InternalResourceViewResolver using the Spring Web MVC Framework. The InternalResourceViewResolver allows mapping webpages with requests.  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.

Steps to create MVC ViewResolver:

  1. Create a project with a name TestWeb explained in the Spring MVC – Hello World Example chapter.
  2. Create a Java classes HelloController .
  3. Create a view file hello.jsp under jsp sub-folder.
  4. The final step is to create the content of the source and configuration files and export the application as explained below.

1. Controller

Controller class to return view named “”hello”, when someone enters http:\\localhost:8080\TestWeb\hello

 

2. InternalResourceViewResolver — TestWeb-servlet.xml

 

3. View — hello.jsp

 

Now, deploy your application on Tomcat server and start your Tomcat server and make sure you are able to access other webpages from the webapps folder using a standard browser. Try to access the URL – http://localhost:8080/TestWeb/hello and if everything is fine with the Spring Web Application, we will see the following screen.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.