Home Tutorial Spring Spring3 Web Spring MVC XmlViewResolver Example

 
 

Share on Google+Share on Google+
Spring MVC XmlViewResolver Example
Posted on: August 22, 2012 at 12:00 AM
Advertisement
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

Advertisement

Related Tags for Spring MVC XmlViewResolver Example:


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: August 22, 2012

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com