Search code examples
javaspringmodelandview

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


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


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());
    }
    

    *