sharkbait
sharkbait

Reputation: 3040

Retrieve a value from an HashMap in JSP page

I have a problem. I tried every kind of solution that I found on Stackoverflow. I have this Java part of code:

Map<String, ArchitetturaUnitaModel> map = new HashMap<String, ArchitetturaUnitaModel>();
        if(!CollectionUtils.isEmpty(searchPageData.getResults())){
            for(ProductData result:searchPageData.getResults()){
                if(result instanceof DocumentoData){
                    String unitaName = ((DocumentoData) result).getUnita();
                    ArchitetturaUnitaModel unita = fondoFacade.getUnitaByNameUnita(unitaName);
                    if(!map.containsKey(result.getCode())){
                        map.put(result.getCode(), unita);
                    }

                }

            }
        }

        model.addAttribute("map", map);

In a JSP page I have this part of code, where I try to retrieve some data from the map, passing the key value:

<c:set value="${map}" var="map"/>



<c:forEach items="${searchPageData.results}" var="product" varStatus="status">
  <c:set value="${product.code}" var="pcode"/>
  <c:set value="${map.get(pcode)}" var="unita"/>
  <b><spring:theme code="text.titolounita" />: </b><c:out value="${unita.nome}"/> <br>
  <b><spring:theme code="text.documento" />: </b>${product.name} <br>
  <b><spring:theme code="text.lineaferroviaria" />: </b><c:out value="${unita.lineaFerroviaria}"/>
</c:forEach>

But I can't obtain nothing from the value unita. If I print the map variable I obtain this:

{123123123=ArchitetturaUnitaModel (8796158591118)}

How I have to write to take the value from the map?

Upvotes: 0

Views: 2193

Answers (2)

sharkbait
sharkbait

Reputation: 3040

I solved this problem in this way:

<c:set value="${mappa}" var="mappa"/>
<c:set value="${product.code}" var="pcode"/>

<b><spring:theme code="text.titolounita" />: </b><c:out value="${mappa[pcode].name}"/> <br>
<b><spring:theme code="text.documento" />: </b>${product.name} <br>
<b><spring:theme code="text.lineaferroviaria" />: </b><c:out value="${mappa[pcode].lineaFerroviaria}"/>

Upvotes: 0

Dmytro Grynets
Dmytro Grynets

Reputation: 953

There is really no need to assign variable of map to the same name variable

I believe you can access map values just using ${map[key]}.

Are you sure Architectural.... have getter with name getNome() and getLine...()?

What are errors? If there are none, maybe variable is just empty

Sorry for ..., names are just too long

Upvotes: 1

Related Questions