How to get all the key value pairs in modelMap object , inside spring controller itself , returned by modelAndView.getModel() in java spring framework

StackOverflow https://stackoverflow.com/questions/21778604

  •  11-10-2022
  •  | 
  •  

Question

Map modelMap = modelAndView.getModel();

I want to retrieve all the keys in the modelMap object , so that i can access each values , these values can be assigned to variables/objects created in the controller class itself

Was it helpful?

Solution

ModelMap subclasses LinkedHashMap which in turns extends HashMap

The generic type of ModelMap is fixed at Map<String, Object>

So you can iterate through the keys of a map as in HashMap, you can use keyset() or entryset()

for (Map.Entry<String, Object> entry : map.entrySet()) {
    System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
}

*

OTHER TIPS

The runtime class of Map returned by modelAndView.getModel() is org.springframework.ui.ModelMap .This class extends LinkedHashMap without changing the keyset() or entryset() methods.Just use keySet() or entrySet()

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top