thymeleaf 内联表达式


thymeleaf 内联表达式

 

 

*******************

内联表达式

 

**************

文本内联

 

th:text ==> [[ .. ]]

<p>Hello, [[${session.user.name}]]!</p>
<p>Hello, <span th:text="${session.user.name}">Sebastian</span>!</p>

 

th:utext ==> [( .. )]

<p>The message is "[(${msg})]"</p>
<p>The message is <span th:utext="${msg}"></span> </p>

 

禁用内联

<p th:inline="none">A double array looks like this: [[1, 2, 3], [4, 5]]!</p>

 

 

**************

javascript 内联

 

<script th:inline="javascript">
    ...
    var username = [[${session.user.name}]];
    ...
</script>

 

 

**************

css 内联

 

<style th:inline="css">
    .[[${classname}]] {
      text-align: [[${align}]];
    }
</style>

 

 

*******************

示例

 

**************

controller 层

 

HelloController

@RestController
public class HelloController {

    @RequestMapping("/hello")
    public ModelAndView hello(ModelAndView mv){
        String info="hello 瓜田李下";
        String value="海贼王";

        String className="background-color";
        String color="green";

        mv.addObject("info",info);
        mv.addObject("value",value);
        mv.addObject("className",className);
        mv.addObject("color",color);

        mv.setViewName("index");
        return mv;
    }
}

 

**************

前端页面

 

index.html

 

<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml"
      xmlns:th="http://www.thymeleaf.org" xmlns:sec="https://www.thymeleaf.org/thymeleaf-extras-springsecurity4">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="/jquery/jquery-3.5.1.min.js"></script>
</head>
<script th:inline="javascript">
    $(function () {
        const value=[[${value}]];

        $("#name").val(value);
    })
</script>

<style th:inline="css">
    .color {
        [[${className}]]: [[${color}]];
    }
</style>
<body>
<div th:align="center" style="color: purple">
    <strong>
        使用内联:<span style="color: coral">[[${info}]]</span><br>
        禁用内联:<span style="color: coral" th:inline="none">[[${info}]]</span><br><br>
    </strong>

    name:<input type="text" id="name" name="value"><br><br>

    <span class="color">瓜田李下</span>
</div>
</body>
</html>

 

*******************

使用测试

 

                                  

 

 

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值