27.在springboot中使用thymeleaf的属性inline(text, javascript 和 none)

说明
本项目是在

在springboot中使用thymeleaf循环

springboot文章23

修改而来

pom.xml文件,核心配置文件等文件,与文章23保持一致,本文只展示最新添加的文件,以减小文章冗余,更容易看出循环的使用方法。

知识点:
th:inline
th:inline 有三个取值类型 (text, javascript 和 none)
内联text
        可以让 Thymeleaf 表达式不依赖于 html 标签,直接使用 内敛表达式 [[ 表达式 ]] 即可获取动态数据,要求在父级标签上加 th:inline = “text” 属性
内联 javascript
可以在 js 中,获取模版中的数据。
1.index.html添加连接语句

2.ThymeleafController类添加方法
 //内联text
    @GetMapping("/inline")
    public String inLine(Model model){
        model.addAttribute("sex","m");
        model.addAttribute("age",20);
        model.addAttribute("name","张山");
        model.addAttribute("myuser",new SysUser(1005,"李华","男",20));
        return "inline";
    }

3.创建inline的html文件
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>inline</title>
    <script type="text/javascript" th:inline="javascript">
        var myname = [[${name}]];
        var myage = [[${age}]];
        function fun() {
            alert("获取的模板中的数据"+myname+","+myage);
        }
    </script>
</head>
<body>
<div style="margin-left: 300px">
    <h3>内联 text,使用内联表达式显示变量的值</h3>
    <div th:inline="text">
        <p>我是[[${name}]],年龄是[[${age}]]</p>
        <h4>如果使用原来的标准表达式展现众多变量,会导致代码非常冗余</h4>
        例如:我是<span th:text="${name}"></span>,年龄是<span th:text="${age}"></span>
        <br>
        <input type="button" name="btn" value="点击获取模板数据" onclick="fun()">
    </div>
</div>
</body>
</html>

4.测试

text测试:

 

javascript测试:

点击 javascript测试

  • 4
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

做一道光

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值