处理在Thymeleaf数组和列表中的整数字符串

huangapple go评论70阅读模式
英文:

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 = {&quot;x&quot;, &quot;-1&quot;, &quot;0&quot;, &quot;1&quot;, &quot;9&quot;, &quot;10&quot;, &quot;11&quot;};

The &quot;x&quot; is included to clarify that this array can contain alphabetic values as well as numeric values.

I have a Thymeleaf template containing the following:

&lt;!DOCTYPE html&gt;
&lt;html xmlns:th=&quot;http://www.thymeleaf.org&quot;&gt;
    &lt;head&gt;
        &lt;title&gt;Test&lt;/title&gt;
        &lt;meta charset=&quot;UTF-8&quot;&gt;
    &lt;/head&gt;

    &lt;body&gt;
        
        &lt;div th:if=&quot;${#arrays.contains(arrayData, &#39;-1&#39;)}&quot;
             th:text=&quot;&#39;found array string \&#39;-1\&#39;&#39;&quot;&gt;&lt;/div&gt;
                
        &lt;div th:if=&quot;${#arrays.contains(arrayData, &#39;0&#39;)}&quot;
             th:text=&quot;&#39;found array string \&#39;0\&#39;&#39;&quot;&gt;&lt;/div&gt;
                
        &lt;div th:if=&quot;${#arrays.contains(arrayData, &#39;1&#39;)}&quot;
             th:text=&quot;&#39;found array string \&#39;1\&#39;&#39;&quot;&gt;&lt;/div&gt;
                
        &lt;div th:if=&quot;${#arrays.contains(arrayData, &#39;9&#39;)}&quot;
             th:text=&quot;&#39;found array string \&#39;9\&#39;&#39;&quot;&gt;&lt;/div&gt;
        
        &lt;div th:if=&quot;${#arrays.contains(arrayData, &#39;10&#39;)}&quot;
             th:text=&quot;&#39;found array string \&#39;10\&#39;&#39;&quot;&gt;&lt;/div&gt;
        
        &lt;div th:if=&quot;${#arrays.contains(arrayData, &#39;11&#39;)}&quot;
             th:text=&quot;&#39;found array string \&#39;11\&#39;&#39;&quot;&gt;&lt;/div&gt;
                
    &lt;/body&gt;

&lt;/html&gt;

I expect this to generate the following output in a browser:

found array string &#39;-1&#39;
found array string &#39;0&#39;
found array string &#39;1&#39;
found array string &#39;9&#39;
found array string &#39;10&#39;
found array string &#39;11&#39;

But I actually get the following:

found array string &#39;-1&#39;
found array string &#39;10&#39;
found array string &#39;11&#39;

Question: Why do the tests fail for the strings &quot;0&quot;, &quot;1&quot;, and &quot;9&quot;? 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&lt;String&gt;, with the Thymeleaf #lists.contains() operator.

The Thymeleaf version is:

&lt;dependency&gt;
    &lt;groupId&gt;org.thymeleaf&lt;/groupId&gt;
    &lt;artifactId&gt;thymeleaf&lt;/artifactId&gt;
    &lt;version&gt;3.0.11.RELEASE&lt;/version&gt;
&lt;/dependency&gt;

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, &quot;0&quot;)}" />

(或者也许#arrays.contains(arrayData, &#39;&#39; + &#39;0&#39;)会起作用?)

英文:

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:

&lt;div th:text=&quot;${#arrays.contains(arrayData, &amp;quot;0&amp;quot;)}&quot; /&gt;

(Or maybe #arrays.contains(arrayData, &#39;&#39; + &#39;0&#39;) would work?)

huangapple
  • 本文由 发表于 2020年9月18日 23:33:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/63958688.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定