Take the 2-minute tour ×

I'm adding an object to my ModelAndView in spring and forwarding to my jsp view. I need to access that object in my jquery. Is this possible without first putting the value in a hidden field? How is it done?

share|improve this question
add comment

2 Answers

up vote 9 down vote accepted
<script type="text/javascript">
   var modelAttributeValue = '${modelAttribute}';

This will resolve the model attribute added by model.addAttribute("modelAttribute", value)

share|improve this answer
hmm. this does not work when the jquery function is a part of a js file that is imported into the UI template –  user766488 May 23 '11 at 18:23
add comment

probably, you can save the model attribute in a hidden field and access it onload as below.

  var modelAttr = $("#modelAttr").val();

input type="hidden" id="modelAttr" name="modelAttr" value="${modelAttribute}"/>

Add c:out around the ${modelAttribute} in the jsp.

share|improve this answer
add comment

protected by Community May 6 at 15:29


Would you like to answer one of these unanswered questions instead?
