Syrianrevo
Syrianrevo

Reputation: 31

how to bind a list of objects from the spring controller to thymeleaf

I have a MenuItems domain and a Menu container that have a list of all MenuItems, I am trying to pass the arraylist from a spring controller to the frontend.

This is the MenuItems domain:

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

}

This is the Menu container:

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();  
 }

}

This is the controller:

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

Here is what I have in thymeleaf template:

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

Here is the error message I am getting:

org.thymeleaf.TemplateEngine : [THYMELEAF][http-nio-8080-exec-10] Exception processing template "/admin/home": Exception evaluating SpringEL expression: "menuItem.itemName" (template: "/admin/home" - line 23, col 25)

Upvotes: 0

Views: 155

Answers (1)

LowKeyEnergy
LowKeyEnergy

Reputation: 652

You are setting your model attribute wrong.

model.addAttribute("menuItems", menuL);

This, naturally, should be your list of menu items, not your custom container. Try this:

model.addAttribute("menuItems", menuItems);

If you insist on passing in the menu container object, you can try:

MenuContainer menuContainer = new MenuContainer();
menuL.setMenuItems(menuItems);
model.addAttribute("menuContainer", menuContainer);

[...]

<tr th:each="menuItem : ${menuContainer.menuItems}">

Upvotes: 0

Related Questions