一、介紹
Java模板引擎是一個讓開發人員能夠方便快捷地生成文本的框架。它將預設的文本固定部分以及動態生成的數據部分混合起來,最終呈現出一個帶有數據的文本。Java模板引擎是基於模板開發的,其中包含:
- 寫死的文本
- 可以被動態替換的表達式
- 一些模板語言特定的符號,用於控制表達式的使用
二、使用場景
Java模板引擎在各種場景下都可以被應用:
- Web開發中,用於渲染HTML文檔
- 郵件模板中,用於填寫郵件的內容
- Word文檔中,用於控制文檔格式及內容的生成
三、主要的Java模板引擎
目前市面上主要的Java模板引擎有:
- FreeMarker: FreeMarker是一個強大、靈活的模板引擎,使用範圍廣泛,特別適用於處理較大的項目。它具有良好的性能,提供了完整的指令,包括條件語句,迭代,表達式等。
- Thymeleaf:Thymeleaf是一種現代的、服務器端的Java模板引擎,代碼更易讀,並且具有很好的擴展性和潛力。Thymeleaf支持HTML,XML,JavaScript,CSS甚至純文本。
- Velocity: Velocity是一種模板引擎,用於完全分離Web設計與程序開發。與其他Java模板引擎相比,它有一個更小的語法集和更少的語言操作,而且更容易上手,是為初學者而設的。
四、FreeMarker用法示例
1、添加依賴
<dependency> <groupId>org.freemarker</groupId> <artifactId>freemarker</artifactId> <version>2.3.31</version> </dependency>
2、編寫模板
以下是一個簡單的模板示例:
<html> <head> <title>User Information</title> </head> <body> <p>Username: ${user.username}</p> <p>Email: ${user.email}</p> </body> </html>
3、填充數據
在代碼中填充數據:
import freemarker.template.Configuration; import freemarker.template.Template; import freemarker.template.TemplateException; import java.io.File; import java.io.IOException; import java.io.PrintWriter; import java.util.HashMap; import java.util.Map; public class FreemarkerDemo { public static void main(String[] args) throws IOException, TemplateException { Configuration cfg = new Configuration(Configuration.VERSION_2_3_31); cfg.setDirectoryForTemplateLoading(new File("src/main/resources/templates")); cfg.setDefaultEncoding("UTF-8"); Template temp = cfg.getTemplate("user.ftl"); Map root = new HashMap(); User user = new User("John", "john@gmail.com"); root.put("user", user); PrintWriter out = new PrintWriter("output.html"); temp.process(root, out); } }
五、Thymeleaf用法示例
1、添加依賴
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>
2、編寫模板
以下是一個簡單的模板示例:
<html xmlns:th="http://www.thymeleaf.org"> <head> <title>User Information</title> </head> <body> <p>Username: <span th:text="${user.username}"></span></p> <p>Email: <span th:text="${user.email}"></span></p> </body> </html>
3、填充數據
在代碼中填充數據:
import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; @Controller @RequestMapping("/users") public class UserController { @GetMapping("/show") public String show(Model model) { User user = new User("John", "john@gmail.com"); model.addAttribute("user", user); return "user"; } }
六、Velocity用法示例
1、添加依賴
<dependency> <groupId>org.apache.velocity</groupId> <artifactId>velocity-engine-core</artifactId> <version>2.2.0</version> </dependency>
2、編寫模板
以下是一個簡單的模板示例:
<html> <head> <title>User Information</title> </head> <body> <p>Username: $user.username</p> <p>Email: $user.email</p> </body> </html>
3、填充數據
在代碼中填充數據:
import org.apache.velocity.Template; import org.apache.velocity.VelocityContext; import org.apache.velocity.app.VelocityEngine; import java.io.StringWriter; public class VelocityDemo { public static void main(String[] args) throws Exception { VelocityEngine ve = new VelocityEngine(); ve.init(); Template t = ve.getTemplate("user.vm"); VelocityContext ctx = new VelocityContext(); User user = new User("John", "john@gmail.com"); ctx.put("user", user); StringWriter writer = new StringWriter(); t.merge(ctx, writer); System.out.println(writer.toString()); } }
七、總結
Java模板引擎是一個功能強大的工具,可以幫助開發人員以一種高效的方式生成文本。在本文中,我們介紹了Java模板引擎的用法,並提供了主要的Java模板引擎示例。通過本文的學習,相信讀者已經掌握了Java模板引擎的核心概念,能夠根據實際項目的需求選擇最適合的模板引擎,提高開發效率。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/270560.html