1.Thymeleaf简介
Thymeleaf是个XML/XHTML/HTML5模板引擎,可以用于Web与非Web应用
Thymeleaf的主要目标在于提供一种可被浏览器正确显示的、格式良好的模板创建方式,因此也可以用作静态建模,Thymeleaf的可扩展性也非常棒。你可以使用它定义自己的模板属性集合,这样就可以计算自定义表达式并使用自定义逻辑,Thymeleaf还可以作为模板引擎框架。
2.引入Thymeleaf
引入依赖
在maven(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>配置Thymeleaf
在application.yml配置Thymeleaf
server: port: 8000spring: thymeleaf: cache: false # 关闭页面缓存 encoding: UTF-8 # 模板编码 prefix: classpath:/templates/ # 页面映射路径 suffix: .html # 试图后的后缀 mode: HTML5 # 模板模式# 其他具体配置可参考org.springframework.boot.autoconfigure.thymeleaf.ThymeleafProperties# 上面的配置实际上就是注入该类的属性值demo示例
创建IndexController
@Controllerpublic class IndexController { // 返回视图页面 @RequestMapping("index") public String index(){ return "index"; }}创建index.html
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>Title</title></head><body> Hello Thymeleaf!</body></html>创建TestController
@RestControllerpublic class TestController { // 返回整个页面 @RequestMapping("/test") public ModelAndView test(){ return new ModelAndView("test"); }}创建test.html
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>Title</title></head><body>Hello Thymeleaf! </br>By: ModelAndView</body></html>3.测试结果
4.Thymeleaf基础语法及使用
1.引入标签
html标签里引入xmlns:th="http:///ishuibo/SpringAll
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。