Showing posts with label Template Engine. Show all posts
Showing posts with label Template Engine. Show all posts

Friday, February 15, 2013

Multi View Resolver in Spring MVC

Sometime we need to have more than one view resolver in Spring MVC.

For example: We need benefit from JSP and also from template page

To resolve this problem, it's very simple in Spring MVC

Add these lines to your *-servlet.xml file:

<!-- Velocity template resolver -->
<bean 
 class="org.springframework.web.servlet.view.velocity.VelocityViewResolver">
 <property name="cache" value="true" />
 <property name="prefix" value="" />
 <property name="suffix" value=".vm" />
 <property name="order" value="1" />

 <!-- if you want to use the Spring Velocity macros, set this property to true -->
 <property name="exposeSpringMacroHelpers" value="true" />
</bean>
Note

Line 7: Tell to Spring that please use this view resolver first
<!-- JSP resolver -->
<bean id="viewResolver"
 class="org.springframework.web.servlet.view.InternalResourceViewResolver">
 <property name="prefix" value="/WEB-INF/views/" />
 <property name="suffix" value=".jsp" />
 <property name="order" value="2" />
</bean>
Note

Line 6: Tell to Spring that please use this after VelocityViewResolver

That's all thing we need to configure.

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

Use this links to invoke the appropriate features
For VelocityViewResolver: http://localhost:8080/multiresolver/fromvm
For InternalResourceViewResolver: http://localhost:8080/multiresolver/fromjsp

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