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

Spring MVC Tutorial with ModelMap in Java JSP Spring MVC: How to Access ModelMap Values in a 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 Spring MVC: How to Access ModelMap Values in a JSP?

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.

If you enjoyed this post, help spread knowledge & subscribe to Crunchify's RSS feed.
twitter Spring MVC: How to Access ModelMap Values in a JSP?facebook Spring MVC: How to Access ModelMap Values in a JSP?google Spring MVC: How to Access ModelMap Values in a JSP?pinterest Spring MVC: How to Access ModelMap Values in a JSP?tumblr Spring MVC: How to Access ModelMap Values in a JSP?buffer Spring MVC: How to Access ModelMap Values in a JSP?

Enjoyed this post?

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

  • Protyay

    Thank You for such a wonderful tutorial

    • http://crunchify.com/ Arpit

      You are welcome Protyay.

  • Manjunath

    Manjunath

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

    • http://crunchify.com/ App Shah

      You are welcome.