Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to access a ModelAndView object within the jsp page

Tags:

I have a code like this in my controller.

ModelAndView result = new ModelAndView("person/sometrends");   result.addObject("data", data);  // data -> Map 

In my JSP page I have written some Java code that is dependent on the data object. How can I get that data object within the JSP code?

I tried something like this (pseudo code).

<%     Map x = ${data}; %> 

But that doesn't work. Not sure how to access it.

like image 613
varun Avatar asked Sep 17 '10 17:09

varun


1 Answers

In a scriptlet, you use the request object like so:

<% Map myMap = (Map) request.getAttribute("data"); %>

If you want to use something like JSTL instead of a scriptlet, you could do:

<c:forEach var="entry" items="${data}"> Name:  ${entry.key} <br/> Value: ${entry.value} <br/> </c:forEach> 

As Davemeister and novice pointed out, combining EL inside a scriptlet was the root cause of your error.

like image 62
codelark Avatar answered Oct 18 '22 20:10

codelark