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.
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
CrunchifyHelloWorld.java
Java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
packagecom.crunchify.controller;
importorg.springframework.stereotype.Controller;
importorg.springframework.ui.ModelMap;
importorg.springframework.web.bind.annotation.RequestMapping;
importorg.springframework.web.bind.annotation.RequestMethod;
/**
* @author Crunchify.com
*
*/
@Controller
@RequestMapping("/welcome")
publicclassCrunchifyHelloWorld{
@RequestMapping(method=RequestMethod.GET)
publicStringprintWelcome(ModelMapmodel){
Stringheading="Spring MVC: How to Access ModelMap Values in a JSP?";
Stringresult1="Hey.. I'm result1.. You are seeing me on welcome page.";
Stringresult2="Hey.. I'm result2.. ";
Stringcredit="Demo by Crunchify. Click here 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
welcome.jsp
XHTML
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
pageEncoding="ISO-8859-1"%>
body{
background-image:url('https://cdn.crunchify.com/bg.png');
}
Spring MVC: How to Access ModelMap Values in a JSP? -Crunchify.com
${heading}
{result1}
${result2}
${credit}