如何在百里香中使用拼写内联列表:每个标签

时间:2016-11-29 12:39:21

标签: spring thymeleaf spring-el

thymeleaf official docs中,它声明了

  

使用Spring Expression Language(Spring EL或SpEL)作为变量表达式语言,而不是OGNL。因此,所有$ {...}和* {...}表达式都将由Spring的表达式语言引擎进行评估。

所以我有以下html代码,其中#{1,2,3}是一个拼写内联列表表达式

<select>
    <option th:value="${opt}" th:each="opt : ${'#{1,2,3}'}"/>
</select>

我希望它可以转化为

<select>
    <option value="1"/>
    <option value="2"/>
    <option value="3"/>
</select>

但它转变为

<select>
    <option value="#{1,2,3}"/>
</select>

为什么呢?提前谢谢!

更新

正确用法为th:each="opt : ${'#{1,2,3}'}"

1 个答案:

答案 0 :(得分:1)

这样做你想要的:

<select>
    <option th:value="${opt}" th:each="opt : ${ {1, 2, 3} }"/>
</select>

http://docs.spring.io/spring/docs/current/spring-framework-reference/html/expressions.html#expressions-inline-lists

${'#{1,2,3}'}评估字符串&#39;#{1,2,3}&#39;,这就是为什么你会得到一个值<option>的{​​{1}}。我不确定表达式#{1,2,3}应该如何表示列表。

相关问题