Thymeleaf:有关于thyme leaf模板的介绍
在我们的日常开发中,总是要显示数据到页面,本人以前做的老项目的基本上都是Jsp,用Jsp的话,
后台和前端的交互代码繁琐,容易搞混,本人推荐thymeleaf模板进行前后端的分离,显示数据到页面
下面为大家写一个案例,让大家更好的认识thymeleaf,希望对初学者有所帮助。
1.首先我们看看application-dev.properties的配置
thymeleaf的模板配置
# thymeleaf模板配置
spring.thymeleaf.prefix=
classpath:/templates/
spring.thymeleaf.suffix=
.html
spring.thymeleaf.mode=
HTML5
spring.thymeleaf.encoding=
UTF-8
spring.thymeleaf.cache=
false
spring.resources.chain.strategy.content.enabled=
true
spring.resources.chain.strategy.content.paths=
/**
配置数据的thyme leaf模板,后缀名,html5的格式,字符编码格式
2.我写一个thymeleaf的controller案例给大家看看
//获取school的值
@RequestMapping("/school")
public ModelAndView school(){
ModelAndView mv=new ModelAndView();
mv.addObject("school",schoolService.findAll());
mv.setViewName("/school");
return mv;
}
以这个显示school里面的数据为一个案例
3.我们看看这个school.html的显示数据的html5的代码
<!DOCTYPE
HTML> <
html
xmlns=
"http://www.w3.org/1999/xhtml"
xmlns:
th
=
"http://www.thymeleaf.org"> <
head> <
title>Hello,thymeleaf!</
title> <
meta
http-equiv=
"Content-Type"
content=
"text/html; charset=UTF-8"/> </
head> <
body> <
p
th
:text=
" ${school}"></
p><
br/> </
body> </
html>
在这里我就简单的让大家更明白一点
4.输入路径进行访问页面,展示数据到页面
成功了,thymeleaf模板就是这么简单
关注本人微博:李日兴LRX
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!