Share on Google+Share on Google+

Spring MVC XmlViewResolver Example

Advertisement
In this tutorial you will learn about the Spring MVC XmlViewResolver

Spring web VelocityViewResolver

The org.springframework.web.servlet.view.velocity.VelocityViewResolver allows you to use apache velocity as a view. To use velocity as view you need to first add the VelocityConfigurer as

<bean class="org.springframework.web.servlet.view.velocity.VelocityConfigurer">
	<property name="resourceLoaderPath">
		<value>/</value>
	</property>
</bean>

And then add the mapping for VelocityViewResolver

<bean id="velocityViewResolver"
		class="org.springframework.web.servlet.view.velocity.VelocityViewResolver">
	<property name="prefix">
		<value>/WEB-INF/views/</value>
	</property>
	<property name="suffix">
		<value>.vm</value>
	</property>
</bean>

The following is the complete 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.net.controller" />

<bean class="org.springframework.web.servlet.view.velocity.VelocityConfigurer">
	<property name="resourceLoaderPath">
		<value>/</value>
	</property>
</bean>

<bean id="velocityViewResolver"
			class="org.springframework.web.servlet.view.velocity.VelocityViewResolver">
	<property name="prefix">
		<value>/WEB-INF/views/</value>
	</property>
	<property name="suffix">
		<value>.vm</value>
	</property>
</bean>

</beans>

Controller class for the above application is

package roseindia.net.controller;

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

@Controller
public class AppController {
	@RequestMapping("/load-view")
	public String loadView() {
		return "hello";
	}
}

The velocity view is as follows

<html>
	<body>
		#set( $foo = "Velocity" )
		Hello $foo World!
	</body>
<html>

Download Complete Source Code

Advertisements

Posted on: August 23, 2012 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial 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.