Spring MVC ResourceBundle View Resolver is used to resolve views based on the entries in to a specified properties file. The property file name is to be specified while configuring the
org.springframework.web.servlet.view.ResourceBundleViewResolver bean in the dispatcher servlet.
If no property file name is given, then ResourceBundleViewResolver loads the views from the default property file – views.property from the project classpath.
If the ResourceBundleViewResolver is unable to find any valid view from any of the property files, Or if it is unable to find the property file itself then we may get the following exception
1 2 3 4 5 6 |
HTTP Status 500 - Request processing failed; nested exception is java.util.MissingResourceException: Can't find bundle for base name my-views, locale en_US type Exception report message Request processing failed; nested exception is java.util.MissingResourceException: Can't find bundle for base name |
Lets see an example of Spring MVC ResourceBundle View Resolver
1. dispatcher servlet configuration
We are using the properties file my-views.properties, so we have defined it in the ResourceBundleViewResolver
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" 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 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <context:component-scan base-package="com.kscodes.sampleproject" /> <bean class="org.springframework.web.servlet.view.ResourceBundleViewResolver"> <property name="basename" value="my-views" /> </bean> </beans> |
2. my-views.properties
The things to remember while creating the properties file are
1. Name of the View – home
2. Type of the View – .class
3. URL of the View – .url
1 2 |
home.(class)=org.springframework.web.servlet.view.JstlView home.url=/WEB-INF/jsp/home.jsp |
3. Controller
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
package com.kscodes.sampleproject.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @Controller public class ResourceBundleResolverController { @RequestMapping(value = "/home") public String showHome() { return "home"; } } |
4. home.jsp
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <html> <head> <title>Spring MVC - ResourceBundle View Resolver Sample</title> </head> <body> <h2>This is My Home Page</h2> <h3>Thanks for visiting my home page !!!!!!!!! </h3> </body> </html> |
Output
When view name home is returned by controller, the ResourceBundleViewResolver will find the key starting with home in my-views.properties file, and return the view that is defined in home.url i.e. /WEB-INF/jsp/home.jsp