本文介绍了c:forEach在上一行中已经存在时不要重复相同的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这方面遇到了麻烦...

I'm having some trouble with this...

我有这样的代码:

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

我有一个这样的表:

type      |  item_name

fruit     |  Banana

fruit     |  Apple

vegetable |  Onion

我在JSP中为每个代码 编写了这样的代码:

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>

在JSP中,我希望它看起来像这样:

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

type      |  Item Name

Fruit     |  Banana

          |  Apple

Vegetable |  Onion

我不想在jsp视图中重复值 fruit .

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

有人可以帮我得到一个例子或一些参考吗?

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

谢谢!

推荐答案

1.

您的HTML中有一些错误:

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.

您应该使用地图 .

地图的键应为不同的 types .

值应为Food对象的列表.

The values should be Lists of Food objects.

然后,您可以在JSP中迭代地图的键.

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

您将需要一个嵌套循环来遍历每个列表中的食物.

我认为您的JSP/JSTL看起来像这样,但是未经测试:

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

还有一个简单的Food类:

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

这是关于将Maps与JSP和JSTL结合使用的问题/答案.

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

这篇关于c:forEach在上一行中已经存在时不要重复相同的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-01 22:23