8.Spring Boot中使用thymeleaf
Spring Boot中使用thymeleaf
Spring Boot支持FreeMarker、Groovy、Thymeleaf和Mustache四种模板解析引擎,官方推荐使用Thymeleaf。
spring-boot-starter-thymeleaf
我们只需要在Spring Boot中使用Thymeleaf只需在pom中加入Thymeleaf的starter即可:
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
在Spring Boot 2.1.7.RELEASE版本中,默认的Thymeleaf版本为3.0.11 RELEASE版本。可以通过如下方式改变默认的Thymeleaf版本:
<properties><thymeleaf.version>3.0.10.RELEASE</thymeleaf.version><thymeleaf-layout-dialect.version>2.0.1</thymeleaf-layout-dialect.version>
</properties>
在Spring Boot中,默认的html页面地址为src/main/resources/templates,默认的静态资源地址为src/main/resources/static。
Thymeleaf默认配置
在Spring Boot配置文件中可对Thymeleaf的默认配置进行修改:
server.servlet.context-path=/web#开启模板缓存(默认值:true)
spring.thymeleaf.cache=true
#Check that the template exists before rendering it.spring.thymeleaf.check-template=true
#检查模板位置是否正确(默认值:true)
spring.thymeleaf.check-template-location=true
#Content-Type的值(默认值:text/html)
spring.thymeleaf.servlet.content-type=text/html
#开启MVC Thymeleaf视图解析(默认值:true)
spring.thymeleaf.enabled=true
#模板编码
spring.thymeleaf.encoding=UTF-8
#要被排除在解析之外的视图名称列表,用逗号分隔
spring.thymeleaf.excluded-view-names=
#要运用于模板之上的模板模式。另见StandardTemplate-ModeHandlers(默认值:HTML5)
spring.thymeleaf.mode=HTML5
#在构建URL时添加到视图名称前的前缀(默认值:classpath:/templates/)
spring.thymeleaf.prefix=classpath:/templates/
#在构建URL时添加到视图名称后的后缀(默认值:.html)
spring.thymeleaf.suffix=.html
#Thymeleaf模板解析器在解析器链中的顺序。默认情况下,它排第一位。顺序从1开始,只有在定义了额外的TemplateResolver Bean时才需要设置这个属性。
spring.thymeleaf.template-resolver-order=
#可解析的视图名称列表,用逗号分隔
#spring.thymeleaf.view-names=
一般开发中将spring.thymeleaf.cache设置为false,其他保持默认值即可。
简单示例
编写一个Controller:
@Controller
public class IndexController {@RequestMapping("/account")public String index(Model m) {List<Account> list = new ArrayList<Account>();list.add(new Account("KangKang", "苍井优", "e10adc3949ba59abbe56e", "超级管理员", "17777777777"));list.add(new Account("Mike", "麦克", "e10adc3949ba59abbe56e", "管理员", "13444444444"));list.add(new Account("Jane","简","e10adc3949ba59abbe56e","运维人员","18666666666"));list.add(new Account("Maria", "小泽玛利亚", "e10adc3949ba59abbe56e", "清算人员", "19999999999"));m.addAttribute("accountList",list);return "account";}
}
编写account.html页面:
public class Account {private String account;private String name;private String password;private String accountType;private String tel;public Account(String account, String name, String password, String accountType, String tel) {super();this.account = account;this.name = name;this.password = password;this.accountType = accountType;this.tel = tel;}public String getAccount() {return account;}public void setAccount(String account) {this.account = account;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}public String getAccountType() {return accountType;}public void setAccountType(String accountType) {this.accountType = accountType;}public String getTel() {return tel;}public void setTel(String tel) {this.tel = tel;}
}
最终项目目录如下所示:
启动项目,访问 http://localhost:8080/web/account:
source code
8.Spring Boot中使用thymeleaf相关推荐
- 在Spring Boot中配置Thymeleaf的模板路径
众所周知,Thymeleaf的模板文件默认是在项目文件夹的src\main\resources\templates目录下的.不过出于特殊需要,要修改其路径怎么办呢? 在我们的项目配置文件applica ...
- Spring boot 中使用 Thymeleaf
Thymeleaf 是一个java类库,他是一个xml/xhtml/html5的模板引擎,可使用在MVC的Web应用的View层. 创建springboot项目,并引入Thymeleaf: 下面附上 ...
- spring boot 学习(二)spring boot 框架整合 thymeleaf
spring boot 框架整合 thymeleaf spring boot 的官方文档中建议开发者使用模板引擎,避免使用 JSP.因为若一定要使用 JSP 将无法使用. 注意:本文主要参考学习了大神 ...
- Spring Boot中Thymeleaf的初步使用
目录 理论 演示 理论 使用TemplateEngine是Spring Boot中推荐的,他的作用是: 把模板(如html界面)和数据匹配好,然后输出,发给用户. 而不是传统的使用jsp进行操作 模版 ...
- 再谈Spring Boot中的乱码和编码问题
编码算不上一个大问题,即使你什么都不管,也有很大的可能你不会遇到任何问题,因为大部分框架都有默认的编码配置,有很多是UTF-8,那么遇到中文乱码的机会很低,所以很多人也忽视了. Spring系列产品大 ...
- Spring Boot + Security + MyBatis + Thymeleaf + Activiti 快速开发平台项目
项目介绍 Spring Boot + Security + MyBatis + Thymeleaf + Activiti 快速开发平台 基于 Layui 的后台管理系统模板,扩展 Layui 原生 U ...
- Spring Boot中Starter是什么
比如我们要在Spring Boot中引入Web MVC的支持时,我们通常会引入这个模块spring-boot-starter-web,而这个模块如果解压包出来会发现里面什么都没有,只定义了一些POM依 ...
- java中的controller_详解Spring Boot中Controller用法
Controller Controller是SpringBoot里最基本的组件,他的作用是把用户提交来的请求通过对URL的匹配,分配个不同的接收器,再进行处理,然后向用户返回结果.他的重点就在于如何从 ...
- twilio_15分钟内使用Twilio和Stormpath在Spring Boot中进行身份管理
twilio 建筑物身份管理,包括身份验证和授权? 尝试Stormpath! 我们的REST API和强大的Java SDK支持可以消除您的安全风险,并且可以在几分钟内实现. 注册 ,再也不会建立au ...
最新文章
- java后台的微信小程序支付的解决方案
- 北斗导航 | 读取ground truth data(python源代码)
- 据库中事务、会话、线程这几个概念是什么关系
- 【Python】Matplotlib绘制极坐标雷达图
- AsyncSocket
- 闭包的理解、缺点以及应用场景
- 您如何构造适合于numpy排序的数组?
- qt5.15.1及以上版本进行编译(windows64位)
- 数字全息干涉偏振相移实验经验总结
- 饿汉式单例模式,懒汉式单例模式
- Java 神级项目 yyds
- 计算机中文件无法删除,电脑文件无法删除怎么办?强制删除文件的方法
- 奥克兰计算机科学专业世界排名,2021年度QS世界大学学科排名发布!奥克兰理工大学特色专业盘点...
- 云计算与虚拟化有什么区别?
- windows10 freeswitch soundtouch 变声
- 【论文阅读】Structured Pruning Learns Compact and Accurate Models
- python语言用法_python语言基本语句用法总结
- IPHONE绑定武大邮箱的一些坑
- AI绘图工具——Midjourney使用笔记
- 计算机编程国际赛事,中小学生学习C++编程可以参加的赛事
热门文章
- 三国群英传霸业之王服务器维护,《三国群英传:霸王之业》1月7日维护更新公告...
- 项目管理之我见:甲方乙方
- Linux07_文件操作
- cocos2d-x 关于旋转和移动的一点小技巧
- 怎么判断冠词用a还是an_不定冠词a和an有哪些用法
- 轩辕谷,黄帝的诞生地
- 鱼的记忆[较为重要的知识点/技巧]
- python用matplotlib画球_Python 用matplotlib画以时间日期为x轴的图像
- 网络舆情信息查询方法有哪些?具体解决方法详解
- 大连海洋计算机专业校址,大连海洋大学有几个校区,哪个校区最好及各校区介绍...