SpringBoot+thymeleaf配置

由于近排项目的需要,就用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的用处不仅仅如此,还有很多方面暂时没有。以后会更加深入的研究。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值