spring boot 学习(二)spring boot 框架整合 thymeleaf
spring boot 框架整合 thymeleaf
spring boot 的官方文档中建议开发者使用模板引擎,避免使用 JSP。因为若一定要使用 JSP 将无法使用。
注意:本文主要参考学习了大神程序员DD的博客。
附上,相应链接:http://blog.didispace.com/springbootweb/
关于 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配置属性系列
spring boot 学习(二)spring boot 框架整合 thymeleaf的更多相关文章
- Spring Boot 学习摘要--关于日志框架
date: 2020-01-05 16:20:00 updated: 2020-01-08 15:50:00 Spring Boot 学习摘要--关于日志框架 学习教程来自:B站 尚硅谷 1. 关于日 ...
- Spring Boot (二):模版引擎 Thymeleaf 渲染 Web 页面
Spring Boot (二):模版引擎 Thymeleaf 渲染 Web 页面 在<Spring Boot(一):快速开始>中介绍了如何使用 Spring Boot 构建一个工程,并且提 ...
- Spring Cloud 学习 之 Spring Cloud Eureka(源码分析)
Spring Cloud 学习 之 Spring Cloud Eureka(源码分析) Spring Boot版本:2.1.4.RELEASE Spring Cloud版本:Greenwich.SR1 ...
- Spring Cloud 学习 之 Spring Cloud Eureka(搭建)
Spring Boot版本:2.1.4.RELEASE Spring Cloud版本:Greenwich.SR1 文章目录 搭建服务注册中心: 注册服务提供者: 高可用注册中心: 搭建服务注册中心: ...
- Spring Cloud学习笔记--Spring Boot初次搭建
1. Spring Boot简介 初次接触Spring的时候,我感觉这是一个很难接触的框架,因为其庞杂的配置文件,我最不喜欢的就是xml文件,这种文件的可读性很不好.所以很久以来我的Spring学习都 ...
- Spring Boot学习笔记——Spring Boot与MyBatis的集成(项目示例)
1.准备数据库环境 # 创建数据库 CREATE DATABASE IF NOT EXISTS zifeiydb DEFAULT CHARSET utf8 COLLATE utf8_general_c ...
- Spring Boot系列二 Spring @Async异步线程池用法总结
1. TaskExecutor Spring异步线程池的接口类,其实质是java.util.concurrent.Executor Spring 已经实现的异常线程池: 1. SimpleAsyncT ...
- Spring Cloud 学习 之 Spring Cloud Ribbon(基础知识铺垫)
文章目录 1.负载均衡: 2.RestTemplate详解: xxxForEntity/xxxForObject:主要介绍get跟post exchange: execute源码分析: 1.负载均衡: ...
- Spring Cloud 学习 (九) Spring Security, OAuth2
Spring Security Spring Security 是 Spring Resource 社区的一个安全组件.在安全方面,有两个主要的领域,一是"认证",即你是谁:二是& ...
随机推荐
- html判断当前页面是否在iframe中/顶级document中
在使用div+iframe布局的应用中,通常我们希望在session超时或者未登录访问时跳转到登录页面,默认情况下iframe中的页面无法直接覆盖父页面,因此需要在登录页面加载的时候判断一下当前是否为 ...
- 利用.bat(批处理)来删除KEIL编译生成的无用文件
新建一个.txt文件. 在里面输入如下内容: del *.bak /s del *.ddk /s del *.edk /s del *.lst /s del *.lnp /s del *.mpf /s ...
- VS编译器之间相互打开的技巧
例如:VS2010的工程在VS2012上打开,在工程属性里面 选择“常规” --> "平台工具集中" 选择 正在打开版本的型号.
- 【前端】javascript+jQuery实现360开机时间显示效果
实现效果: 实现原理: 给关闭按钮绑定点击事件,点击以后触发动画效果.利用jQuery的animate方法,先让显示天气的盒子高度变为0,接着让整个包含天气和事件的盒子宽度变为0,最后通过将displ ...
- VS2017无法启动程序 操作在当前状态中是非法的
工具--选项--调试--常规--启用asp.net的JavaScript调试(chrome和ie)去掉勾选
- 【附7】turbine
一.作用 聚集同一个微服务的相同的commandKey.Threadpool.commandGroupKey数据进行聚合 二.配置 1.集群(cluster)(turbine聚集数据的粒度) turb ...
- hdu 6444 Neko's loop 单调队列优化DP
Neko's loop Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total ...
- [bzoj 1774][Usaco2009 Dec]Toll 过路费
题目描述 跟所有人一样,农夫约翰以着宁教我负天下牛,休叫天下牛负我的伟大精神,日日夜夜苦思生 财之道.为了发财,他设置了一系列的规章制度,使得任何一只奶牛在农场中的道路行走,都 要向农夫约翰上交过路费 ...
- [BZOJ1122][POI2008]账本BBB 单调队列+后缀和
Description 一个长度为n的记账单,+表示存¥1,-表示取¥1.现在发现记账单有问题.一开始本来已经存了¥p,并且知道最后账户上还有¥q.你要把记账单修改正确,使得 1:账户永远不会出现负数 ...
- Unity3D学习笔记(二十):Rect、Canvas、Toggle、Slider、ScrollBar
Rect Transform(锚点):图片中心的四个点,界面以雪花形式显示 当四个点在一起的时候组成锚点,当四个点分开的时候组成锚框(合则锚点,分则锚框) Anchors: ----Min x:控 ...