带条件的字符串连接(Thymeleaf + SpringEL)

时间:2016-06-23 21:13:21

标签: spring spring-mvc thymeleaf

我的对象产品有type(string)和trade(boolean)。 如果交易成真,我想附加产品类型和“交易”。

代码正在运行:

th:classappend="${product.type}"

以及:

th:classappend="${product.trade}? trade"

但是我想要两者兼而有之,我都没有。

我想要的例子,但我没有找到如何。

th:classappend="${product.type} + ${product.trade}? trade"

2 个答案:

答案 0 :(得分:0)

你可以尝试这个并告诉我这是否适合你

th:classappend="${product.trade ? 'Trade'  +' '+ product.type : product.type}"

答案 1 :(得分:0)

<div th:if="${product.trade == true}" th:text="${product.type}"></div>

<div th:with="flag = ${product.trade == true}"></div>

<div th:if="${flag}" th:text="${product.type}></div>

<div th:unless="${flag}" text="flag is false"></div>

<div th:if="${product.trade == true}">
    <span th:text="${product.type}"> </span> trade
</div>