Reputation: 13334
When using ModelandView
constructor with two parameters - String viewName, Map model
- to pass multiple parameters to a view JSP
, how do I then retrieve them?
If they are retrieved like map values, then what is the map name?
Let say, my controller code has the following:
Map<String, Object> myModel = new HashMap<String, Object>();
myModel.put("1", "one");
myModel.put("2", "two");
return new ModelAndView("view", myModel);
What do I need to put into JSP
so "one" and "two" appear on the page?
I'm using Java EE 7, Spring 3.2.4, GlassFish 4 server
UPDATE:
SOLVED (See the accepted answer).
Since this post continues to receive downvotes I would like to point out that I posted what I unsuccessfully tried and what actually worked in my response to the accepted answer below on the same day the question was posted and answered.
Upvotes: 2
Views: 10654
Reputation: 3724
If you really want to put "1"
as model
key, you can retrieve the value like this
<%out.print(request.getAttribute("1"));%>
${requestScope['1']}
Note that put "1"
as model
key is really a bad idea.
Upvotes: 2
Reputation: 11757
The model objects will be exposed using their keys:
Map<String, Object> myModel = new HashMap<String, Object>();
myModel.put("myVar", "myValue");
return new ModelAndView("view", myModel);
In the JSP file:
...
<p>${myVar}</p>
...
I am not sure if it would work with your given variable names (1
and 2
). But I would recommend to use real good names for your variable, as in your Java code.
Note that you could create the ModelAndView
without to have to create a Map
like this:
ModelAndView modelAndView = new ModelAndView("view");
modelAndView.add("myVar", "myValue");
return modelAndView;
Upvotes: 3