Spring Boot with Velocity
Apache Velocity is a Java-based template engine used to generate dynamic web pages. This guide covers the key concepts and steps for using Velocity with Spring Boot, including setting up dependencies, creating controllers, designing Velocity templates, and rendering views.
Key Concepts of Spring Boot with Velocity
- Template Engine: Velocity is a template engine for generating dynamic web content.
- Template Resolver: Velocity uses template resolvers to find and process templates.
- Context: A data container used for passing variables to templates.
- Directives and Variables: Velocity provides directives for control statements and variables for data substitution.
Setting Up Dependencies
Include the Spring Boot Starter Velocity dependency in your pom.xml
file:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-velocity</artifactId>
</dependency>
Creating a Controller
Create a controller to handle web requests and return Velocity templates:
Example: HomeController.java
// HomeController.java
package com.example.demo.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
@Controller
public class HomeController {
@GetMapping("/")
public String home(Model model) {
model.addAttribute("message", "Hello, Velocity with Spring Boot!");
return "index";
}
}
Designing Velocity Templates
Create Velocity templates to render HTML views. Place your templates in the src/main/resources/templates
directory:
Example: index.vm
#set($message = "Hello, Velocity with Spring Boot!")
<!DOCTYPE html>
<html>
<head>
<title>Spring Boot with Velocity</title>
</head>
<body>
<h1>Welcome to Spring Boot with Velocity</h1>
<p>Message: $message</p>
</body>
</html>
Rendering Views
When the controller method returns a view name, Spring Boot will resolve the view using Velocity and render the template:
// HomeController.java
package com.example.demo.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
@Controller
public class HomeController {
@GetMapping("/")
public String home(Model model) {
model.addAttribute("message", "Hello, Velocity with Spring Boot!");
return "index";
}
}
Using Velocity Directives and Variables
Velocity provides various directives for control statements and variables for data substitution:
- Variable Substitution:
$variable
- Set Directive:
#set($variable = value)
- If Directive:
#if(condition) ... #end
- ForEach Directive:
#foreach($item in $collection) ... #end
Example: Using Velocity Directives
#set($message = "Hello, Velocity with Spring Boot!")
<!DOCTYPE html>
<html>
<head>
<title>Velocity Directives</title>
</head>
<body>
<h1>Velocity Directives</h1>
<p>Message: $message</p>
#if($message)
<p>Message is not empty.</p>
#end
#set($items = ["Item1", "Item2", "Item3"])
<ul>
#foreach($item in $items)
<li>$item</li>
#end
</ul>
</body>
</html>
Handling Form Submissions
Handle form submissions using Velocity and Spring Boot:
Example: UserController.java
// UserController.java
package com.example.demo.controller;
import com.example.demo.model.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
@Controller
public class UserController {
@GetMapping("/user")
public String showForm(Model model) {
model.addAttribute("user", new User());
return "userForm";
}
@PostMapping("/user")
public String submitForm(User user, Model model) {
model.addAttribute("user", user);
return "userResult";
}
}
Example: userForm.vm
#set($user = {"name": "", "email": ""})
<!DOCTYPE html>
<html>
<head>
<title>User Form</title>
</head>
<body>
<h1>User Form</h1>
<form action="/user" method="post">
<label>Name: <input type="text" name="name" /></label><br/>
<label>Email: <input type="email" name="email" /></label><br/>
<button type="submit">Submit</button>
</form>
</body>
</html>
Example: userResult.vm
<!DOCTYPE html>
<html>
<head>
<title>User Result</title>
</head>
<body>
<h1>User Result</h1>
<p>Name: $user.name</p>
<p>Email: $user.email</p>
</body>
</html>
Key Points
- Template Engine: Velocity is a template engine for generating dynamic web content.
- Template Resolver: Velocity uses template resolvers to find and process templates.
- Context: A data container used for passing variables to templates.
- Directives and Variables: Velocity provides directives for control statements and variables for data substitution.
- Include the Spring Boot Starter Velocity dependency in your
pom.xml
file. - Create controllers to handle web requests and return Velocity templates.
- Design Velocity templates to render HTML views and place them in the
src/main/resources/templates
directory. - Use Velocity directives and variables for control statements and data substitution in templates.
- Handle form submissions using Velocity and Spring Boot.
Conclusion
Spring Boot and Velocity provide a powerful combination for building dynamic web applications. By understanding and using the capabilities of Velocity with Spring Boot, developers can create flexible and maintainable web applications with ease. Happy coding!