news 2026/4/23 6:48:28

简单的Java模板Thymeleaf使用案例

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
简单的Java模板Thymeleaf使用案例

一、背景

Thymeleaf模板是Java的一种动态页面模板框架。

作用:类似JSP页面。

二、项目使用Thymeleaf框架需要的依赖

需要attoparser、javassist、ognl、thymeleaf、unbescape、slf4j等依赖。

适合Maven引入:

<dependency> <groupId>org.thymeleaf</groupId> <artifactId>thymeleaf</artifactId> <version>3.1.3.RELEASE</version> </dependency>

普通的依赖下载链接:

https://github.com/thymeleaf/thymeleaf/releases/download/thymeleaf-3.1.0.RELEASE/thymeleaf-3.1.0.RELEASE-dist.zip

三、项目代码案例

(一)模板案例

在类路径下的 templates目录下有index.html文件模板,内容如下:

<!DOCTYPE html> <html lang="en" xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>主页</title> </head> <body> 名字:<p th:text="${name}"></p> 消息:<p th:text="${message}"></p> </body> </html>

如图:

(二)测试代码

需要创建模板发现器、模板引擎、上下文对象。

代码如下:

import org.thymeleaf.TemplateEngine; import org.thymeleaf.context.Context; import org.thymeleaf.templateresolver.ClassLoaderTemplateResolver; /** * @author * @version 1.0.0 * <p> * date: 2025/12/25 **/ public class Test { public static void main(String[] args) { // 1、定义类路径模版发现器 ClassLoaderTemplateResolver resolver = new ClassLoaderTemplateResolver(); // 设置模板文件的前缀路径 resolver.setPrefix("templates/"); // 设置模板文件的后缀名 resolver.setSuffix(".html"); // 设置模板模式为HTML resolver.setTemplateMode("HTML"); // 设置字符编码 resolver.setCharacterEncoding("UTF-8"); // 禁用模版缓冲 resolver.setCacheable(false); // 2、定义模版引擎 TemplateEngine engine = new TemplateEngine(); // 设置模板发现器 engine.setTemplateResolver(resolver); // 3、定义上下文路径 Context context = new Context(); context.setVariable("name", "小明"); context.setVariable("message", "执行成功"); // 4、处理模板,返回填充后的结果 String str = engine.process("index", context); // 5、打印结果 System.out.println("结果如下:"); System.out.println(str); } }

说明:前缀是"templates/",后缀是".html",模版名是"index",最终拼接的结果:"templates/index.html"。

执行结果如下:

四、其他说明

1、其他的模板发现器

ServletContextTemplateResolver 适合Servlet的模版发现

UrlTemplateResolver 适合网络URL的模板发现

FileTemplateResolver 适合文件路径的模板发现

2、其他的处理方法

输出处理结果到某个Writer对象,例如:"OutputStreamWriter类型对象"、"HttpServletResponse对象的getWriter()返回PrintWriter对象"。

void process(String template, IContext context, Writer writer)

3、其他的上下文对象

适合JavaWeb的上下文对象类:

WebContext(HttpServletRequest request, HttpServletResponse response, ServletContext servletContext)

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/4/10 22:20:56

IPXWrapper终极指南:让经典游戏在Windows 11重获联机对战能力

IPXWrapper终极指南&#xff1a;让经典游戏在Windows 11重获联机对战能力 【免费下载链接】ipxwrapper 项目地址: https://gitcode.com/gh_mirrors/ip/ipxwrapper IPXWrapper是一个专为解决Windows系统IPX/SPX协议兼容性问题的开源工具。在现代Windows 10/11系统上&…

作者头像 李华
网站建设 2026/4/20 9:01:02

宝可梦数据管理新纪元:5步掌握AutoLegalityMod高效操作

宝可梦数据管理新纪元&#xff1a;5步掌握AutoLegalityMod高效操作 【免费下载链接】PKHeX-Plugins Plugins for PKHeX 项目地址: https://gitcode.com/gh_mirrors/pk/PKHeX-Plugins 还在为宝可梦个体值优化而反复调试&#xff1f;想要快速组建完美对战队伍却不知从何入…

作者头像 李华
网站建设 2026/4/23 6:33:32

5步构建企业级微信机器人:从零到生产部署完整指南

5步构建企业级微信机器人&#xff1a;从零到生产部署完整指南 【免费下载链接】java-wechaty Java Wechaty is a Conversational SDK for Chatbot Makers Written in Kotlin 项目地址: https://gitcode.com/gh_mirrors/ja/java-wechaty 在当今数字化转型浪潮中&#xff…

作者头像 李华
网站建设 2026/4/22 2:25:26

Sketch文本查找替换插件终极指南:3倍效率提升的秘诀

Sketch文本查找替换插件终极指南&#xff1a;3倍效率提升的秘诀 【免费下载链接】Sketch-Find-And-Replace Sketch plugin to do a find and replace on text within layers 项目地址: https://gitcode.com/gh_mirrors/sk/Sketch-Find-And-Replace 你是否曾经在设计过程…

作者头像 李华
网站建设 2026/4/19 23:10:02

浏览器PPT革命:无需安装的专业演示文稿创作体验

浏览器PPT革命&#xff1a;无需安装的专业演示文稿创作体验 【免费下载链接】PPTist 基于 Vue3.x TypeScript 的在线演示文稿&#xff08;幻灯片&#xff09;应用&#xff0c;还原了大部分 Office PowerPoint 常用功能&#xff0c;实现在线PPT的编辑、演示。支持导出PPT文件。…

作者头像 李华
网站建设 2026/4/22 19:47:49

PowerToys中文版终极指南:Windows效率神器一键配置全解析

PowerToys中文版终极指南&#xff1a;Windows效率神器一键配置全解析 【免费下载链接】PowerToys-CN PowerToys Simplified Chinese Translation 微软增强工具箱 自制汉化 项目地址: https://gitcode.com/gh_mirrors/po/PowerToys-CN 还在为复杂的英文界面而烦恼吗&…

作者头像 李华