简体   繁体   中英

How to access map key/value pair contained in a ModelMap object from jsp Spring MVC

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM