简体   繁体   中英

is this correct format for inserting JSTL in javascript?

I have JSP string array like

String[] valueArray = new String[valueList.size()];
valueArray = valueList.toArray(valueArray);

I want to use this in Javascript using JSTL

    var array = new Array();
 <c:forEach items="${valueArray}" var="item">
 array.push("${item}");
 </c:forEach>

somehow this does not work. it does not populated any values. Please help.

If the first set of codes are scriptlets in your jsp file, you should change your second set of codes as something like:

var array = new Array();
 <% for(int i=0; i<valueArray.length; i++){ %>
 array.push("<%= valueArray[i] %>");
 <% } %>

Or if they were a part of a java file (may be a servlet or an action class), you should add a line:

session.setAttribute("valueArray", valueArray);

This will add your valueArray in your session, ready to be retrieved on your jsp.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM