介绍
Thymeleaf是面向Web和独立环境的现代服务器端Java模板引擎,能够处理HTML,XML,JavaScript,CSS甚至纯文本。
Thymeleaf的主要目标是提供一个优雅和高度可维护的创建模板的方式。为了实现这一点,它建立在自然模板的概念上,将其逻辑注入到模板文件中,不会影响模板被用作设计原型。这改善了设计的沟通,弥合了设计和开发团队之间的差距。
Thymeleaf也从一开始就设计了Web标准 - 特别是HTML5 - 允许您创建完全验证的模板,如果这是您需要的。
案例操作
引入pom依赖
<!--引入Thymeleaf可以查看依赖关系,发现spring-boot-starter-thymeleaf下面已经包括了spring-boot-starter-web,所以可以把spring-boot-starter-web的依赖去掉.-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!--引入Thymeleaf-->
添加helloWord.html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8"/>
<title>springboot-thymeleaf demo</title>
</head>
<body>
<p th:text="'hello1212, ' + ${name} + '!'" />
</body>
</html>
添加toPage.java 的controler类
package com.big.fly.controller;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import javax.servlet.http.HttpServletRequest;
/**
* Created by Administrator on 2018/4/23.
*/
@Controller
@Slf4j
public class ToPage {
/**
* IDEA搭建SpringBoot集成Thymeleaf
*
*/
@RequestMapping("/helloWord")
public String toHelloWord(HttpServletRequest request, @RequestParam(value = "name", required = false, defaultValue = "springboot-thymeleaf") String name) {
log.info("into url :",name);
request.setAttribute("name", name);
return "helloWord";
}
}
可以添加:@Slf4j注解
配置pom.xml就可以了
<!--Spring Boot下的lombok安装 https://www.cnblogs.com/weiapro/p/7633645.html-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<!--Spring Boot下的lombok安装-->
application.yml设置
server:
port: 8081
代码层次结构如图
运行结果如图