springboot学习之thymeleaf

//将HTML页面放到classpath:/templates/下面面,thymeleaf即可渲染页面
@ConfigurationProperties(
    prefix = "spring.thymeleaf"
)
public class ThymeleafProperties {
    private static final Charset DEFAULT_ENCODING;
    public static final String DEFAULT_PREFIX = "classpath:/templates/";
    public static final String DEFAULT_SUFFIX = ".html";
    private boolean checkTemplate = true;
    private boolean checkTemplateLocation = true;
    private String prefix = "classpath:/templates/";
    private String suffix = ".html";

语法
1导入名称空间

<html xmlns:th="http://www.thymeleaf.org">//会有语法提示

bug来的莫名奇妙,抓狂。。。
spring boot需要在setting中修改本地仓库的位置

//使用restcontroller页面显示的是return的值
@RestController
public class TestController {
    //classpath:/templates/success
    @RequestMapping("/success")
    public String success(Map<String,Object> map){
        map.put("hello","你好");
        return "success";
    }
}

以下

@Controller
public class TestController {
    //classpath:/templates/success
    @RequestMapping("/success")
    public String success(Map<String,Object> map){
        map.put("hello","你好");
        return "success";
    }
}
//会取出hello的值,只显示数据内容,并不显示前端页面所写的内容
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>测试页面</title>
</head>
<body>
<p th:text="${hello}">测试一下</p>//会将p标签里面的内容覆盖掉
<p>测试一下</p>//成功显示
</body>
</html>

一开始使用controller注解并不能访问页面,后来莫名奇妙的好了。。。以及不知道为啥要关闭cache(貌似springboot的版本控制中thymeleaf已经更新到3.0以上了,不需要在pom文件的properties中修改版本)
语法规则
1)th:text;改变当前元素里面的文本内容
th: 任意html属性,来替换原生属性
官方文档中关于th的内容
2)表达式
参考官方文档:
Standard Expression Syntax

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值