java - 如何在 thymeleaf numbers.sequence() 方法中使用常量类中的变量?
问题描述
我在这样的Constants
类中定义了两个静态变量
public static String MIN_VALUE = 1;
public static String MAX_VALUE = 10;
我想创建一个 html 选择标签,其中包含所有数字从MIN_VALUE
to开始的选项MAX_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({)'
如何在百里香的方法中使用我Constant
班级中的变量numbers.sequence
?
我发现这个问题的标题几乎相似,但我没有通过model
这里的属性传递我的变量限制。我可以使用thymeleaf 标签从Constants
类中访问我的变量。Constants.VAR_NAME
解决方案
- 不要嵌套
${...}
表达式。 - 按照评论中的说明使用
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>
推荐阅读
- arrays - 如何在 Arduino 灯板上同时运行两个 LED 阵列时消除延迟?
- python - 如何将数据框导出到现有的格式化 csv 文件?
- bitcoin - bitcoin-cli 命令参数中的详细程度是什么?
- java - Rapidoid REST 一个内衬示例抛出空指针异常
- node.js - nginx 不重定向后端
- python - 创建一个包装函数 safe input(),它的工作方式与函数 input() 类似,只是它在引发异常时返回 None
- python - 按索引索引 pandas 数据帧
- ios - unity iOS 支持,安装失败
- css - React:使用混合的 React 和 DOM 事件时停止点击事件传播
- python - 为什么不执行睡眠指令之前的行?