Home  >  Spring  >  Spring MVC

Spring MVC ResourceBundleViewResolver Example with Java Config

April 06, 2015
ResourceBundleViewResolver is a ViewResolver which uses ResourceBundle property file for bean definitions. Property file is located in classpath. A bean of ResourceBundleViewResolver is created and base property file name is configured using setBasename() . We achieve internationalization using ResourceBundleViewResolver. In ResourceBundle property file, there are key-value pair values. The key will be created using (string value returned by controller method + attribute of different view classes like JstlView and RedirectView).

result.(class): This is a key which will be used to assign view class like JstlView and RedirectView . Here result is a string value retuned by controller method.
result.url : For the result value, we need to assign page.

The default file name is views. We can use any name, but base name needs to configure with ResourceBundleViewResolver using its setBasename() method. For i18, we can create different file name with different language code.

Resource Bundle Property File

Find the property file. Here we are configuring data for two retuned values of controller method.

Our resource bundle file is for en language code. Hence file name is appended with _en.

Print Screen of Project Structure in Eclipse

Find the project structure in eclipse. The property file will be kept in classpath.
Spring MVC ResourceBundleViewResolver Example with Java Config

Java Config for ResourceBundleViewResolver Bean

Find the java configuration file to define ResourceBundleViewResolver bean.

package com.concretepage.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;
public class ResolverConfig {  
    public ResourceBundleViewResolver resourceBundleViewResolver() {  
	ResourceBundleViewResolver resolver = new ResourceBundleViewResolver();
        return resolver;  

Create Controller

Find the controller class. Here we have two methods one for testing JstlView and second for RedirectView.

package com.concretepage;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
public class MyWorldController {
    public String info(Model model) {
	 model.addAttribute("msg", "ResourceBundleViewResolver Demo");
         return "result";
    public String getInfo() {
         return "refer";

Create JSP

We are creating a JSP file to test JstlView.

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


Build and deploy WAR in tomcat, and access URL to test JstlView.
1. http://localhost:8080/concretepage-1/myworld/info
Spring MVC ResourceBundleViewResolver Example with Java Config
2. Access URL http://localhost:8080/concretepage-1/myworld/home
to test RedirectView, we will be redirected at our given absolute URL.

Download Complete Source Code


©2021 concretepage.com | Privacy Policy | Contact Us