Warm tip: This article is reproduced from stackoverflow.com, please click
jsp jstl foreach

c:forEach don't repeat same values when already present in previous row

发布于 2020-03-27 15:41:30

I'm having some trouble with this...

I have code like this:

Market market = new market();
List<Market > list = marketService.getMarketItemList(market);
model.addAttribute("list", list);

I have a table like this:

type      |  item_name

fruit     |  Banana

fruit     |  Apple

vegetable |  Onion

And I have coded a for each in my JSP like this:

<c:forEach var="cmenu" items="${list}">
    <li><a href="${url_itemmarket}/${cmenu.itemName}">${cmenu.description}/a>/li>
</c:forEach>

In the JSP, I want it to look like this:

type      |  Item Name

Fruit     |  Banana

          |  Apple

Vegetable |  Onion

I don't want to repeat value fruit in jsp view.

Can anyone help me to get an example or some references for this?

Thanks!

Questioner
Wizard Mage
Viewed
65
2017-05-23 18:32

1.

You have some errors in your HTML:

<li><a href="${url_itemmarket}/${cmenu.itemName}">${cmenu.description}/a>/li>
                                                                      ^  ^
                                                                      |  |
    two errors here (mising < characters) --------------------------------

    replace with this -----------------------------------------------------
                                                                      |   |
                                                                      v   v
<li><a href="${url_itemmarket}/${cmenu.itemName}">${cmenu.description}</a></li>

2.

You should use a Map.

The keys of the map should be the different types.

The values should be Lists of Food objects.

Then you can iterate over the keys of the map in your JSP.

You'll need a nested loop to iterate over the Foods in each List.

I think your JSP/JSTL would look something like this, but it's untested:

<table>
  <tr><th>type</th><th>Item Name</th></tr>
  <!-- iterate over each key in the map -->
  <c:forEach var="foodMapEntry" items="${foodMap}">
    <tr>
      <td>${foodMapEntry.key}</td>
      <td>
        <!-- iterate over each item in the list of foods -->
        <c:forEach var="food" items="${foodMapEntry.value}">         
          | ${food.name}<br/>         
        </c:forEach>
      </td>
    </tr>   
  </c:forEach>
</table>

Here's some code that shows how to build the map used above:

/* create a list of food */
List<Food> foodList = new ArrayList<Food>();

/* add some fruits to the list */
foodList.add(new Food("Banana", "fruit"));
foodList.add(new Food("Apple", "fruit"));

/* add some veggies to the list */
foodList.add(new Food("Onion", "vegetable"));
foodList.add(new Food("Mushroom", "vegetable"));

/* add some candy to the list */
foodList.add(new Food("Chocolate", "candy"));
foodList.add(new Food("Gummy Bears", "candy"));

/* create a Map that maps food types to lists of Food objects */
Map<String, List<Food>> foodMap = new HashMap<String, List<Food>>();

/* populate the map */
for (Food f : foodList) {
    String foodType = f.getType();
    if (foodMap.containsKey(foodType)) {
        foodMap.get(foodType).add(f);
    }
    else {
       List<Food> tempList = new ArrayList<Food>();
       tempList.add(f);
       foodMap.put(foodType, tempList);
    }
}

And a simple Food class:

class Food {
   private String name;
   private String type;

   public Food(String n, String t) {
       name = n;
       type = t;
   }

   public String getName() { return name; }
   public String getType() { return type; }
}

Here's a question/answer about using Maps with JSP and JSTL.