SpringBoot整合Thymeleaf模板引擎

1、pom.xml 引入 thymeleaf

<!--引入thymeleaf模块引擎-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

2、application.properties 配置

spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.cache=false
spring.thymeleaf.suffix=.html
spring.thymeleaf.mode=HTML

3、Controller

@GetMapping("/qrcode/get")
public String getCode(Model model) {
    R r = userService.get();
    model.addAttribute("data", r);
    return "index";
}

4、html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <!--th:text 将div里面的文本内容设置为 -->
    <h1>欢迎你,<span th:text="${data.code}"></span>!</h1>
</body>


<script type="text/javascript" th:inline="javascript">
    var data = [[${data}]];
    console.log(data, 11)
</script>
</html>

本文链接: https://jianz.xyz/index.php/archives/343/

1 + 8 =
快来做第一个评论的人吧~