How to use InternalResourceViewResolver In Spring MVC

Now that, we have configured InternalResourceViewResolver with any of the above types, then we can use logical view names in our controllers, instead of the physical resource path.

Our configured InternalResourceViewResolver will map our logical view_name to the actual physical resource by adding the prefix and suffix to the logical name.

  • In the controllers edit the view_name string to only contain the logical name(filename)
  • In the controllers while returning the view_names as the output omit the path and extension.

Java




package com.example.controller;
  
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
  
import java.util.Map;
  
//Controller for MVC
@Controller
public class HelloController {
    
    // mapping /hello url with get type of request
    // requestparam to get the name value from the
    // get request url, map used to pass value to result.jsp
    @GetMapping("/hello")
    public String getView(@RequestParam("name") String name,
                          Map<String, Object> map)
    {
        map.put("name", name);
          // logical name of the hello.jsp
        return "hello";
    }
}


Output:

output gif

Explanation of the above code:

  • In the above code, the result view_name has been edited to only contain the logical name and not the entire path.
  • Our configured InternalResourceViewResolver will add the prefix of /WEB-INF/jsp/ and a suffix of .jsp to the logical view name “hello” will be resolved to /WEB-INF/jsp/hello.jsp at runtime.

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

...