Spring MVC: How to Access ModelMap Values in a JSP?

Spring MVC Tutorial with ModelMap in Java - JSP

Do you have one of below questions?

  • How do I access ModelMap in a JSP?
  • My ModelMap is not passing beans to JSP.
  • How can I pass multiple values from Spring Controller to JSP?
  • in JSP how to get multiple values from Spring MVC Controller?

Here is a simple working solution for all of above questions.

Pre-requirement: Hello World Example – Spring MVC 3.2.1

Please go through above example which has all detailed steps on how to setup your 1st Hello World Spring MVC example.. Once you are done with that we will modify below two files to see how ModelMap works.

  • welcome.jsp
  • CrunchifyHelloWorld.java

Here is a code we need to change for this example.

And you are all set..

Re-Deploy project to Tomcat and visit this URL: http://localhost:8080/CrunchifySpringMVC3.2.1/welcome.html

Spring MVC Example by Crunchify

ModelMap subclasses LinkedHashMap, and provides some additional conveniences to make it a bit easier to use by controllers

  • addAttribute can be called with just a value, and the map key is then inferred from the type.
  • The addAttribute methods all return the ModelMap, so you can chain method called together, e.g. modelMap.addAttribute('x', x).addAttribute('y',y)
  • The addAttribute methods checks that the values aren’t null
  • The generic type of ModelMap is fixed at Map<String, Object>, which is the only one that makes sense for a view model.

So nothing earth-shattering, but enough to make it a bit nicer than a raw Map. Spring will let you use either one.

You can also use the Model interface, which provides nothing other than the addAttributemethods, and is implemented by the ExtendedModelMap class which itself adds further conveniences. ref.

Have anything to add to this article? Please chime in and join the conversion.

Enjoyed this post?

Be sure to subscribe to the Crunchify newsletter and get regular updates about awesome posts just like this one and more! Join more than 13000 subscribers!

  • Protyay

    Thank You for such a wonderful tutorial

    • http://crunchify.com/ App Shah

      You are welcome Protyay.

  • Manjunath


    Great Article for beginners. It is very useful.
    Thanks for sharing your Knowledge.

    • http://crunchify.com/ App Shah

      You are welcome.