Thymeleaf用法2

官方文档:
https://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.html#standard-expression-syntax

使用时需要为html添加属性:

<html xmlns:th="http://www.thymeleaf.org">

传输消息,(变量表达式)

controller层

@Controller
public class indexController {
    @RequestMapping("/a")
    public String test(Model model){
    //属性名:msg,属性值hello Thymeleaf
        model.addAttribute("msg","hello Thymeleaf");
        return "test";
    }
}

test.html页面

<!DOCTYPE html>
<html lang="en">
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<!-- th代表使用thyme leaf模板,${}变量表达式}-->
<div th:text="${msg}"></div>
</body>
</html>
<body>
<!-- msg:<h1>hello Thymeleaf</h1>-->
<div th:text="${msg}"></div>
<div th:utext="${msg}"></div>
</body>

结果:
在这里插入图片描述

遍历

<div th:each="fruit:${fruits}">[[${fruit}]]</div>
或者
<div th:each="fruit:${fruits}" th:text="${fruit}"></div>
@Controller
public class indexController {
    @RequestMapping("/a")
    public String test(Model model){
        model.addAttribute("msg","<h1>hello Thymeleaf</h1>");

        model.addAttribute("fruits", Arrays.asList("apple","orange","banana"));
        return "test";
    }

}

结果:
在这里插入图片描述

posted @   ︶ㄣ演戲ㄣ  阅读(3)  评论(0编辑  收藏  举报  
相关博文:
阅读排行:
· 百万级群聊的设计实践
· 永远不要相信用户的输入:从 SQL 注入攻防看输入验证的重要性
· 全网最简单!3分钟用满血DeepSeek R1开发一款AI智能客服,零代码轻松接入微信、公众号、小程
· .NET 10 首个预览版发布,跨平台开发与性能全面提升
· 《HelloGitHub》第 107 期
点击右上角即可分享
微信分享提示