【问题标题】:String concat with conditional (Thymeleaf + SpringEL)带条件的字符串连接(Thymeleaf + SpringEL)
【发布时间】:2016-06-27 17:54:45
【问题描述】:

我有类型(字符串)和贸易(布尔)的对象产品。 如果交易为真,我想附加产品类型和“交易”。

代码正在运行:

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

还有:

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

但我想同时连接两者,但我失败了。

我想要的示例,但我没有找到方法。

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

【问题讨论】:

    标签: spring spring-mvc thymeleaf


    【解决方案1】:

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

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

    【讨论】:

    • 太棒了!它进行了调整: ${product.trade ? '贸易' +' '+ product.type : product.type}。你能解释一下第二部分吗? "product.type : product.type" 我不懂这个sintax,为什么我们不能直接使用"product.type"?非常感谢,太糟糕了,我没有足够的声誉来投票,希望有人这样做!
    • 很高兴我的回答对您有所帮助。我写的表达式是“布尔语句?如果为真,则为结果:如果为假,则为真”的形式,因此如果 product.trade 为真,则将其作为 classappend Trade 和空格,否则 product.type 的值只有该值产品类型。希望这是有道理的。如果您需要更多说明,请告诉我
    • 是的,我明白了。出于某种原因,我将布尔语句理解为在交易后结束。但是在你的解释之后,我意识到一切都是布尔语句的一部分,发生了什么很明显。再次感谢您!
    【解决方案2】:
    <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>
    

    【讨论】:

      猜你喜欢
      • 2017-10-14
      • 2020-10-21
      • 1970-01-01
      • 2019-04-19
      • 1970-01-01
      • 2020-08-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多