数组和列表中将整数作为字符串处理

数组和列表中将整数作为字符串处理

本文介绍了在 Thymeleaf 数组和列表中将整数作为字符串处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么以下 Thymeleaf th:if 测试对字符串0"、1"和9"失败?

我有一个 Java 数组,如下所示:

String[] arrayData = {x"、-1"、0"、1"、9"、10"、11"};

包含 x" 是为了说明该数组可以包含字母值和数字值.

我有一个包含以下内容的 Thymeleaf 模板:

<html xmlns:th="http://www.thymeleaf.org"><头><title>测试</title><元字符集=UTF-8"><身体><div th:if="${#arrays.contains(arrayData, '-1')}";th:text="'找到数组字符串\'-1\''"></div><div th:if=${#arrays.contains(arrayData, '0')}";th:text="'找到数组字符串\'0\''"></div><div th:if="${#arrays.contains(arrayData, '1')}";th:text="'找到数组字符串\'1\''"></div><div th:if="${#arrays.contains(arrayData, '9')}";th:text="'找到数组字符串\'9\''"></div><div th:if="${#arrays.contains(arrayData, '10')}";th:text="'找到数组字符串\'10\''"></div><div th:if="${#arrays.contains(arrayData, '11')}";th:text="'找到数组字符串\'11\''"></div></html>

我希望这会在浏览器中生成以下输出:

找到数组字符串'-1'找到数组字符串 '0'找到数组字符串 '1'找到数组字符串 '9'找到数组字符串 '10'找到数组字符串 '11'

但我实际上得到了以下信息:

找到数组字符串'-1'找到数组字符串 '10'找到数组字符串 '11'

问题:为什么字符串0"1""的测试失败?9"?我做错了什么?

针对十个字符串值0"的所有此类测试均已执行.通过9"失败.超出该范围的任何事情都按预期工作.

如果我使用 ArrayList 和 Thymeleaf #lists.contains() 运算符,也会发生同样的事情.

Thymeleaf 版本是:

<groupId>org.thymeleaf</groupId><artifactId>百里香</artifactId>3.0.11.RELEASE</依赖>

据我所知,我认为实现 #arrays.contains() 函数的 Thymeleaf 代码是 这里 - 看起来很简单.

我的 Java 版本是 AdoptOpenJDK 14.

在这个特定场景中使用 Spring.


提供答案后更新

如果我使用任何单个字符(例如 x)进行测试,则会发生与 09 相同的问题.所以标题在这方面有点误导.

解决方案

我怀疑如果您不使用 Spring,Thymeleaf 表达式会使用 OGNL insteald 进行解释SPeL -- 似乎将单个字符常量视为 char 类型而不是 String 类型,因此 #arrays.contains 表达式匹配失败.

我没有测试 OGNL 的设置,但根据 这篇文章,这应该有效:

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

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.

解决方案

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?)

这篇关于在 Thymeleaf 数组和列表中将整数作为字符串处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-26 06:43