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 Spring MVC Tutorial
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.
- CrunchifyHelloWorld.java
- welcome.jsp
Here is a code we need to change for this example.
Step-1. Update CrunchifyHelloWorld.java
package com.crunchify.controller; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; /** * @author Crunchify.com * */ @Controller @RequestMapping("/welcome") public class CrunchifyHelloWorld { @RequestMapping(method = RequestMethod.GET) public String printWelcome(ModelMap model) { String heading = "Spring MVC: How to Access ModelMap Values in a JSP?"; String result1 = "Hey.. I'm result1.. You are seeing me on welcome page."; String result2 = "Hey.. I'm result2.. "; String credit = "Demo by <a href='https://crunchify.com'>Crunchify</a>. Click <a href='https://crunchify.com/category/java-tutorials/'>here</a> for more than 350 Java Examples."; // you can add any Collection Objects to ModelMap // including JSON, String, Array, Map, List, etc... model.addAttribute("heading", heading); model.addAttribute("result1", result1); model.addAttribute("result2", result2); model.addAttribute("credit", credit); return "welcome"; } }
Step-2. Update welcome.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <style type="text/css"> body { background-image: url('https://crunchify.com/bg.png'); } </style> <title>Spring MVC: How to Access ModelMap Values in a JSP? - Crunchify.com</title> <meta rel="author" href="https://crunchify.com"> </head> <body> <div align="center"> <br> <h1>${heading}</h1> <h2>${result1}</h2> <br> <h3>${result2}</h3> <br> <br> ${credit} </div> </body> </html>
And you are all set..
Step-3. Re-Deploy project
Redeploy project CrunchifySpringMVCTutorial
on Tomcat Server again.
Visit URL: http://localhost:8080/CrunchifySpringMVCTutorial/welcome.html
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 theModelMap
, 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 atMap<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 addAttribute
methods, and is implemented by the ExtendedModelMap
class which itself adds further conveniences.