SpringBoot中的Thymeleaf基本使用
初次书写博客,不足之处请多体谅
Thymeleaf官方文档中英文版本
链接:link
提取码:z6od
1、Thymeleaf介绍
- Thymeleaf是用来开发Web和独立环境项目的服务器端的Java模版引擎
- JSP也是模板引擎,不过SpringBoot官方推荐使用thymeleaf模板引擎
2、在SpringBoot中使用
引入Thymeleaf(IDEA)
1、创建引入thymeleaf的SpringBoot项目
项目结构:
2、创建成功后在pom.xml文件中会自动导入依赖
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-thymeleaf</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency>
3、可在源码中查看thymeleaf的一些配置
- SpringBoot会自动帮我们配置thymeleaf
@Configuration(proxyBeanMethods = false
)
@EnableConfigurationProperties({ThymeleafProperties.class})
@ConditionalOnClass({TemplateMode.class, SpringTemplateEngine.class})
@AutoConfigureAfter({WebMvcAutoConfiguration.class, WebFluxAutoConfiguration.class})
public class ThymeleafAutoConfiguration {public ThymeleafAutoConfiguration() {}
}
- 属性集文件中所配置的thymeleaf的一些相关信息
@ConfigurationProperties(prefix = "spring.thymeleaf"
)
public class ThymeleafProperties {//默认的读取存储thymeleaf渲染文件的地址private static final Charset DEFAULT_ENCODING;public static final String DEFAULT_PREFIX = "classpath:/templates/";public static final String DEFAULT_SUFFIX = ".html";private boolean checkTemplate = true;private boolean checkTemplateLocation = true;private String prefix = "classpath:/templates/";private String suffix = ".html";private String mode = "HTML";private Charset encoding;private boolean cache;private Integer templateResolverOrder;private String[] viewNames;private String[] excludedViewNames;private boolean enableSpringElCompiler;private boolean renderHiddenMarkersBeforeCheckboxes;private boolean enabled;private final ThymeleafProperties.Servlet servlet;private final ThymeleafProperties.Reactive reactive;
}
4、Controller层以及前台页面的功能实现展示
Controller层代码展示
package com.heihei.springboot.controller;import com.heihei.springboot.entity.Student;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;@Controller
public class MyController {@RequestMapping("index")public String index(){return "index";}@RequestMapping("/show")public String show(Model model){model.addAttribute("hello","Hello Springboot templates");model.addAttribute("test01","<p>解析还是不解析</p>");model.addAttribute("list", Arrays.asList("弟弟","嘿嘿","啦啦啦"));List<Student> students=new ArrayList<>();students.add(new Student("黑虎",5,"男"));students.add(new Student("阿福",7,"女"));model.addAttribute("student",students);model.addAttribute("stu",new Student("龙龙",21,"男孩子"));return "index";}@RequestMapping("/shows")public String shows(int id){System.out.println(id);return "index";}
}
html文件中代码:
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head><meta charset="UTF-8"><title>Title</title>
</head>
<body>welcome to thymleaf index page!<hr><h3 th:text="${hello}">多多多</h3><hr><h3 th:text="${test01}"></h3><hr><h3 th:utext="${test01}"></h3><!--使用thymeleaf可以替换我们原有的所有属性值--><input type="text" name="username" value="123123123" th:type="password" th:name="${hello}" th:value="${hello}"><!-- 超链接 --><br><br><a href="https://www.baidu.com" th:href="@{https://www.taobao.com}">百度一下</a><br><!--文本内容的填充 在标签中间使用--><p>[[${hello}]]</p><br><a th:href="@{/shows(id=1)}">请求资源参数传递</a><br><h3>List Iteration</h3><!--集合的遍历--><br><p th:each="li:${list}" th:text="${li}"></p><!--对象集合的遍历--><table border="1"><tr><th>Name</th><th>Age</th><th>Sex</th></tr><tr th:each="students:${student}"><td th:text="${students.name}"></td><td th:text="${students.age}"></td><td th:text="${students.sex}"></td></tr></table><br><!--* 和 $ 之间的区别--><div th:object="${stu}"><p>Name:<span th:text="*{name}"></span></p><p>Age:<span th:text="*{age}"></span></p><p>Sex:<span th:text="*{sex}"></span></p></div>
</body>
</html>
在使用thymeleaf时首先需要引入相关语法提示
<html xmlns:th="http://www.thymeleaf.org">
5、对应页面功能展示:
SpringBoot中的Thymeleaf基本使用相关推荐
- SpringBoot中使用thymeleaf的switch来实现if-else if -else的效果
场景 SpringBoot中使用thymeleaf的开关语句实现if-else if -else的效果. Switch使用示例: <div th:switch="${user.role ...
- 【SpringBoot】3、SpringBoot中整合Thymeleaf模板引擎
SpringBoot 为我们提供了 Thymeleaf 自动化配置解决方案,所以我们在 SpringBoot 中使用 Thymeleaf 非常方便 一.简介 Thymeleaf是一个流行的模板引擎,该 ...
- SpringBoot中使用thymeleaf时点击按钮触发事件失败
场景 在SpringBoot项目中使用thymeleaf模板时按钮的点击事件没法触发. 需要用到jquery的delegate()函数. delegate()函数用于为指定元素的一个或多个事件绑定事件 ...
- SpringBoot中使用thymeleaf时ajax请求不能回显消息
场景 在SpringBoot项目中使用thymeleaf模板时,在js文件中使用ajax提交表单 不能成功回显消息. 实现 修改为 html中: <button id="parseBt ...
- SpringBoot中访问Thymeleaf提示:元素类型 meta 必须由匹配的结束标记终止。
场景 新建SpringBoot项目后整合Thymelaf后访问页面提示: 元素类型 "meta" 必须由匹配的结束标记 "</meta>" 终止. ...
- SpringBoot中整合Thymeleaf
场景 springboot不建议使用jsp,使用模板引擎,比如thymeleaf,velocity,freemarker. 项目搭建专栏: https://blog.csdn.net/BADAO_LI ...
- thymeleaf html模块化,SpringBoot中使用Thymeleaf模板开发的后台管理框架
系统简介 JavaWeb专业版,企业级开发框架SpringBoot+Layui+Thymeleaf+MybatisPlus开发权限(RBAC)及内容管理框架,框架中集成了权限管理.模块管理,数据库管理 ...
- Idea开发springBoot 中使用 thymeleaf 模板$表达式报红波浪线解决方案
html页面设置前如下显示: 我的Idea版本是2021.2.4,解决过程如下: 1.在idea上方菜单栏中选择File --> Settings 2.在settinngs --> In ...
- SpringBoot中对thymeleaf的时间进行格式化显示
场景 对thymeleaf中时间进行格式化显示. 实现 <div class="form-group col-md-4 ml_10"><label>创建时间 ...
最新文章
- 超级挂载 实现过程-代码
- 【转载】插件自动升级
- 小程序访问本地服务器拒绝访问,文件夹拒绝访问的原因与解决办法
- restful java demo_java通过servlet实现restful接口示例
- 【软件工程】用户在软件项目中承担的工作
- html实现动态折线图
- 谈现代企业中(一)菜鸟和大牛
- C语言 — 数据类型,基本整型所占字节数
- 【路径规划】基于matlab模糊控制机器人路径规划【含Matlab源码 1643期】
- java发送邮件的两种实现方式(包括如何伪造发件人及其原理)
- 小米8使用usb连接计算机,小米怎么连接电脑usb(小米8连接电脑看不到文件)
- CWDM SFP彩光模块知识百科
- 阿里天池工业蒸汽量预测baseline-数据探索篇
- 苹果手机软件闪退怎么解决_《NBA2K21》闪退怎么办 闪退异常问题解决方法
- 生物化学 药物设计与研发笔记:变构(Allostery)药物设计
- 失眠可以用什么东西改善一个失眠多年的朋友给我推荐
- 调制与变频、基带信号与射频信号中的IQ调制(又称矢量调制)
- RS485调试的几个要点
- 2022-2028年中国菌菇酱行业市场现状分析及发展前景展望报告
- 我的助理辞职了 -- 做一个有心的人
热门文章
- 【Nuxt3 ssr 服务器渲染 】
- 2017下半年软件设计师考试难度分析
- 葫芦侠图床带API网站源码 切勿非法用途
- 解决 OKHTTP 返回 Response 乱码问题
- 【 田静语法】—简单句的核心构成
- 妲己智能机器人开箱_王者荣耀妲己智能机器人18分钟售罄 网友:强烈建议出同款皮肤...
- 如何设置小程序页面各个部分的背景颜色?
- python 慕课网请编写一个fib函数_中国大学MOOC(慕课)_C程序设计_选修课测试答案...
- c语言测试数据的边界,黑盒测试-边界值(示例代码)
- 操作系统笔记(II:从进程同步到文件管理)