Java Configuration

Spring also provides a way for configuration programmatically using Java Annotations. This way of configuration is less verbose compared to XML-based configuration. Providing only a configuration class and some beans will be enough.

Procedure to configure InternalResourceViewResolver using Java code:

  • Create a configuration class under the config subpackage.
  • Provide configuration objects in terms of spring beans using @Bean annotated methods.
  • Configure the prefix and suffix attribute of the InternalResourceViewResolver bean.

Java




// File Name: AppConfig.java
package com.example.config;
  
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
  
//config class
@Configuration
public class AppConfig implements WebMvcConfigurer {
    
    // creating InternalResourceViewResolver bean
    @Bean
    public ViewResolver getInternalResourceViewResolver(){
        InternalResourceViewResolver viewResolver
            = new InternalResourceViewResolver();
        
        // setting prefix and suffix to the path & extension
        viewResolver.setPrefix("/WEB-INF/jsp/");
        viewResolver.setSuffix(".jsp");
        return viewResolver;
    }
}


Explanation of the above code:

  • A configuration class has been created with @Configuration spring-context annotation to provide configurations.
  • The configuration class should implement the WebMvcConfigurer Interface to provide Spring MVC configuration.
  • Create a method and annotate it @Bean to provide Spring Beans, here InternalResourceViewResolver is provided as a bean.
  • Create an object of type InternalResourceViewResolver and configure the prefix and suffix of it using setter methods.
  • Set the parent directory path in the prefix and the extension of the resource in the suffix of the object.
  • Return the InternalResourceViewResolver object to use it as a bean in the Spring application context.

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

...