0

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!

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Wizard Mage
  • 75
  • 2
  • 7
  • This is pure jsp or anything – newuser Aug 29 '13 at 01:51
  • i think this can use for each,,,but maybe need modification – Wizard Mage Aug 29 '13 at 02:19
  • You could do it with a simple foreach, but it will get messy. You'd have to keep track of the previous type and determine whether or not it has changed for each row. If you use a Map it will be much cleaner. You can iterate over each key (which would be a type) and then print the associated list of fruit names, markets, etc... – jahroy Aug 29 '13 at 06:28
  • i alredy use hashmap but still same.. key = fruit | value = banana the output still same.. – Wizard Mage Aug 29 '13 at 06:56
  • If you're already using a HashMap, you're using `` wrong. Take a look at the answer I link to in my answer. It shows how to use `` tag with a Map (note that the `var` in the loop is actually of type `MapEntry`). The example code you've shared makes it look like you're using a list (not a Map). I also added a bunch of example code to my answer... – jahroy Aug 29 '13 at 06:59
  • I'm not sure if these are typos or not, but you have two errors in your JSP: `/a>` and `/li>` is incorrect. I'll edit my answer to talk about that, too. – jahroy Aug 29 '13 at 07:09

3 Answers3

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.

Community
  • 1
  • 1
jahroy
  • 22,322
  • 9
  • 59
  • 108
  • I don't know if that `|` symbol is allowed in the JSP... You might want to try without that first ;-) Unforunately I can't test anything I typed above, so there may be errors. – jahroy Aug 29 '13 at 07:36
  • this works thanks!!..and thanks to help me fixed my Question..! my english bad >,<" You rockkkk... ^_^ – Wizard Mage Aug 29 '13 at 07:55
1

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
Jasper de Vries
  • 19,370
  • 6
  • 64
  • 102
0

Store your data like this:

Map<String, List<String>> data = new HashMap<String, List<String>>();
SergeyB
  • 9,478
  • 4
  • 33
  • 47