英文:
Handling Integers as Strings in Thymeleaf Arrays and Lists
问题
为什么以下 Thymeleaf 的 th:if
测试对字符串 "0"、"1" 和 "9" 失败?
我有一个 Java 数组如下:
String[] arrayData = {"x", "-1", "0", "1", "9", "10", "11"};
其中包括 "x" 是为了说明该数组既可以包含字母值,也可以包含数字值。
我有一个包含以下内容的 Thymeleaf 模板:
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Test</title>
<meta charset="UTF-8">
</head>
<body>
<div th:if="${#arrays.contains(arrayData, '-1')}"
th:text="'found array string ''-1'''"></div>
<div th:if="${#arrays.contains(arrayData, '0')}"
th:text="'found array string ''0'''"></div>
<div th:if="${#arrays.contains(arrayData, '1')}"
th:text="'found array string ''1'''"></div>
<div th:if="${#arrays.contains(arrayData, '9')}"
th:text="'found array string ''9'''"></div>
<div th:if="${#arrays.contains(arrayData, '10')}"
th:text="'found array string ''10'''"></div>
<div th:if="${#arrays.contains(arrayData, '11')}"
th:text="'found array string ''11'''"></div>
</body>
</html>
我期望这会在浏览器中生成以下输出:
found array string '-1'
found array string '0'
found array string '1'
found array string '9'
found array string '10'
found array string '11'
但实际上我得到了以下结果:
found array string '-1'
found array string '10'
found array string '11'
问题:为什么对于字符串 "0"、"1" 和 "9" 的测试会失败?我做错了什么?
所有针对字符串值 "0" 到 "9" 的测试都失败。在该范围之外的任何值都按预期工作。
如果我使用 ArrayList<String>
并使用 Thymeleaf 的 #lists.contains()
操作符,情况也是一样的。
Thymeleaf 版本是:
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf</artifactId>
<version>3.0.11.RELEASE</version>
</dependency>
据我所知,实现 #arrays.contains()
函数的 Thymeleaf 代码在这里 - 它看起来很简单。
我的 Java 版本是 AdoptOpenJDK 14。
在这个特定场景中,我没有使用 Spring。
回答后更新
如果我测试任何单个字符(例如 x
),与 "0" 到 "9" 一样会出现相同的问题。所以标题在这方面有些误导。
英文:
Why do the following Thymeleaf th:if
tests fail for the strings "0", "1", and "9"?
I have a Java array as follows:
String[] arrayData = {"x", "-1", "0", "1", "9", "10", "11"};
The "x"
is included to clarify that this array can contain alphabetic values as well as numeric values.
I have a Thymeleaf template containing the following:
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Test</title>
<meta charset="UTF-8">
</head>
<body>
<div th:if="${#arrays.contains(arrayData, '-1')}"
th:text="'found array string \'-1\''"></div>
<div th:if="${#arrays.contains(arrayData, '0')}"
th:text="'found array string \'0\''"></div>
<div th:if="${#arrays.contains(arrayData, '1')}"
th:text="'found array string \'1\''"></div>
<div th:if="${#arrays.contains(arrayData, '9')}"
th:text="'found array string \'9\''"></div>
<div th:if="${#arrays.contains(arrayData, '10')}"
th:text="'found array string \'10\''"></div>
<div th:if="${#arrays.contains(arrayData, '11')}"
th:text="'found array string \'11\''"></div>
</body>
</html>
I expect this to generate the following output in a browser:
found array string '-1'
found array string '0'
found array string '1'
found array string '9'
found array string '10'
found array string '11'
But I actually get the following:
found array string '-1'
found array string '10'
found array string '11'
Question: Why do the tests fail for the strings "0"
, "1"
, and "9"
? What am I doing wrong?
All such tests for the ten string values "0" through "9" fail. Anything outside that range works as expected.
The same thing happens if I use an ArrayList<String>
, with the Thymeleaf #lists.contains()
operator.
The Thymeleaf version is:
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf</artifactId>
<version>3.0.11.RELEASE</version>
</dependency>
As far as I can tell, I think the Thymeleaf code which implements the #arrays.contains()
function is here - and it looks straightforward.
My Java version is AdoptOpenJDK 14.
I am not using Spring in this specific scenario.
Update, After Answer was Provided
If I test with any single character (e.g. x
) the same problem happens as with 0
through 9
. So the title is somewhat misleading in that regard.
答案1
得分: 2
我怀疑如果你没有使用Spring
,Thymeleaf表达式将会使用OGNL
来解释,而不是SPeL
—— 这似乎将单个字符常量视为char
类型而不是String
类型,因此#arrays.contains
表达式无法匹配。
我没有设置来测试OGNL,但根据这篇帖子,这应该可以工作:
<div th:text="${#arrays.contains(arrayData, "0")}" />
(或者也许#arrays.contains(arrayData, '' + '0')
会起作用?)
英文:
I suspect that if you aren't using Spring
, Thymeleaf expressions are being interpreted using OGNL
insteald of SPeL
-- which appears to treat single character constants as type char
instead of type String
and so the #arrays.contains
expressions fail to match.
I don't have a setup to test OGNL, but according to this post, this should work:
<div th:text="${#arrays.contains(arrayData, &quot;0&quot;)}" />
(Or maybe #arrays.contains(arrayData, '' + '0')
would work?)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论