由于近排项目的需要,就用SpringBoot进行开发。于是就涉及到thymeleaf的使用。这里就不作详细介绍,thymeleaf的介绍可参考这里:thymeleaf介绍

1.准备

创建一个SpringBoot项目,以下为目录结构:
在这里插入图片描述

2.配置

创建成功后在pom.xml中如下引入依赖:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

之后在springboot的配置文件(我这里命名为application.properites)中添加如下配置信息:

# templates文件夹的路径
spring.thymeleaf.prefix=classpath:/templates/
# templates中的所有文件后缀名,如/templates/main.html
spring.thymeleaf.suffix=.html

在templates文件夹中创建一个测试页面:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
Hello world
</body>
</html>

Controller页面:

@Controller
public class TestController {

    @RequestMapping(value = {"/indexPage"})
    public String indexData(){
        return "main";
    }
}

3.测试

在浏览器输入http://localhost:8080/indexPage,结果如下:
在这里插入图片描述
thymeleaf的用处不止如此,例如导入外部JS或CSS文件(注意的是这种js和css文件或文件夹必须在文件夹static中),可以这样导入:

<!-- JS导入 -->
<script th:src="@{/js/vue.js}"></script>
<!-- CSS导入 -->
<link th:href="@{/css/element-index.css}" rel="stylesheet" />

4.结尾

thymeleaf的用处不仅仅如此,还有很多方面暂时没有。以后会更加深入的研究。

Logo

助力广东及东莞地区开发者,代码托管、在线学习与竞赛、技术交流与分享、资源共享、职业发展,成为松山湖开发者首选的工作与学习平台

更多推荐