Java Thymeleaf #lists.contains() 表达式实用程序不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20666635/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Thymeleaf #lists.contains() expression utility not working
提问by Tom Lerma
I'm working with the thymeleaf standard dialect and trying to render a list of checkboxes in a form. The rendering is ok, however, the problem is where I try to apply the "checked" property to the checkboxes using the thymeleaf #lists.contains() expression utility method.
我正在使用 thymeleaf 标准方言并尝试在表单中呈现复选框列表。渲染没问题,但是,问题是我尝试使用 thymeleaf #lists.contains() 表达式实用程序方法将“checked”属性应用于复选框。
So I have a model class that has the following fields:
所以我有一个具有以下字段的模型类:
private List<Template> templates;
@FormParam("selectedTemplates")
private List<String> selectedTemplates = Lists.newArrayList();
A Thymeleaf template html fragment:
Thymeleaf 模板 html 片段:
<div th:each="template : *{templates}">
<input type="checkbox" name="selectedTemplates" th:value="${template.id}"
th:checked="${#lists.contains(product.selectedTemplates, template.id)}" />
<label th:text="${template.filename} + ' (' + ${template.description} + ')'" />
<!-- Attempt to use the list contains to check the field -->
<div th:text="${product.selectedTemplates}"/>
<div th:text="${template.id}"/>
<div th:text="${#lists.contains(product.selectedTemplates, template.id)}" />
</div>
The output on the page for one of the checkboxes that should be selected.
页面上应选择的复选框之一的输出。
<input type="checkbox" name="selectedTemplates" value="4" /> (Template Name)
<div>[4,5]</div>
<div>4</div>
<div>false<div>
So as you can see, I print the list which has values [4,5] and I use the #lists.contains method to see if it has template.id in it, however, the method always returns false. I even tried some hard coded ids to test the method and I always get "false" back.
如您所见,我打印了包含值 [4,5] 的列表,并使用 #lists.contains 方法查看其中是否包含 template.id,但是,该方法始终返回 false。我什至尝试了一些硬编码的 id 来测试该方法,但我总是得到“false”。
For example:
例如:
<div th:text="${product.selectedTemplates}"/>
<div th:text="${#lists.contains(product.selectedTemplates, 4)}" />
Prints [4,5]false
打印 [4,5]false
<div th:text="${product.selectedTemplates}"/>
<div th:text="${#lists.contains(product.selectedTemplates, '4')}" />
Prints [4,5]false
打印 [4,5]false
Not sure what I'm doing wrong, but it seems so straight forward, not sure what else to try. My guess is there is something wrong with the syntax. Any suggestions or advice is greatly appreciated. I'm not able to find any resources on troubleshooting this problem, the thymeleaf guide quickly glosses over that section.
不确定我做错了什么,但它看起来很简单,不知道还能尝试什么。我的猜测是语法有问题。非常感谢任何建议或建议。我找不到解决此问题的任何资源,thymeleaf 指南很快就掩盖了该部分。
采纳答案by Tom Lerma
I had posed this answer in thymeleaf forum and got some help to determine the root cause and resolution. See [http://forum.thymeleaf.org/Problem-with-thymeleaf-expression-utility-lists-contains-td4027317.html][1]
我在 thymeleaf 论坛上提出了这个答案,并得到了一些帮助来确定根本原因和解决方案。见 [ http://forum.thymeleaf.org/Problem-with-thymeleaf-expression-utility-lists-contains-td4027317.html][1]
Essentially, the single character strings are being interpreted as a character type by default and therefore never match any of the strings in the list. Multi-character strings evaluate to a string type therefore work as expected. By encapsulating the value being searched in html encoded quotes, the parser is forced to evaluate the single character string as a string type instead of a char type. For example:
本质上,默认情况下,单个字符串被解释为字符类型,因此永远不会匹配列表中的任何字符串。多字符串评估为字符串类型,因此按预期工作。通过将要搜索的值封装在 html 编码的引号中,解析器被迫将单个字符串评估为字符串类型而不是字符类型。例如:
<div th:text="${#lists.contains(testList, "3")}"/>
<div th:text="${#lists.contains(testList, "P")}"/>
Just wanted to post this in case anyone was interested in the root cause and solution.
只是想发布这个以防万一有人对根本原因和解决方案感兴趣。
回答by Martin Frey
As i can see your selectedtemplates contains a list of templates and not ids, right?
正如我所看到的,您的 selectedtemplates 包含一个模板列表而不是 id,对吗?
If thats the case you should check the list like this:
如果是这种情况,您应该像这样检查列表:
<input type="checkbox" name="selectedTemplates" th:value="${template.id}" th:checked="${#lists.contains(product.selectedTemplates, template)}" />
回答by Tom Lerma
I did some further experimentation and found an issue where the #lists.contains() utility method does not work for a single character string. I would add that I'm using thymeleaf 2.0.19, but also tried 2.1.1.RELEASE with the same result.
我做了一些进一步的实验,发现了一个问题,即 #lists.contains() 实用程序方法对单个字符串不起作用。我想补充一点,我使用的是 thymeleaf 2.0.19,但也尝试了 2.1.1.RELEASE,结果相同。
I created a simple list in my model and added some values:
我在我的模型中创建了一个简单的列表并添加了一些值:
@FormParam("testList")
private List<String> testList = Lists.newArrayList();
testList.add("test1");
testList.add("test2");
testList.add("3");
testList.add("P");
testList.add("33");
Then tested the #lists.contains() method like so:
然后像这样测试 #lists.contains() 方法:
<div th:text="${product.testList}"/>
<div th:text="${#lists.contains(product.testList, 'test1')}"/>
<div th:text="${#lists.contains(product.testList, 'test2')}"/>
<div th:text="${#lists.contains(product.testList, '3')}"/>
<div th:text="${#lists.contains(product.testList, 'P')}"/>
<div th:text="${#lists.contains(product.testList, '33')}"/>
And the output is as follows:
输出如下:
<div>[test1, test2, 3, P, 33]</div>
<div>true</div>
<div>true</div>
<div>false</div>
<div>false</div>
<div>true</div>
So, clearly the method does not work for single character strings. Because I'm working on a new project I simply reset the sequences driving these ids so that I don't have any single character id's to work with. That is certainly not the solution I was hoping for but it works. I will also add that in order to get the method to work in the context of my question, I had to add an empty character to my id like so:
因此,显然该方法不适用于单个字符串。因为我正在处理一个新项目,所以我只需重置驱动这些 id 的序列,这样我就没有任何单个字符 id 可以使用。这当然不是我希望的解决方案,但它有效。我还要补充一点,为了让该方法在我的问题的上下文中工作,我必须向我的 id 添加一个空字符,如下所示:
th:checked="${#lists.contains(product.selectedTemplates, '' + template.id)}"
Without it, the contains method would return "false" because template.id is a Long type.
没有它, contains 方法将返回“false”,因为 template.id 是 Long 类型。
回答by SaganTheBest
i know the question is old, but i post this answer so can be useful to others users having the same problem.
我知道这个问题很老,但我发布了这个答案,所以对遇到同样问题的其他用户很有用。
i don't know if it's you or another user, but herei found that we must add
我不知道是你还是其他用户,但在这里我发现我们必须添加
'' + template.id
'' + 模板.id
so:
所以:
th:checked="${#lists.contains(product.selectedTemplates, '' + template.id)}"
for me it worked! Thank you
对我来说它奏效了!谢谢