Table of Contents
In this post , we will see how to use ModelMap in Spring MVC.
Spring MVC tutorial:
- Spring MVC hello world example
- Spring MVC Hibernate MySQL example
- Spring MVC interceptor example
- Spring MVC angularjs example
- Spring MVC @RequestMapping example
- Spring Component,Service, Repository and Controller example
- Spring MVC @ModelAttribute annotation example
- Spring MVC @RestController annotation example
- Spring MultiActionController Example
- Spring MVC ModelMap
- Spring MVC file upload example
- Spring restful web service example
- Spring restful web service json example
- Spring Restful web services CRUD example
- Spring security hello world example
- Spring security cus
ModelMap object is used to pass multiple values from Spring MVC controller to JSP .
Some basics about Spring MVC ModelMap class:
- ModelMap class subclasses LinkedHashMap. It add some methods for convenience.
- It provides addAttribute method to put key value pair. This method return ModelMap objects that can be used for chaining.
- ModelMap uses
as generics. - ModelMap checks for null values.
Example:
Prerequisite:
You need to create spring mvc hello world example first.
Once you are done with spring mvc sample project. We need to add or modify following files.
- Make changes to HelloWorldController.java
- Modify index.jsp
- Modify hello.jsp
Thats all we need to do to demonstrate Spring MultiActionController example.
1)Â change HelloWorldController.java as below
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
package org.arpit.java2blog.springmvc.controller; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.RequestMapping; @Controller public class HelloWorldController { @RequestMapping("/helloworld") public String hello(ModelMap map) { String helloWorldMessage = "Hello world from java2blog!"; String welcomeMessage = "Welcome to java2blog!"; map.addAttribute("helloMessage", helloWorldMessage); map.addAttribute("welcomeMessage", welcomeMessage); return "hello"; } } |
2)Â Modify index.jsp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>HelloWorld</title> </head> <body> Click here to read hello and welcome message </body> </html> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Hello</title> </head> <body> ${helloMessage} <br/> ${welcomeMessage} </body> </html> |
4)Â It ‘s time to do maven build.
Right click on project -> Run as -> Maven build
5)Â Provide goals as clean install (given below) and click on run
Run the application
6)Â Right click on project -> run as -> run on server
Select apache tomcat and click on finish
7)Â You will see below screen:
When you click on  link, you will get below screen
We are done with Spring MVC ModelMap example. If you are still facing any issue, please comment.
Was this post helpful?
Let us know if this post was helpful. Feedbacks are monitored on daily basis. Please do provide feedback as that\'s the only way to improve.
message is not displaying in my case