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

烈酒焚心 提交于 2020-02-15 06:42:50

问题


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!


回答1:


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.




回答2:


If you cannot create a map, so if you need to work with a list, you can check the previous value.

You can create a variable containing the previous value and check for that:

<c:set var="types" value="${['fruit','fruit','vegetable']}"/>
<c:forEach items="${types}" var="type">
  ${type eq previousType ? '-same-' : type}<br/>
  <c:set var="previousType" value="${type}"/>
</c:forEach>

Or, you could use the index using the varStatus attribute:

<c:set var="types" value="${['fruit','fruit','vegetable']}"/>
<c:forEach items="${types}" var="type" varStatus="status">
  ${status.index gt 0 and types[status.index - 1] eq types[status.index] ? '-same-' : type}<br/>
</c:forEach>

Here you could also use not status.first instead of status.index gt 0.

Both will output:

fruit
-same-
vegetable



回答3:


Store your data like this:

Map<String, List<String>> data = new HashMap<String, List<String>>();


来源:https://stackoverflow.com/questions/18500790/cforeach-dont-repeat-same-values-when-already-present-in-previous-row

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!