SpringBoot集成Thymeleaf(六)
SpringBoot集成Thymeleaf从入门到精通(全)
SpringBoot集成Thymeleaf
本篇文章的学习
在这之前可以补充springboot的基础
详情可看我之前的文章
springboot从入门到精通(全)
Thymeleaf
- Thymeleaf 是一个流行的模板引擎,该模板引擎采用 Java 语言开发
- Thymeleaf 是另外的一种模板技术,它本身并不属于 Spring Boot,Spring Boot只是很好地集成这种模板技术,作为前端页面的数据展示
在创建模板的时候还需要多选择一个这个
之后默认会自动添加这些依赖

Springboot 使 用 thymeleaf 作 为 视 图 展 示 , 约 定 将 模 板 文 件 放 置在src/main/resource/templates 目录下,静态资源放置在 src/main/resource/static 目录下
创建一个控制层面
@Controller
public class UserController {
@RequestMapping(value = "/message")
public String message(Model model) {
model.addAttribute("data","SpringBoot集成Thymeleaf模版引擎");
return "message";
}
页面这个要配置在特定的目录下
还要加上名称空间才能用她的函数xmlns:th="http://www.thymeleaf.org"
具体使用名称空间的text后的数据,如果有数据传入就会将其替代,如果没有数据才会显示后面的内容
具体底层是html,通过这个名称空间,以及他的函数才可以进行加载识别参数
xmlns:th=“http://www.thymeleaf.org”
xmlns -> 命名空间
命名空间后面的地址是一个约束文件,约束你使用thymeleaf表达式
的一个规则文件,就好比我们之前在xml文件中的一此dtd文件
DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Titletitle>
head>
<body>
<h2 th:text="${data}">展示要显示的内容0h2>
body>
html>
配置后启动
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
也可以配置一个视图解析器
默认是有视图解析器的
所谓的视图解析器,下面才是真实跳转的路径
#设置thymeleaf模版引擎的前/后缀,(可选项)
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
为什么使用了th前缀就可以获取后台数据?
那是因为项目中添加了thymeleaf的核心依赖,它的核心依赖会去
解析thymeleaf自己定义的这些标签名称,通过thymeleaf自己的java核心代码来获取我们的后台数据
html 不会认识${}语法。
请求的流程是,发送请求给服务器,服务器接收请求后,处理请求,跳转到指定的静态 html 页面,在服务器端,Thymeleaf 模板引擎会按照它的语法,对动态数据进行处理,
所以如果要是 th 开头,模板引擎能够识别,会在服务器端进行处理,获取数据;如果没有以 th 开头,那么 Thymeleaf 模板引擎不会处理,直接返回给客户端了
字符串拼接
这个拼接类似java代码
在html代码中的展示
<span th:text="'共'+${totalRows}+'条'+${totalPage}+'页,当前第'+${currentPage}+'页,首页 上一页 下一页 尾页'">span>
https://blog.csdn.net/weixin_47872288/article/details/121031656
posted @
2022-05-12 22:03
1scy
阅读(14)
评论(0)
编辑
刷新页面返回顶部
<script type="text/javascript">
var commentManager = new blogCommentManager();
commentManager.renderComments(0);
fixPostBody();
window.tocManager.displayDisableTocTips = false;
window.tocManager.generateToc();
setTimeout(function() { incrementViewCount(cb_entryId); }, 50); deliverT2();
deliverC1C2();
loadNewsAndKb();
LoadPostCategoriesTags(cb_blogId, cb_entryId); LoadPostInfoBlock(cb_blogId, cb_entryId, cb_blogApp, cb_blogUserGuid);
GetPrevNextPost(cb_entryId, cb_blogId, cb_entryCreatedDate, cb_postType);
loadOptUnderPost();
GetHistoryToday(cb_blogId, cb_blogApp, cb_entryCreatedDate);
</script>
<script>loadBlogNews();</script>
<script>loadBlogDefaultCalendar();</script>
<script>loadBlogSideColumn();</script>
Copyright ? 2022 1scy
Powered by .NET 6 on Kubernetes <script src="https://blog-static.cnblogs.com/files/Return-blog/xue.js"></script> <script src="https://blog-static.cnblogs.com/files/yjlblog/cursor-effects.js"></script> <script type="text/javascript"> /* 鼠标特效 */ var a_idx = 0; jQuery(document).ready(function($) { $("body").click(function(e) { var a = new Array("?富强?","?民主?","?文明?","?和谐?","?自由?","?平等?","?公正?","?法治?","?爱国?","?敬业?","?诚信?","?友善?"); var $i = $("").text(a[a_idx]); a_idx = (a_idx + 1) % a.length; var x = e.pageX, y = e.pageY; $i.css({ "z-index": 999999999999999999999999999999999999999999999999999999999999999999999, "top": y - 20, "left": x, "position": "absolute", "font-weight": "bold", "color": "rgb("+~~(255*Math.random())+","+~~(255*Math.random())+","+~~(255*Math.random())+")" }); $("body").append($i); $i.animate({ "top": y - 180, "opacity": 0 }, 1500, function() { $i.remove(); }); }); }); </script> <script async="" src="https://www.googletagmanager.com/gtag/js?id=UA-476124-1"></script> <script> window.dataLayer = window.dataLayer || []; function gtag(){dataLayer.push(arguments);} gtag('js', new Date()); var kv = getGACustom(); if (kv) { gtag('set', kv); } gtag('config', 'UA-476124-1'); </script> <script defer="" src="https://hm.baidu.com/hm.js?866c9be12d4a814454792b1fd0fed295"></script>