ModelMap model = new ModelMap(); model.addAttribute("message", "Hello World!"); String message = (String) model.get("message"); System.out.println(message); // prints "Hello World!"
ModelMap model = new ModelMap(); model.addAttribute("name", "John"); model.addAttribute("age", 30); String name = (String) model.get("name"); int age = (int) model.get("age"); System.out.println(name + " is " + age + " years old."); // prints "John is 30 years old."In this example, we create a new ModelMap object and add two attributes to it, "name" and "age", with the respective values "John" and 30. We then use the get() method to retrieve the values of the "name" and "age" attributes and store them in variables. We then print out a message that uses the variables to create a sentence that says "John is 30 years old.". Package Library: org.springframework.ui