1

I have a ModelMap variable "model", the value object contained in the model map itself is a HashMap.

Controller code:

public String func(ModelMap model) 
{
    HashMap<String, List<String> aMap = new HashMap<String, List<String>();
    ArrayList<String> aList = new ArrayList<String>();
    ....// give aList some data
    aMap.put("keystring", aList);
    model.addAttribute("aMap", aMap); 

    String view = "test";
    return view;
}

test.jsp code:

 var data = '${aMap}'; 
 // I know this gets the entire aMap including its key ("keystring") 
 // and the value (aList)
 var key ='${aMap}.key'; 
 alert(key); 
 var value ='${aMap}.value'; 
 alert(value);  

I also tried:

 var va= data.key; // also tried data[key], data['key']
 alert(va);      

but they all printed either an empty string or undefined. However, if I printed "data", then I can see the entire map.

How do I access aMap's key ("keyString") and the value (aList) from test.jsp script part ? Any help will be appreciated.

jlp
  • 1,656
  • 6
  • 33
  • 48

1 Answers1

1

You might want to take a look at this solution How to iterate HashMap using JSTL forEach loop?

It also seems that in your method should not be @ResponseBody since you are returning a view name.

Community
  • 1
  • 1
Vladislav Lezhnin
  • 837
  • 1
  • 7
  • 17
  • I just tried adding var key ='${aMap.key}'; alert(key); var value ='${aMap.value}'; alert(value), but both returned me empty string. – jlp Mar 14 '15 at 18:47
  • var key ='${aMap.key}' - that construction does not make any sence, since aMap does not have a property key and value. Entry of a map has, but a map itself does not – Vladislav Lezhnin Mar 14 '15 at 19:07