java - JSP: forEach - Don't repeat certain values -


i'm having trouble this...

i have code this:

market market = new market(); list<market > list = marketservice.getmarketitemlist(market); model.addattribute("list", list); 

i have table this:

type      |  item_name  fruit     |  banana  fruit     |  apple  vegetable |  onion 

and have coded for each in jsp this:

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

in jsp, want this:

type      |  item name  fruit     |  banana            |  apple  vegetable |  onion 

i don't want repeat value fruit in jsp view.

can me example or references this?

thanks!

1.

you have errors in html:

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

2.

you should use map.

the keys of map should different types.

the values should lists of food objects.

then can iterate on keys of map in jsp.

you'll need nested loop iterate on foods in each list.

i think jsp/jstl this, it's untested:

<table>   <tr><th>type</th><th>item name</th></tr>   <!-- iterate on each key in map -->   <c:foreach var="foodmapentry" items="${foodmap}">     <tr>       <td>${foodmapentry.key}</td>       <td>         <!-- iterate on each item in list of foods -->         <c:foreach var="food" items="${foodmapentry.value}">                    | ${food.name}<br/>                  </c:foreach>       </td>     </tr>      </c:foreach> </table> 

here's code shows how build map used above:

/* create list of food */ list<food> foodlist = new arraylist<food>();  /* add fruits list */ foodlist.add(new food("banana", "fruit")); foodlist.add(new food("apple", "fruit"));  /* add veggies list */ foodlist.add(new food("onion", "vegetable")); foodlist.add(new food("mushroom", "vegetable"));  /* add candy list */ foodlist.add(new food("chocolate", "candy")); foodlist.add(new food("gummy bears", "candy"));  /* create map maps food types lists of food objects */ map<string, list<food>> foodmap = new hashmap<string, list<food>>();  /* populate map */ (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 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 question/answer using maps jsp , jstl.


Comments

Popular posts from this blog

Unable to remove the www from url on https using .htaccess -