Spring MVC ResourceBundleViewResolver Example


 

Spring MVC ResourceBundleViewResolver Example

In this tutorial you will learn about ResourceBundleViewResolver

In this tutorial you will learn about ResourceBundleViewResolver

Spring MVC ResourceBundleViewResolver Example

org.springframework.web.servlet.view.ResourceBundleViewResolver uses the ResourceBundle from class path by default. That bundle is mapped with their class and location for each views returned by its controller.

The following is the mapping for ResourceBundleViewResolver

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

It will find out spring-views.properties in the class path. This file will contain the mapping for all the views as given bellow.

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

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

A typical dispatcher-servlet.xml is given below

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

	<mvc:annotation-driven />
	<context:component-scan base-package="roseindia.controller" />

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

<bean id="service" class="roseindia.service.serviceImpl.AppServiceImpl">
	<property name="dataSource" ref="dataSource" />
</bean>

</beans>

The Following the controller class

package roseindia.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class AppController {
	@RequestMapping("load-view")
	public String loadView(Model model) {
		model.addAttribute("greetings", "Welcome");
		return "spring-view";
	}
}

Download Complete Source Code

Ads