2017-02-11 75 views
0

我想通過遍歷一個循環並將值連接到這個字符串變量中來在thymeleaf中創建一個字符串變量。然後我想在<span>元素中顯示這個字符串。我想實現的可以用java編寫如下:Thymeleaf構建一個字符串

String forDisplay = ""; 
foreach (MyObject o : myObjectCollection) { 
    if (o.type == 1) { forDisplay += o.stringValue; } 
} 

然後在我想把它放在像span這樣的html元素中。我知道如何使用:

<span th:each="o : ${objectCollection}" th:if="${o.type == 1}" th:text="${o.stringValue}"></span> 

但是這創造<span>每個滿足條件的元素。我只想在th標記空閒部分建立我的字符串,然後我只想在單個<span>元素中顯示我的字符串。

回答

0

艾哈邁德,看看Expression Utility Objects for Strings,從Thymeleaf docs。

您有參加的項目有三種方法:

${#strings.arrayJoin(namesArray,',')} // For Arrays 
${#strings.listJoin(namesList,',')} // For Lists 
${#strings.setJoin(namesSet,',')} // For Sets 

這些實用對象提供了很多的聚集,日曆涼爽方法等

ATT

+0

我已經看到你的建議相關捐資;但我只是想知道是否有可能與像選項一樣的jsp代碼塊。像'<%// Code Here%>'。我知道這與Thymeleaf的哲學有些相反,但在某些情況下我需要這種自由編碼能力。無論如何感謝 – Ahmet

0

這是我如何加入到數字使用「,」作爲分隔符的字符串

<span th:each="instrumentDescriptor, iterStat : ${instrument.instrumentDescriptors}" th:text="!${iterStat.last} ? ${instrumentDescriptor.instrumentVersion} + ', ': ${instrumentDescriptor.instrumentVersion}"></span> 
+0

再次我想有一個像thymeleaf設施的jsp代碼塊。我發現顯然不可能擁有這種功能。 – Ahmet