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.
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 result1 = "Hey.. I'm result1.. You can seeing me on welcome page"; String result2 = "Hey.. I'm result2.. "; String credit = "Demo by <a href='http://crunchify.com'>Crunchify</a>. Click <a href='http://crunchify.com/category/java-web-development-tutorial/'>here</a> for more than 150 examples."; // you can add any Collection Objects to ModelMap // including JSON, String, Array, Map, List, etc... model.addAttribute("result1", result1); model.addAttribute("result2", result2); model.addAttribute("credit", credit); return "welcome"; } }
<%@ 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> <title>Spring 3.2.1 MVC Example: Hello World - Crunchify.com</title> <meta rel="author" href="http://Crunchify.com"> </head> <body> <div align="center"> <h1>{result1}</h1> <br> <h2>${result2}</h2> <br> <br> ${credit} </div> </body> </html>
And you are all set..
Re-Deploy project to Tomcat and visit this URL: http://localhost:8080/CrunchifySpringMVC3.2.1/welcome.html
ModelMapsubclasses
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
Modelinterface, which provides nothing other than the
addAttributemethods, and is implemented by the
ExtendedModelMapclass which itself adds further conveniences.
The post Spring MVC: How to Access ModelMap Values in a JSP? appeared first on Crunchify.