【发布时间】:2019-04-19 12:45:36
【问题描述】:
百里香模板有这样的问题。我得到:
Exception evaluating SpringEL expression: "prod.itemName" (main:18)
从这个表达式:
<tr th:each="prod: ${product}">
<td th:text="${prod.itemName}">sdfsdf</td>
<td th:text="${prod.price}">asdasd</td>
<td>asddasdas</td>
</tr>
product 是作为模型属性传递给 thymeleaf 模板的列表。我想知道为什么它不能正常工作。
我使用了这个来源: https://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.html#iteration-basics
编辑:
package shopbasket;
public class Item {
String itemName;
double price;
Item(String itemName, double price){
this.itemName=itemName;
this.price=price;
}
void setItemName(String itemName) {
this.itemName=itemName;
}
String getItemName() {
return itemName;
}
void setPrice(float price) {
this.price=price;
}
double getPrice() {
return price;
}
}
【问题讨论】:
-
prod.itemName 是否可能为空?
-
我认为不能为空
-
你确定你拥有类的所有getter和setter吗?我在这里查看完全相同的代码及其工作:/
-
只是一个猜测...
prod: ${product}- 假设其他一切都正常,您是否需要冒号前的空格,例如prod : ${product}?几乎每个在线 sn-p 都有那个空格,即使你链接的教程也有。也许这就是这里的问题 -
prod的类型可能不是你想象的那样。在循环中只输出${prod},看看ThymeLeaf 告诉你它是什么类,然后从那里继续。
标签: java spring loops thymeleaf