2016-05-16 76 views
2

我有這樣的一個模板:如何將此Thymeleaf 2.1.4的模板片段遷移到Thymeleaf 3.0?

<table th:with="isEven=false"> 
     <tr th:if="*{foo} != null" class='odd' th:class="${isEven=!isEven}?'even':'odd'"> 
     foo 
     </tr> 
     <tr th:if="*{bar} != null" class='odd' th:class="${isEven=!isEven}?'even':'odd'"> 
     bar 
     </tr> 
</table> 

它曾與Thymeleaf 2.1.4但3.0.0它拋出以下異常:

org.thymeleaf.exceptions.TemplateProcessingException: 
    Exception evaluating OGNL expression: "isEven=!isEven" (template: "foo" - line 10, col 34) 

Caused by: java.lang.UnsupportedOperationException: 
    Cannot set values into VariablesMap instances from OGNL Expressions 

任何想法如何解決這一問題?

注: 這不是一個循環,所以沒有varStat對象像

<tr th:each="prod : ${prods}" th:class="${prodStat.odd}? 'odd'">

<tr>生成取決於th:if條件,我想有一個偶/奇怪的切換爲他們。

編輯:我爲此問題打開了一個Github issue

回答

1

Thymeleaf 3不允許修改上下文變量作爲OGNL/SpringEL表達式的橫向影響(實際上這是2.1中錯誤允許的內容)。

但是,如果我正確理解您的代碼,您應該能夠在th:with中創建所需的標誌。類似這樣的:

<table th:with="hasFoo=(*{foo} != null)"> 
     <tr th:if="${hasFoo}" class='even'> 
     foo 
     </tr> 
     <tr th:if="*{bar} != null" class='odd' th:class="${hasFoo}? even : odd"> 
     bar 
     </tr> 
</table> 
+0

這沒有什麼幫助,因爲有很多很多其他的變量跟着'foo'和'bar'。如果內容不是空/空,並且在生成的輸出中交替使用「偶數」/「奇數」類,則意味着「打印某些內容」。我基本上需要'varStat',但沒有封閉循環,有點像「手動」varStat。 – Huxi

+0

在這種情況下,似乎最適合的解決方案是創建一個包含在上下文中的bean,以及可以從表達式調用哪些方法來返回索引(以及另一種方法來告訴您它是偶數還是奇數)。類似於varStat,但手動添加。 –

相關問題