以下是我的jsp文件中将Java的ArrayList
转换为JavaScript的Array
的代码:
<% for(int k=0;k<listDate.size();k++){%>
var temp =<%=listDate.get(k)%>
dates[<%=k%>] = temp;
<%}%>
document.write(dates[0]); // it prints out a totally unrelated value which I have no idea what it is "1974".
答案 0 :(得分:0)
这只是为您提供如何更新代码和使用scriptlet结果的参考 -
<%
String hiddenResult = "";
// DO your business logic here and assign value to the hiddenResult
<% for(int k=0;k<listDate.size();k++){%>
hiddenResult = hiddenResult + listDate.get(k)+",";
hiddenResult = hiddenResult.substring(0,hiddenResult.length()-1);
%>
Assign the scriptlet result in the hidden field of the form and use that hidden field like this
<input type="hidden" id="hiddenInputResult" name="hiddenDDResult" value="<%=hiddenResult%>" />
<!--assign the value here in hidden field -->
function myFunction(){
var arrayValue = document.getElementById("hiddenInputResult").value;
// you can split by using arrayValue.split(","); to use
}