Java的arraylist到javascript的数组不起作用

时间:2015-12-30 11:19:45

标签: javascript java arrays arraylist

以下是我的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".

1 个答案:

答案 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

}