0

I have a route object. It contains arrayList of options. I want to know the size of options and store it in some variable. (This thing needs to be done in JSP). I then want to access this variable in jquery. How to do it?

JSP code :

<%!int loopSize = routes.get(0).getOptions().size(); %>
  <c:forEach var="route" items="${routes}" varStatus="loopCounter">
    <c:forEach var="option" items="${route.options}" 
               varStatus="loopCounter2">
    <---more code -->
    </c:forEach> 
  </c:forEach>

But it is saying routes undefined. I am using struts2 and action class. Routes is coming from the action class

Bhushan Kawadkar
  • 28,279
  • 5
  • 35
  • 57
user3681970
  • 1,201
  • 4
  • 19
  • 37

1 Answers1

2

One solution may be to store the length of array in hidden field and access it in jquery.

<input type="hidden" id="loopSize" name= "loopSize" value= "${fn:length(routes[0].options)}"/>

And using the id you can accesss the value in jquery

user3681970
  • 1,201
  • 4
  • 19
  • 37
  • This really isn't the best answer. Yes it will work, but why (1) assign a variable to hidden input so that you can (2) retrieve the value of the hidden input via JavaScript and (3) assign it to a JavaScript variable when you could simply assign it to the variable. See my answer below as well as this [stackoverflow question](http://stackoverflow.com/questions/2431084/is-it-possible-to-use-jsp-variable-value-to-initialize-jquery-variable) and this [stackoverflow question](http://stackoverflow.com/questions/3287114/how-to-set-the-jstl-variable-value-in-javascript). – Shaggy Apr 24 '15 at 19:22