Spring、Thymleaf 和字符串列表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26049708/
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
Spring, Thymleaf and lists of strings
提问by Lori Cook
Okay, I a wet-behind-the-ears newcomer to Spring and Thymleaf. I'm trying to do something so simple it should be a no-brainer. But I can't get it to work. The simple question is - how do you show a list of strings in an web page?
好吧,我是 Spring 和 Thymleaf 的新手。我正在尝试做一些很简单的事情,应该很容易。但我无法让它工作。一个简单的问题是 - 如何在网页中显示字符串列表?
I have the following model
我有以下型号
import java.util.List;
public class TestModel {
private List<String> list = null;
private String content;
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public List<String> getList() { return list; }
public void setList(final List<String> list) {
this.list = list;
}
}
My web page contains the following:
我的网页包含以下内容:
<div th:if="${greeting.list != null}">
<h1>Result</h1>
<ul>
<th:block th:object="${greeting}" th:each="item : ${list}">
<li th:text="${item.name}">Item description here...</li>
</th:block>
</ul>
</div>
I added the ".name" to "item" only because I found a couple of examples where they had a list of strings and did something similar. But they had the ".name" on the object.
我将“.name”添加到“item”只是因为我发现了几个例子,其中有一个字符串列表并做了类似的事情。但是他们在对象上有“.name”。
But it still doesn't work. The unordered list ends up empty. I.e. There isn't any list items inside the unordered tags.
但它仍然不起作用。无序列表最终为空。即无序标签内没有任何列表项。
What amI doing wrong? Pointers gladly accepted.
什么我哪里做错了?欣然接受指点。
回答by michal.kreuzman
Since there's no example of filling model I supposed you put some strings into instance of TestModel's list field like this.
由于没有填充模型的示例,我假设您TestModel像这样将一些字符串放入's list 字段的实例中。
TestModel greeting= new TestModel();
List<String> list = new ArrayList<>();
list.add("A");
list.add("B");
model.addAttribute("greeting", greeting);
Then there are more errors in your Thymeleaf template example.
然后在您的 Thymeleaf 模板示例中有更多错误。
- If you are using object selection through
th:objectyou must at first place use asterix*to access object properties. Asterisk syntax evaluates expressions on selected objects instead of context variables map. - Object selection affects only children nodes in DOM.
- In your example you want iterate over list of strings (
List<String>) but you want to access propertynamewhich in fact don't exists on JavaStringobject.
- 如果您使用对象选择,
th:object您必须首先使用星号*来访问对象属性。星号语法评估选定对象上的表达式,而不是上下文变量映射。 - 对象选择仅影响 DOM 中的子节点。
- 在您的示例中,您想要遍历字符串列表 (
List<String>) 但您想要访问name实际上在 JavaString对象上不存在的属性。
You must fix your Thymeleaf template in one way - see examples.
您必须以一种方式修复您的 Thymeleaf 模板 - 请参阅示例。
No object selection at all
根本没有对象选择
<div th:if="${greeting.list != null}">
<h1>Result</h1>
<ul>
<li th:each="item : ${greeting.list}" th:text="${item}">Item description here...</li>
</ul>
</div>
Proper object selection
正确的对象选择
<div th:if="${greeting.list != null}">
<h1>Result</h1>
<ul>
<th:block th:object="${greeting}">
<li th:each="item : *{list}" th:text="${item}">Item description here...</li>
</th:block>
</ul>
</div>
回答by Rakesh Goswami
<table th:object="${userList}" id="userTable" border="1">
<tr th:each="user :${userList}">
<td th:text="${user.getName()}"></td>
<td th:text="${user.getEmail()}"></td>
</tr>
</table>
Another example using table and Object. Might be useful to someone else.
另一个使用表和对象的例子。可能对其他人有用。

