Open In App

Spring MVC – ResourceBundleViewResolver Configuration

Last Updated : 15 Jan, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

ResourceBundleViewResolver in Spring MVC is used to resolve “view named” by view beans in the “.properties” file. View beans defined in the properties file are utilized by the ResourceBundleViewResolver to determine the view names. Using the Spring Web MVC Framework, the ResourceBundleViewResolver may be utilized as demonstrated in the following example.

ResourceBundleViewResolver Configuration in Spring MVC

ResourceBundleViewResolver is a ViewResolver that uses a ResourceBundle property file for bean definitions. It is built with a bean and a base property file name set using setBasename(). This function performs internationalization and generates key-value pair values for view classes like JstlView and RedirectView. The default file name is viewed, but the base name can be changed using setBasename().

Resource Bundle Property File

Locate the property file. In this case, the data for two returned controller method values are being configured.

result.(class)=org.springframework.web.servlet.view.JstlView
result.url=/views/success.jsp
refer.(class)=org.springframework.web.servlet.view.RedirectView
refer.url=http://geeksforgeeks.org

Java Config for ResourceBundleViewResolver Bean

To define the ResourceBundleViewResolver bean, locate the Java configuration file:

Java




package org.geeksforgeeks.config;  
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.view.ResourceBundleViewResolver;
@Configuration 
@ComponentScan("org.geeksforgeeks"
public class ResolverConfig {  
    @Bean  //bean configuration
    public ResourceBundleViewResolver resourceBundleViewResolver() {  
    ResourceBundleViewResolver resolver = new ResourceBundleViewResolver();   //object creation
    resolver.setBasename("spring-mvc-views");
        return resolver;  
    }


Create Controller

Locate the class controller. Two methods are available here: one for testing JstlView and the other for RedirectView.

Java




package org.geeksforgeeks;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("/myworld")   //Mapping will be done here
public class MyWorldController {
    @RequestMapping("/info")     //Controller MAPPING
    public String info(Model model) {
     model.addAttribute("msg", "ResourceBundleViewResolver");
         return "result";
    }
    @RequestMapping("/home")
    public String getInfo() {
         return "refer";
    }


Create a JSP file

To test JstlView, we are generating a JSP file.

Java




<html>
<head>
<title> Spring MVC ResourceBundleViewResolver </title>
</head>
    <body>
    <h1>Message :  ${msg}</h1>
    </body>
</html>


Output:

Output Screen

Now launch your Tomcat server and check if a regular browser can visit additional webpages from the webapps subdirectory.

Conclusion

In this article, we learnt on the Java Spring MVC – ResourceBundleViewResolver Configuration. Using the view beans specified in the properties file, the ResourceBundleViewResolver resolves the view names. The ResourceBundleViewResolver may be utilized with the Spring Web MVC Framework.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads