[英]How to bind a list of objects from a form in the View to the Controller in Spring 3
[英]how to bind a list of objects from the spring controller to thymeleaf
我有一个MenuItems域和一个包含所有MenuItems列表的Menu容器,我正在尝试将arraylist从spring控制器传递到前端。
这是MenuItems域:
public class MenuItems {
private String itemName;
private String itemDescription;
private String itemPrice;
private String itemQuantity;
private String itemCategory;
public String getItemName() {
return itemName;
}
public void setItemName(String itemName) {
this.itemName = itemName;
}
public String getItemDescription() {
return itemDescription;
}
public void setItemDescription(String itemDescription) {
this.itemDescription = itemDescription;
}
public String getItemPrice() {
return itemPrice;
}
public void setItemPrice(String itemPrice) {
this.itemPrice = itemPrice;
}
public String getItemQuantity() {
return itemQuantity;
}
public void setItemQuantity(String itemQuantity) {
this.itemQuantity = itemQuantity;
}
public String getItemCategory() {
return itemCategory;
}
public void setItemCategory(String itemCategory) {
this.itemCategory = itemCategory;
}
}
这是Menu容器:
public class MenuContainer {
private List<MenuItems> menuAList;
public List<MenuItems> getMenuItems() {
return menuAList;
}
public void setMenuItems(List<MenuItems> menuList) {
menuAList = menuList;
}
public String toString(){
return menuAList.toString();
}
}
这是控制器:
@RequestMapping(value = "/admin/home", method = RequestMethod.GET)
public String home(Model model) throws Exception{
List<MenuItems> menuItems = KafkaConsumerFromTopic.menuArrayL;
MenuContainer menuL = new MenuContainer();
menuL.setMenuItems(menuItems);
model.addAttribute("menuItems", menuL);
//System.out.println(menuL.toString());
return "/admin/home";
}
这是百里香模板中的内容:
<table>
<thead>
<tr>
<th>Item Name</th>
<th>Item Description</th>
<th>Item Price</th>
<th>Item Quantity</th>
<th>Item Category</th>
</tr>
</thead>
<tbody>
<tr th:each="menuItem : ${menuItems}">
<td th:text="${menuItem.itemName}"></td>
<td th:text="${menuItem.itemDescription}"></td>
<td th:text="${menuItem.itemPrice}"></td>
<td th:text="${menuItem.itemQuantity}"></td>
<td th:text="${menuItem.itemCategory}"></td>
</tr>
</tbody>
</table>
这是我收到的错误消息:
org.thymeleaf.TemplateEngine:[THYMELEAF] [http-nio-8080-exec-10]异常处理模板“ / admin / home”:评估SpringEL表达式的异常:“ menuItem.itemName”(模板:“ / admin / home”-第23行,第25行)
您将模型属性设置为错误。
model.addAttribute("menuItems", menuL);
自然,这应该是您的菜单项列表,而不是您的自定义容器。 尝试这个:
model.addAttribute("menuItems", menuItems);
如果您坚持要传递菜单容器对象,则可以尝试:
MenuContainer menuContainer = new MenuContainer();
menuL.setMenuItems(menuItems);
model.addAttribute("menuContainer", menuContainer);
[...]
<tr th:each="menuItem : ${menuContainer.menuItems}">
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.