首页 技术 正文
技术 2022年11月7日
0 收藏 503 点赞 377 浏览 1691 个字

spring boot 框架整合 thymeleaf

spring boot 的官方文档中建议开发者使用模板引擎,避免使用 JSP。因为若一定要使用 JSP 将无法使用。

注意:本文主要参考学习了大神程序员DD的博客。

附上,相应链接:http://blog.didispace.com/springbootweb/

关于 thymeleaf 模板引擎的介绍

thymeleaf 学习笔记

同时,模板引擎默认的模板配置路径为:src/main/resources/templates

Spring Boot中使用Thymeleaf

      引入依赖,并在默认的模板路径src/main/resources/templates下编写模板文件。

附上依赖:

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
<version>1.4.3.RELEASE</version>
</dependency>

直接在 Controller 中调用模板即可

@Controller
public class HelloController {
@RequestMapping("/")
public String index(ModelMap map) {
// 加入一个属性,用来在模板中读取
map.addAttribute("host", "http://blog.didispace.com");
// return模板文件的名称,对应src/main/resources/templates/index.html
return "index";
}
}
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8" />
<title></title>
</head>
<body>
<h1 th:text="${host}">Hello World</h1>
</body>
</html>

直接打开html页面展现Hello World,但是启动程序后,访问http://localhost:8080/,则是展示Controller中host的值:http://blog.didispace.com,做到了不破坏HTML自身内容的数据逻辑分离。

Thymeleaf的默认参数配置

如有需要修改默认配置的时候,只需复制下面要修改的属性到application.properties中,并修改成需要的值,如修改模板文件的扩展名,修改默认的模板路径等。

# 是否开启模板缓存,默认true
spring.thymeleaf.cache=true
# 检查模板位置是否存在
spring.thymeleaf.check-template-location=true
# Content-Type value
spring.thymeleaf.content-type=text/html
# 是否启用MVC-Thymeleaf视图
spring.thymeleaf.enabled=true
# 模板编码
spring.thymeleaf.encoding=UTF-8
# 应该从解析中排除的视图名称列表(用逗号分隔)
spring.thymeleaf.excluded-view-names=
# 要应用于模板的模板模式。另请参见StandardTemplateModeHandlers。
spring.thymeleaf.mode=HTML5
# 在链接网址时预先查看名称的前缀。
spring.thymeleaf.prefix=classpath:/templates/
# 链接网址时附加到视图名称的后缀。
spring.thymeleaf.suffix=.html
# 指定模板的解析顺序,默认为第一个.
spring.thymeleaf.template-resolver-order=
# 指定使用模板的视图名,多个以逗号分隔
spring.thymeleaf.view-names=

SpringBoot配置属性系列

SpringBoot配置属性

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,000
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,512
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,358
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,141
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,771
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,849