thymeleaf(一) ___ helloword

2018-06-03  本文已影响0人  蜗牛船长
thymeleaf是什么

我们引用官网的一句介绍:
thymeleaf是⾯向Web和独⽴环境的现代服务器端Java模板引擎,能够处 理HTML,XML,JavaScript,CSS甚⾄纯⽂本。Thymeleaf旨在提供⼀个优雅的、⾼度可维护的创建模板的⽅式。为了实 现这⼀⽬标,Thymeleaf建⽴在⾃然模板的概念上,将其逻辑注⼊到模板 ⽂件中,不会影响模板设计原型。 这改善了设计的沟通,弥合了设计和 开发团队之间的差距。

简而言之就是一个类似freemarker一样的模板引擎,把我们后端的数据渲染到前端页面.thymeleaf的模板
模板完美的遵循html规范,即使直接打开文件也可以显示出来,不会改变前端或美工设计的html页面,同时是springboot官方推荐的模板引擎.

怎么使用

使用一个简单的springboot demo来演示基本的使用

<dependency>
          <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-starter-thymeleaf</artifactId>
  </dependency>
  spring:
    thymeleaf:
      servlet:
         content-type: text/html
      cache: false
      prefix: classpath:/templates/
      suffix: .html
      mode: HTML5
      encoding: UTF-8
@Controller
public class IndexController {


   @GetMapping("/index")
   public String index(){

     return "index/index";
   }
 }

不过一个普通的html文件怎么就能被模板解析呢,索引需要在html顶部加入声明

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>测试</title>
</head>
<body>
<p th:text="${hello}"></p>
</body>
</html>
上一篇 下一篇

猜你喜欢

热点阅读