Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert jstl/JSP variables to javascript variables? [duplicate]

Is it possible to convert jstl/JSP variables to javascript variables?

Here is the JSTL:

 <c:forEach var="responseString" items = "${responseString}">
     <c:out value="${responseString.response}" />  
 </c:forEach>

I want to transfer the value inside ${responseString.response} to a javascript var response

HERE is the javascript function:

directionsService.route(request, function(response, status)

I tried this tutorial;

var response = {
<c:forEach var="responseString" items = "${responseString}">
    <c:out value="${responseString.response}" />
</c:forEach>
}

Obviously it didn't work, I kind of screwed it up, please help.

Added info, ${responseString.response} is actually JSON, I just converted it to String:

{"routes": [{"bounds" : {"northeast":{"lat":14.650,"lng":121.050610}, "status":"OK"}

It's a deeply nested JSON.

like image 858
PHil Avatar asked Jun 06 '26 04:06

PHil


1 Answers

Do something like this:

  var availableTags = [
  <c:forEach items="${vendorMap}" var="vendor" varStatus="vendorStatus">
     '<c:out value="${vendor}" />'
     <c:if test="${!vendorStatus.last}">    
     ,    
     </c:if> 
  </c:forEach>
 ];

Note that you should not add "," for the last entry and also single quotes

like image 175
Shweta Avatar answered Jun 07 '26 18:06

Shweta



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!