XML Configuration

A traditional way to provide configuration is via XML files to the Spring context. Provide the spring beans in the servlet configuration XML file.

  • Add the InternalResourceViewResolver as a bean in the dispatcher-servlet.xml file using the <bean> tag.
  • Use the property tag to set the prefix and suffix property of the bean.

XML




<?xml version="1.0" encoding="UTF-8"?>  
<beans xmlns="http://www.springframework.org/schema/beans"  
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
    xmlns:context="http://www.springframework.org/schema/context"  
    xmlns:mvc="http://www.springframework.org/schema/mvc"  
    xsi:schemaLocation="  
        http://www.springframework.org/schema/beans  
        http://www.springframework.org/schema/beans/spring-beans.xsd  
        http://www.springframework.org/schema/context  
        http://www.springframework.org/schema/context/spring-context.xsd  
        http://www.springframework.org/schema/mvc  
        http://www.springframework.org/schema/mvc/spring-mvc.xsd">  
      <!-- to use annotation in our classes -->
      <mvc:annotation-driven />
      <!-- scanning for components in the base package "com.example" -->
    <context:component-scan base-package="com.example"></context:component-scan>
      <!-- bean creation -->
    <!-- Configure InternalResourceViewResolver -->
    <bean id="viewResolver"
          class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp" />
    </bean>
</beans>


Explanation of the above code:

  • A bean is created using the <bean> tag and the class is set to the InternalResourceViewResolver class.
  • Configure the prefix and suffix properties of the bean using property tags.
  • Provide the name of the property tag and the preferred value in the value attribute.
  • Here prefix is the directory path from webapp /WEB-INF/jsp/ and the suffix is the .jsp extension of the files.

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

...