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.spring-mvc-views_en.properties
result.(class)=org.springframework.web.servlet.view.JstlView result.url=/views/success.jsp refer.(class)=org.springframework.web.servlet.view.RedirectView refer.url=http://concretepage.com
Print Screen of Project Structure in Eclipse
Find the project structure in eclipse. The property file will be kept in classpath.Java Config for ResourceBundleViewResolver Bean
Find the java configuration file to define ResourceBundleViewResolver bean.ResolverConfig.java
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; @Configuration @ComponentScan("com.concretepage") public class ResolverConfig { @Bean public ResourceBundleViewResolver resourceBundleViewResolver() { ResourceBundleViewResolver resolver = new ResourceBundleViewResolver(); resolver.setBasename("spring-mvc-views"); return resolver; } }
Create Controller
Find the controller class. Here we have two methods one for testing JstlView and second for RedirectView.MyWorldController.java
package com.concretepage; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; @Controller @RequestMapping("/myworld") public class MyWorldController { @RequestMapping("/info") public String info(Model model) { model.addAttribute("msg", "ResourceBundleViewResolver Demo"); return "result"; } @RequestMapping("/home") public String getInfo() { return "refer"; } }
Create JSP
We are creating a JSP file to test JstlView.success.jsp
<html> <head> <title> Spring MVC ResourceBundleViewResolver </title> </head> <body> <h1>Message : ${msg}</h1> </body> </html>
Output
Build and deploy WAR in tomcat, and access URL to test JstlView.1. http://localhost:8080/concretepage-1/myworld/info
to test RedirectView, we will be redirected at our given absolute URL.