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

后端 未结 3 1120
無奈伤痛
無奈伤痛 2021-01-16 20:02

I\'m having some trouble with this...

I have code like this:

Market market = new market();
List list = marketService.getMarketItemList         


        
相关标签:
3条回答
  • 2021-01-16 20:32

    Store your data like this:

    Map<String, List<String>> data = new HashMap<String, List<String>>();
    
    0 讨论(0)
  • 2021-01-16 20:39

    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
    
    0 讨论(0)
  • 2021-01-16 20:40

    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.

    0 讨论(0)
提交回复
热议问题