0

I am using Spring3 MVC. I have a controller method which will return an object. Now how can i get that object and assign it to global variable?

SomeObject uiInfo = new SomeObject("data","123");
return new ModelAndView("SomePage", "uiInfo", uiInfo);

Now how can i get this model in JSP and assign it to global variable?

Thanks!

user755806
  • 6,565
  • 27
  • 106
  • 153
  • You can reference it in the JSP with `${uiInfo}`. I'm not sure what you mean by assign it to a global variable. – GriffeyDog Mar 19 '14 at 14:03
  • My requirement is, i need to access this uiInfo inside an external JS file which is embedded in the same jsp file. Is it possible? – user755806 Mar 19 '14 at 14:05

1 Answers1

0

Return your model object to a view in your jsp's.

In your JSP you can use jstl tag library; something ike this,

<c:forEach var="global" items="${uiInfo}">
value1:  ${uiInfo.data1} <br/>
value2: ${uiInfo.data2} <br/>
</c:forEach>
Nikhil
  • 1,166
  • 2
  • 17
  • 35
  • user, can i use this global variable in an external js which is embedded in same jsp? – user755806 Mar 19 '14 at 14:30
  • yes, you can use it if you have declared the variable globally and not inside a specific function – Nikhil Mar 19 '14 at 14:33
  • where should i declare value1 and value2 variables? – user755806 Mar 19 '14 at 14:35
  • value 1 and value 2 are just placeholders in your javascript object. you can give any name to it. It is just a name for your data inside the javascript object "global", you do not have to declare it. – Nikhil Mar 19 '14 at 14:41
  • now can i directly use value1 and value2 in external js? – user755806 Mar 19 '14 at 14:44
  • No, you will have to send the variable "global" to your external js file because value1 and value2 are inside the "global" object. – Nikhil Mar 19 '14 at 14:45
  • To understand what i mean by sending JSP object to javascript file - check this link "http://stackoverflow.com/questions/6577246/how-to-access-a-java-object-in-javascript-from-jsp" – Nikhil Mar 19 '14 at 14:49