Friday, February 15, 2013

Integrating Velocity with Spring MVC

To integrate Velocity with Spring MVC, we create a Spring MVC by maven then follow these steps

1. Dependencies 

 Open pom.xml file, add these dependencies
<dependency>

 <groupId>org.apache.velocity</groupId>

 <artifactId>velocity</artifactId>

 <version>1.7</version>

</dependency>

<dependency>

 <groupId>org.apache.velocity</groupId>

 <artifactId>velocity-tools</artifactId>

 <version>2.0</version>

</dependency>

2. Context configuration 

 To enable the Velocity features, we need to define new VelocityConfigurer and VelocityViewResolver as shown bellow
<!-- This bean sets up the Velocity environment for us based on a root path 

  for templates. Optionally, a properties file can be specified for more control 

  over the Velocity environment, but the defaults are pretty sane for file 

  based template loading. -->

 <bean id="velocityConfig"

  class="org.springframework.web.servlet.view.velocity.VelocityConfigurer">

  <property name="resourceLoaderPath" value="/WEB-INF/views/" />

 </bean>



 <!-- View resolvers can also be configured with ResourceBundles or XML files. 

  If you need different view resolving based on Locale, you have to use the 

  resource bundle resolver. -->

 <bean id="viewResolver"

  class="org.springframework.web.servlet.view.velocity.VelocityViewResolver">

  <property name="cache" value="true" />

  <property name="prefix" value="" />

  <property name="suffix" value=".vm" />

  

  <!-- if you want to use the Spring Velocity macros, set this property to true -->

  <property name="exposeSpringMacroHelpers" value="true" />

 </bean>

3. Prepare a template file


<html>
    <body>
        <h1>Users:</h1> <br/>
        #foreach($user in $users)
         $user.name <br/>
        #end
    </body>
</html>

4. Finally, create a controller


package com.lapth82.springmvc.velocity.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

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

import com.lapth82.springmvc.velocity.model.User;

@Controller
public class HomeController {

 @RequestMapping(value="/")
 public ModelAndView test(HttpServletResponse response) throws IOException{
  
  User user1 = new User();
  user1.setName("lapth82");
  user1.setTitle("blogger");
  
  User user2 = new User();
  user2.setName("user2");
  user2.setTitle("blogger");
  
  List<User> lstUser = new ArrayList<User>();
  lstUser.add(user1);
  lstUser.add(user2);
  
  Map<String, List<User>> model = new HashMap<String, List<User>>();
  model.put("users", lstUser);
  
  return new ModelAndView("home", model);
 }
}


For the demonstration, please download Source code from my repository on Github

1 comment:

  1. The VelocityConfigurer does not have a property named resourceLoaderPath as shown in the example.

    Has this changed?

    ReplyDelete