springboot对Thymeleaf模板引擎的支持
Thymeleaf对比jsp最大的优势在于:
它是静态的html资源,不需要进行编译成servlet运行在tomcat服务器上,前端ui可以在其上直接进行美工
#指定模板所在的目录
spring.thymeleaf.classpath=/templates/
#检查模板路径是否存在
spring.thymeleaf.check-template-location=true
#是否缓存,开发模式下设置为false,避免改了模板还要重启服务器,线上设置为true,可以提高性能。
spring.thymeleaf.cache=false
#模板文件后缀名
spring.thymeleaf.suffix= .html
#编码格式
spring.thymeleaf.encoding=UTF-8
#content-typespring.thymeleaf.servlet.content-type=text/html#spring.thymeleaf.mode的默认值是HTML5,其实是一个很严格的检查,改为LEGACYHTML5可以得到一个可能更友好亲切的格式要求。#比如你在使用Vue.js这样的库,然后有<div v-cloak></div>这样的html代码,也被thymeleaf认为不符合要求而抛出错误。
spring.thymeleaf.mode = LEGACYHTML5
public class UserController {
/*** 采用model往request域中存值,存入2个普通的字符串\
* @param model
* @return
*/
@GetMapping("/userinfo1")
publicStringuserinfo1(Modelmodel) {
Stringusername="xiadewang";
Stringpassword="123456";
model.addAttribute("username", username);
model.addAttribute("password", password);return"userinfo1";
}
}
<!DOCTYPE html>
<htmllang="en"xmlns:th="http://www.thymeleaf.org">
<head>
<metacharset="UTF-8">
<title>Title</title>
</head>
<body>
<pth:text="'用户名:'+${username}"></p>
<pth:text="'密码:'+${password}"></p>
</body>
</html>
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· 什么是nginx的强缓存和协商缓存
· 一文读懂知识蒸馏
· Manus爆火,是硬核还是营销?