Display multi-valued hash map with one key and multiple values ​​in JSP

I have the following MultiHashMap in a .java controller

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, I have a table that displays the 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 get an error when I try to display this value from the controller.

+4
source share
3 answers

Apache Commons MultiHashMap is deprecated , you should use MultiValueMap instead.

Outdated . The class is now available as a MultiValueMap in the map subpackage. This version should be removed in v4.0 collections.

Both the MultiHashMap and MultiValueMap classes have a getCollection(Object key) method that returns a collection of values ​​for this particular key:

 java.util.Collection getCollection(java.lang.Object key) 

Gets the collection associated with the specified key.

You can iterate over it in JSP like any other collection using JSTL, OGNL, etc.


EDIT

Surprisingly, there is not even one example of using this class in JSTL or OGNL all over the Internet ... I think your question will become popular, +1;)

Since the MultiValueMap (and the obsolete MultiHashMap) is a map with a collection of values ​​for each map key, it should be repeated in JSTL, like this one (untested, but supposedly correct) ( now TESTED on JSTL 1.1.2 in the Servlet 2.4 / JSP 2.0 container ) :

 <c:forEach var="entry" items="${multiMap}"> <br/>-> Key: <c:out value="${entry.key}"/> <br/>-> Values for this key: <c:forEach var="currentValue" items="${entry.value}"> <br/>|---> value: <c:out value="${currentValue}"/> </c:forEach> </c:forEach> 

Filling this type

 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 result is this (copied and pasted from my JSP):

 -> Key: key1 -> Values for this key: |---> value: value1-1 |---> value: value1-2 |---> value: value1-3 -> Key: key3 -> Values for this key: |---> value: value3-1 |---> value: value3-2 |---> value: value3-3 -> Key: key2 -> Values for this key: |---> value: value2-1 |---> value: value2-2 |---> value: value2-3 

Then it works fine (except for the curious fact that key3 fits up to key2); if you still have problems, they are not code related, but in your configuration.

Please check out the JSTL wiki tags on SO , this good article or the numerous answers to JSTL problems from BalusC here on SO.


As a final point, although looking at the code that you use to create the MultiValueMap, I highly recommend that you start creating some custom object , as object-oriented programming suggests, to avoid over-nesting lists and Maps, which can lead to unreadable, unreachable code.

+4
source

I have not used MultiHashMaps before, but I usually display the contents of the HashMap on the jsp page (without the generics example):

 Iterator iter = map.entrySet().iterator(); while(iter.hasNext()){ Map.Entry entry = iter.next(); Object key = entry.getKey(); Object value = entry.getValue(); 
+1
source

Using JTL Core taglibrary: <%

 Map<String, String> countryList = new HashMap<String, String>(); countryList.put("United States", "Washington DC"); countryList.put("India", "Delhi"); countryList.put("Germany", "Berlin"); countryList.put("France", "Paris"); countryList.put("Italy", "Rome"); request.setAttribute("capitalList", countryList); %> <c:forEach var="country" items="${capitalList}"> Country: ${country.key} - Capital: ${country.value} </c:forEach> 

If you use struts, you can arrange the logical tag library to repeat and display the map in JSP:

  <logic:iterate name="students" id="nameObj" scope="session"> <bean:write name="nameObj" property="key"/> <bean:write name="nameObj" property="value"/> </logic:iterate> 
0
source

Source: https://habr.com/ru/post/1480722/


All Articles