java - Render multi value hash map with single key and multiple values in JSP -


i have following multihashmap in controller.java

multihashmap multimap= new multihashmap();  list<samplelist> list1 = (list<samplelist>)                           request.getportletsession().getattribute("list1");   iterator<samplelist> samplelistiterator= list1.iterator();  while(samplelistiterator.hasnext()){     samplelist samplelist = samplelistiterator.next();     list <sublist> sublist =  samplelist.getsublist();     iterator<sublist> sublistiterator = sublist.iterator();      while(sublistiterator.hasnext() ){         sublist sublist2 = sublistiterator.next();         multimap.put(sublist2.getcategorysubcategory(),sublist2.getcost());     } } 

in jsp have table displays above hashmap

<table>     <tbody>         <c:foreach var="item" items="${multimap}">             <tr>                 <th> ${item.key}</th>                 <c:foreach var="valuelist" items=${item.value}>                     <td> ${valuelist}</td>                 </c:foreach>             </tr>         </c:foreach>     </tbody> </table> 

i'm getting error when try render value controller.

apache commons multihashmap deprecated , should use instead multivaluemap.

deprecated. class available multivaluemap in map subpackage. version due removed in collections v4.0.

both multihashmap , multivaluemap classes have getcollection(object key) method, return collection of values specific key :

java.util.collection  getcollection(java.lang.object key) 

gets collection mapped specified key.

you can iterate in jsp other collection, using jstl, ognl, etc...


edit

surprinsingly, there not one example of class usage in jstl nor ognl in whole internet... guess question become popular, +1 ;)

since multivaluemap (and deprecated multihashmap) map collection of values every key of map, should iterated in jstl (untested, presumably right) (now tested on jstl 1.1.2 in servlet 2.4 / jsp 2.0 container):

<c:foreach var="entry" items="${multimap}">     <br/>-> key: <c:out value="${entry.key}"/>     <br/>-> values key:     <c:foreach var="currentvalue" items="${entry.value}">         <br/>|---> value: <c:out value="${currentvalue}"/>     </c:foreach> </c:foreach> 

populating this

multimap = new multivaluemap(); multimap.put("key1", "value1-1"); multimap.put("key1", "value1-2"); multimap.put("key1", "value1-3"); multimap.put("key2", "value2-1"); multimap.put("key2", "value2-2"); multimap.put("key2", "value2-3"); multimap.put("key3", "value3-1"); multimap.put("key3", "value3-2"); multimap.put("key3", "value3-3"); 

the output (copied , pasted jsp):

-> key: key1 -> values key: |---> value: value1-1 |---> value: value1-2 |---> value: value1-3 -> key: key3 -> values key: |---> value: value3-1 |---> value: value3-2 |---> value: value3-3 -> key: key2 -> values key: |---> value: value2-1 |---> value: value2-2 |---> value: value2-3  

then works (apart curious fact key3 coming before key2); if still have problems, they're not code-related, in config.

please check out jstl tag wiki on so, this nice article or numerous answers jstl problems balusc here on so.


as final though, looking @ code using to create multivaluemap, suggest start building custom object, object oriented programming suggests, avoid excessive nesting of lists , maps, result in unreadable, unmaintainable code.


Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -