【问题标题】:How to use variables from a Constants class in thymeleaf numbers.sequence() method?如何在 thymeleaf numbers.sequence() 方法中使用常量类中的变量?
【发布时间】:2021-08-06 18:58:04
【问题描述】:

我在 Constants 这样的类中定义了两个静态变量

public static String MIN_VALUE = 1;
public static String MAX_VALUE = 10;

我想创建一个 html 选择标签,其中包含从 MIN_VALUEMAX_VALUE 的所有数字的选项。如果我在百里香中使用硬编码数字,我可以轻松做到这一点:

<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_NAMEConstants 类访问我的变量。

【问题讨论】:

  • 你需要T(com.example.Constants).MIN_VALUE

标签: java spring thymeleaf


【解决方案1】:
  1. 不要嵌套${...} 表达式。
  2. 如 cmets 所述,使用 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>

【讨论】:

    猜你喜欢
    • 2017-04-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-10
    • 1970-01-01
    • 2016-05-25
    • 2021-12-15
    相关资源
    最近更新 更多