我在像这样的Constants
类中定义了两个静态变量
public static String MIN_VALUE = 1;
public static String MAX_VALUE = 10;
我想创建一个 html 选择标记,其中包含所有数字从 MIN_VALUE
到 MAX_VALUE
的选项。如果我在 thymeleaf 中使用硬编码数字,我可以轻松做到这一点:
<select>
<option th:each="number: ${#numbers.sequence(1, 10)}" th:value="${number}" th:text="${number}"/>
</select>
但是如果我尝试这样的事情:
<option th:each="number: ${#numbers.sequence(${Constants.MIN_VALUES}, ${Constants.MAX_VALUES})}" th:value="${number}" th:text="${number}"/>
我收到此错误:
EL1043E: Unexpected token. Expected 'rparen())' but was 'lcurly({)'
如何在 thymeleaf 的 numbers.sequence
方法中使用 Constant
类中的变量?
我找到了this标题几乎相似的问题,但我没有通过此处的 model
属性传递变量限制。我可以使用 thymeleaf 标签中的 Constants.VAR_NAME
从 Constants
类访问我的变量。
最佳答案
- 不要嵌套
${...}
表达式。 - 按照注释中的说明使用
T(com.example.Constants).MIN_VALUE
。 (其中com.example.Constants
是完整的包和类名称。)
示例:
<select>
<option
th:each="number: ${#numbers.sequence(T(com.example.Constants).MIN_VALUE, T(com.example.Constants).MAX_VALUE)}"
th:value="${number}"
th:text="${number}" />
</select>
关于java - 如何在 thymeleaf Numbers.sequence() 方法中使用 Constants 类中的变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68686397/