Spring MVC XmlViewResolver Example


 

Spring MVC XmlViewResolver Example

In this tutorial you will learn about the XmlViewResolver

In this tutorial you will learn about the XmlViewResolver

Spring MVC XmlViewResolver Example

org.springframework.web.servlet.view.XmlViewResolver allows us to write view defnition file with the same using the same familiar file. The default file name and location is WEB-INF/views.xml

The following is the mapping for XmlViewResolver in dispatcher-servlet.xml

<?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"
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">

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

	<bean class="org.springframework.web.servlet.view.XmlViewResolver">
		<property name="location">
			<value>/WEB-INF/spring-views.xml</value>
		</property>
		<property name="order" value="3" />
	</bean>
</beans>

The above configuration fill find out the spring-views.xml in the WEB-INF directory.

The following is the configuration for spring-views.xml

<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-2.5.xsd">

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

The controller class for above configurations is as

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 "springView";
	}
}

Download Complete Source Code

Ads