在 SpringEL 表达式中将字符串转换为 Long 以进行选择

IVR*_*ger 1 html casting spring-el thymeleaf

我使用列表中对象的 ID 来决定是否应选择它,如下所示:

<option th:each="tag : ${tagList}" th:value="${tag.id}" th:text="${tag.text}" th:selected="${idSet.contains(tag.id)}"></option>

但是,我必须Set<Long>使用现有的 idSet () 来专门创建 idSet (),作为我的原始实现Set<String>

<option th:each="tag : ${tagList}" th:value="${tag.id}" th:text="${tag.text}" th:selected="${searchSet.contains(Long.valueOf(tag.id))}"></option>

或者

<option th:each="tag : ${tagList}" th:value="${tag.id}" th:text="${tag.text}" th:selected="${searchSet.contains(Long.parseLong(tag.id))}"></option>

产生了这个异常:

Caused by: org.springframework.expression.spel.SpelEvaluationException: EL1011E: Method call: Attempted to call method valueOf(java.lang.Long) on null context object

如何在 SpringEL 表达式中使用带有 long 的现有字符串集?

Dir*_*yne 5

特殊的“T”运算符可用于指定 java.lang.Class 的实例(“类型”)。静态方法也可以使用此运算符来调用

parseLong(String)是一个静态方法Long

所以这应该有效

T(Long).parseLong(...)
Run Code Online (Sandbox Code Playgroud)