1

Need your help. I am not able to get json string in jsp after adding in modelAndView. After debugging, I found it's get added in modelAndView instace.

Below is the code snippet:

Controller:

modelAndView.addObject("json-data",jsonhelper.getJSONString(viewData));

JSP

<c:if test="${json-data != null}">

<script type="text/javascript">

     window.jsonData =${json-data};

 </script>

</c:if>

Here viewData is the object, that I need to get in jsp, but in jsp it's giving 0.

ved
  • 909
  • 2
  • 17
  • 43

1 Answers1

3

The dash (-) in json-data is being interpreted as the arithmetic operator minus.

According to the spec (1.7.1):

Binary operators - A {+,-,*} B

If A and B are null, return (Long)0

Therefore json-data is resolved to 0, json-data != null yields true, and window.jsonData is assigned the value 0.

One solution is to rename your variable to jsonData or any other valid Java identifier.

Alternatively, this will also work:

window.jsonData = <%= request.getAttribute("json-data") %>
Community
  • 1
  • 1
Bewusstsein
  • 1,591
  • 13
  • 19
  • I got that, but is it mentioned somewhere in documentation – ved May 05 '15 at 06:34
  • @ved I looked and found that my first answer wasnt quite correct, see edited answer for clarification and reference to the EL spec. – Bewusstsein May 05 '15 at 06:59
  • link you have mentioned is not working. Please give me valid link. – ved May 05 '15 at 07:03
  • @ved the link should work now; if not, try this one: https://cwiki.apache.org/confluence/download/attachments/7045154/UEL.pdf or search for "jsp expression language specification type:pdf" – Bewusstsein May 05 '15 at 07:16