Swiftorial Logo
Home
Swift Lessons
Tutorials
Learn More
Career
Resources

Spring MVC and Velocity

Apache Velocity is a Java-based template engine that provides a simple yet powerful template language to reference objects defined in Java code. This guide covers the key concepts and steps for integrating Velocity with Spring MVC, including setting up Velocity, creating Velocity templates, and using Velocity features in your Spring MVC application.

Key Concepts of Spring MVC and Velocity

  • Velocity Template Engine: A template engine that processes HTML, XML, JavaScript, CSS, and text files.
  • VelocityViewResolver: A Spring MVC ViewResolver implementation that uses Velocity templates.
  • Velocity Expressions: Special syntax used to process templates, such as variable expressions, loops, and conditionals.

Setting Up Velocity

Include the necessary dependencies for Velocity in your pom.xml file:

<dependencies>
    <!-- Spring Boot Starter Velocity -->
    <dependency>
        <groupId>org.apache.velocity.tools</groupId>
        <artifactId>velocity-tools-generic</artifactId>
        <version>3.0</version>
    </dependency>

    <!-- Spring Boot Starter Web -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
</dependencies>

Configuring Velocity

Configure Velocity in your Spring MVC application by defining a VelocityConfigurer bean:

WebConfig.java

// WebConfig.java
package com.example.springmvc.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.view.velocity.VelocityConfigurer;
import org.springframework.web.servlet.view.velocity.VelocityViewResolver;

@Configuration
public class WebConfig {

    @Bean
    public VelocityConfigurer velocityConfigurer() {
        VelocityConfigurer configurer = new VelocityConfigurer();
        configurer.setResourceLoaderPath("/WEB-INF/views/");
        return configurer;
    }

    @Bean
    public VelocityViewResolver velocityViewResolver() {
        VelocityViewResolver resolver = new VelocityViewResolver();
        resolver.setCache(true);
        resolver.setPrefix("");
        resolver.setSuffix(".vm");
        return resolver;
    }
}

Creating Velocity Templates

Create Velocity templates in the src/main/webapp/WEB-INF/views directory:

home.vm

#set($message = "Welcome to Spring MVC with Velocity!")
<!DOCTYPE html>
<html>
<head>
    <title>Home</title>
</head>
<body>
    <h1>Home</h1>
    <p>$message</p>
</body>
</html>

Creating the Controller

Create a controller to handle requests and use Velocity templates:

HomeController.java

// HomeController.java
package com.example.springmvc.controllers;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

@Controller
public class HomeController {

    @GetMapping("/home")
    public String home(Model model) {
        model.addAttribute("message", "Welcome to Spring MVC with Velocity!");
        return "home";
    }
}

Using Velocity Features

Velocity provides various features for template processing, such as variable expressions, loops, and conditionals:

Variable Expressions

<p>$message</p>

Loops

#foreach($item in $items)
    <p>$item</p>
#end

Conditionals

#if($condition)
    <p>Condition is true</p>
#end

Key Points

  • Velocity Template Engine: A template engine that processes HTML, XML, JavaScript, CSS, and text files.
  • VelocityViewResolver: A Spring MVC ViewResolver implementation that uses Velocity templates.
  • Velocity Expressions: Special syntax used to process templates, such as variable expressions, loops, and conditionals.
  • Include the necessary dependencies for Velocity in your pom.xml file.
  • Configure Velocity in your Spring MVC application by defining a VelocityConfigurer bean.
  • Create Velocity templates in the src/main/webapp/WEB-INF/views directory.
  • Create a controller to handle requests and use Velocity templates.
  • Use Velocity features such as variable expressions, loops, and conditionals to process templates.

Conclusion

Integrating Velocity with Spring MVC allows you to create dynamic and interactive web applications. By setting up Velocity, creating Velocity templates, and using Velocity features, developers can enhance their Spring MVC applications and provide a rich user experience. Happy coding!