百里香变化变量 [英] Thymeleaf change variable

查看:38
本文介绍了百里香变化变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Thymeleaf 的新手,我需要做这样的事情:

<div th:each="element2 : ${list2}"><div th:if="element2.name == 'someName'"><div th:with="test=true">测试已更改</div>

<div th:text="${test}"></div>

如果我尝试此代码,我会看到测试已更改",但我的变量测试始终为假

解决方案

A with expression 创建或覆盖 局部变量.

这意味着修改后的变量只能在你声明了 with 表达式的元素内部访问.

在您的情况下,测试的输出写在修改 div 之外,因此您可以从外部获得结果.

I am new in Thymeleaf and i need to do something like this:

<div th:each="element : ${list}" th:with="test=false">
    <div th:each="element2 : ${list2}">
        <div th:if="element2.name == 'someName'">
            <div th:with="test=true">test changed</div>
        </div>
    </div>
    <div th:text="${test}"></div>
</div>

If i try this code i see "test changed" but my variable test is always false

解决方案

A with expression creates or overrides a local variable.

That means that the modified variable is only accessible inside the element you declared the with expression on.

In your case the output of test is written outside of the modification div so you get the result from outside.

这篇关于百里香变化变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
Java开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆