HowToDoInJava

  • Java 8
  • Regex
  • Concurrency
  • Best Practices
  • Spring Boot
  • JUnit5
  • Interview Questions
  • Dark Mode

Spring MVC ResourceBundleViewResolver Configuration Example

By Lokesh Gupta | Filed Under: Spring MVC

In previous examples, we learned about XmlViewResolver and InternalResourceViewResolver view templates. In this post, we will learn about ResourceBundleViewResolver templates. ResourceBundleViewResolver loads view beans from a resource bundle in the classpath root. Note that ResourceBundleViewResolver can also take advantage of the resource bundle capability to load view beans from different resource bundles for different locales (this is not possible with other two view resolvers).

By default, ResourceBundleViewResolver loads view names from “views.properties” file present into classpath but this location can be overridden through the basename property.

<bean class="org.springframework.web.servlet.view.ResourceBundleViewResolver">
    <property name="basename" value="views" />
</bean>

In views.properties resource bundle, you can declare view beans in the format of properties and their declaration is equivalent to the XML bean declaration which we saw in XmlViewResolver example.

<!--beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

    <bean id="home" class="org.springframework.web.servlet.view.JstlView">
        <property name="url" value="/WEB-INF/jsp/home.jsp" />
    </bean>

    <bean id="admin/home" class="org.springframework.web.servlet.view.JstlView">
        <property name="url" value="/WEB-INF/jsp/admin/home.jsp" />
    </bean>

    <bean id="logOffRedirect" class="org.springframework.web.servlet.view.RedirectView">
        <property name="url" value="home" />
    </bean>
</beans-->

//Equivalent resource bundle properites

home.(class)=org.springframework.web.servlet.view.JstlView
home.url=/WEB-INF/jsp/home.jsp


adminHome.(class)=org.springframework.web.servlet.view.JstlView
adminHome.url=/WEB-INF/jsp/admin/home.jsp

logOffRedirect.(class)=org.springframework.web.servlet.view.RedirectView
logOffRedirect.url=home

With above configuration, when controller return a logical view name “home” then ResourceBundleViewResolver will find the key starting with “home” in views.properties file, and return the corresponding view’s URL i.e. “/WEB-INF/jsp/home.jsp” back to the DispatcherServlet.

Drop me your queries if something needs more explanation.

Happy Learning !!

TwitterFacebookLinkedinRedditPocket

About Lokesh Gupta

A family guy with fun loving nature. Love computers, programming and solving everyday problems. Find me on Facebook and Twitter.

1
Leave a Reply

This comment form is under antispam protection
1 Comment threads
0 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
1 Comment authors
This comment form is under antispam protection
  Subscribe  
newest oldest most voted
Notify of
Binh Thanh Nguyen

Thanks, nice tip

Vote Up0Vote Down  Reply
4 years ago

Search Tutorials

Spring MVC Tutorial

  • MVC – Introduction
  • MVC – Hello World
  • MVC – JSTL
  • MVC – @RequestMapping
  • MVC – Custom Validator
  • MVC – JSR-303 Validation
  • MVC – Dropdown
  • MVC – Submit Form
  • MVC – MessageSourceAware
  • MVC – XmlViewResolver
  • MVC – i18n and i10n
  • MVC – Interceptor
  • MVC – HandlerInterceptor
  • MVC – Multi File Upload (Ajax)
  • MVC – Multi File Upload
  • MVC – File Download
  • MVC – Interview Questions
  • InternalResourceViewResolver
  • ResourceBundleViewResolver
  • SimpleMappingExceptionResolver
  • annotation-config vs component-scan
  • ContextLoaderListener vs DispatcherServlet

Popular Tutorials

  • Java 8 Tutorial
  • Core Java Tutorial
  • Collections in Java
  • Java Concurrency
  • Spring Boot Tutorial
  • Spring AOP Tutorial
  • Spring MVC Tutorial
  • Spring Security Tutorial
  • Hibernate Tutorial
  • Python Tutorial
  • Jersey Tutorial
  • Maven Tutorial
  • Log4j Tutorial
  • Regex Tutorial

Meta Links

  • Advertise
  • Contact Us
  • Privacy policy
  • About Me

Recommended Reading

  • 10 Life Lessons
  • Secure Hash Algorithms
  • How Web Servers work?
  • How Java I/O Works Internally?
  • Best Way to Learn Java
  • Java Best Practices Guide
  • Microservices Tutorial
  • REST API Tutorial
  • How to Start New Blog

Copyright © 2016 · HowToDoInjava.com · All Rights Reserved. | Sitemap

wpDiscuz