Configuring InternalResourceViewResolver

In the initial configuration we have returned a string as a view name /WEB-INF/jsp/hello.jsp as doing so our code is tightly coupled with that hello.jsp in that particular directory. Changing the path and templating extension will be hard as we need to edit all our view_name in Controllers.

But we can configure InternalResourceViewResolver for us to dynamically map the logical name returned by us to the particular physical resource located in any directory and using different template extensions.

Split the actual physical resource name into multiple parts:

Actual path =>  prefix + view_name + suffix
  • prefix: path from webapp to the directory which contains the view file. (/WEB-INF/jsp/)
  • view_name: file name of the view (hello)
  • suffix: extension used by the template engine (.jsp)

In the controllers that use only the logical view_name, our configured InternalResourceViewResolver will map our view_name by adding the prefix and suffix and will resolve the view_name to the actual physical resource name.

Spring MVC – InternalResourceViewResolver Configuration

Spring MVC is a powerful tool to create web projects provided by the Spring framework. Spring MVC follows the Model-View-Controller Architecture internally to handle and process the data, where the data will be visualized via the View to the user from the Model layer. The interaction will be monitored and handled by the Controller layer of the Architecture.

In Spring MVC, InternalResourceViewResolver is a class that plays a vital role in mapping the logical view name to the actual physical resource view. Using logical view names helps us to achieve code modularity and a flexible way to manage the views.

Prerequisite

  • Java – Core and Advanced Java
  • Spring & Spring MVC
  • Maven & Eclipse or any other IDE’s for Java

Similar Reads

InternalResourceViewResolver

In Spring MVC, the views are returned as a response to the requests, one can directly return the physical resource name but it will make the code tightly coupled to the physical resource, and changing the view templates will be hard. Another way would be...

Initial Setup

Before starting with the InternalResourceViewResolver configuration we need to have a working Spring MVC project to carry out the configuration. Let’s set it up first....

Configuring InternalResourceViewResolver

...

Configuration Method

...

1. XML Configuration

...

2. Java Configuration

...

Using InternalResourceViewResolver

...

Conclusion

...