0

I am trying to put array from JSTL for loop to an array in JS

<c:forEach items="${defaultWishList}" var="eachItem">
    ${eachItem.getItem().getId()};
    ${eachItem.getItem().getName()};
</c:forEach>

and put each row in :

var ar = new Array();

Now I tried the following way which is not working

<script type="text/javascript">
var ar = new Array();
var aaa="";
var bbb="";
<c:forEach items="${defaultWishList}" var="eachItem">
    aaa = ""+${eachItem.getItem().getId()};
    bbb = ""+${eachItem.getItem().getName()};
    ar.push({
    F : aaa,
    L : bbb
    });
</c:forEach>
</script>

Is it possible to do this. If yes then how??

2
  • 1
    If course it's possible, JSP is just a template. That said, why bother doing it like this? Expose the value as json, and do that conversion outside of the view. Commented Feb 18, 2014 at 11:21
  • how to do that? could you pleas write me the code?? Commented Feb 18, 2014 at 11:22

1 Answer 1

1

Something like this should work - you have to access the JSTL object using its syntax - i.e ${eachItem.item.id} and not ${eachItem.getItem().getId()}

<script type="text/javascript">

var ar = new Array();

<c:forEach items="${defaultWishList}" var="eachItem">
    ar.push({
    F : '${eachItem.item.id}',
    L : '${eachItem.item.name}'
    });
</c:forEach>
</script>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.