如何将对象列表从spring控制器绑定到thymeleaf

时间:2019-08-20 18:10:21

标签: java spring 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;
}

}

这是“菜单”容器:

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 /主页”-第23行,第25行)

1 个答案:

答案 0 :(得分:0)

您将模型属性设置为错误。

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}">