0

I am working with Spring for the first time. Right now, I have a simple HTML form to display a list of items according to some filters, just as follows:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>List of Items</title>
</head>
<body>
<form method="post" action="api/item/listItems">
    <h1>Search Items</h1>
    <div class="inset">
        <p>
            <label for="itemCode">Item Code</label>
            <input type="text" name="itemCode" id="itemCode">
        </p>
        <p>
            <label for="itemEnum">Active / No Active</label>
            <input type="text" name="itemEnum" id="itemEnum">
        </p>
        <p>
            <label for="MaxPrice">Max Price</label>
            <input type="text" name="MaxPrice" id="MaxPrice">
        </p>
        <p>
            <label for="MinPrice">Max Price</label>
            <input type="text" name="MinPrice" id="MinPrice">
        </p>
        <p>
            If you don´t complete a filter, that filter won´t be taken into account.
        </p>
    </div>
    <p class="p-container">
        <input name="submit" type="submit" value="See Items" />
    </p>

</form>
    <p>List of Items: ${listofitems}</p>
</body>
</html>

I also have a controler that get all the items that suits that filters:

 @PostMapping("/listItems")
    public String listItems(@RequestParam("itemCode") String itemCode, @RequestParam("MaxPrice") int MaxPrice, @RequestParam("MinPrice") int MinPrice, @RequestParam("itemEnum") String itemEnum, @ModelAttribute("item") Item item, ModelMap model ){
        List<Item> itemList = itemRepository.findAll();

        List<Item >finalItemList = new ArrayList<Item>();

        if(MaxPrice == 0){
            MaxPrice = 1000000;
        }

        for(int i=0;i<itemList.size();i++){
            if(((itemList.get(i).getPrice() <= MaxPrice)&&(itemList.get(i).getPrice() >= MinPrice))){
                if((itemList.get(i).getItemCode().equals(itemCode))||(itemCode.isEmpty())){
                    if((itemList.get(i).getItemEnum().getEstado().equals(itemEnum))||(itemEnum.isEmpty())){
                        finalItemList.add(itemList.get(i));
                    }
                }
            }
        }

        model.addAttribute("listofitems", finalItemList);
        return "itemView";
    }

The controller works fine. However, I don´t know how to send that list of items back to the item.html to be displayed. Right now, I added it to the model as "listofitems".

How can I achieve this? Thank you for your help!

1 Answers1

0

You have to send back the data with model but that's done. Then you have to do a Thymeleaf iteration on the list.

Greg
  • 163
  • 1
  • 2
  • 9