spring boot 最简单的 Thymeleaf demo

一、引入相关jar包

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

二、配置 application.yml 文件

三、 在资源目录下 新建 templates 资源目录,在该目录下新建index.html

 

四:新建Controller

@Controller
public class ThymeleafController {
    @RequestMapping("/thymeleaf")
    public String index(ModelMap map) {
        // 加入一个属性,用来在模板中读取
        map.addAttribute("baidu", "http://www.baidu.com");
        // return模板文件的名称,对应src/main/resources/templates/index.html
        return "index";
    }
}

五、编写 index.html

<!DOCTYPE html>
<html xmlns:th="http://www.w3.org/1999/xhtml">
<head lang="en">
    <meta charset="UTF-8" />
    <title></title>
</head>
<body>
<h1>Hello World</h1>
<h1 th:text="${baidu}" >Hello World</h1>
<a th:href="${baidu}">百度一下 你就知道</a>
</body>
</html>

六、浏览器输入 http://localhost:8080/thymeleaf

说明:org.springframework.boot.autoconfigure.thymeleaf.ThymeleafProperties这个类里面有默认的配置。 
spring-boot很多配置都有默认配置: 
比如默认页面映射路径为   classpath:/templates/*.html 
静态文件路径为   classpath:/static/

下一章 更新如何页面跳转,如何curd

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值