0

我需要将一个数组列表从 jsp 传递给 javascript,然后在 javascript 中遍历该列表。有人可以帮助我处理 jsp 语法以及 javascript 迭代代码吗?

TIA

4

1 回答 1

1

试试这个代码:

ArrayList list = new ArrayList();
list.add("aaa");
list.add("bbbb");
list.add("cccc");
Iterator individualItems = list.iterator();
int i = 0;
String script = "<script type=\"text/javascript\">var list = new Array();";
while(individualItems.hasNext())
{
    script += "list["+i+"] = \""+individualItems.next()+"\";";
    i++;
}
script += "for(i=0;i<list.length;i++){document.writeln(list[i]);}";
script += "</script>";
//now do whatever you want with it
PrintWriter output = response.getWriter();
output.println(script);
于 2013-08-26T05:51:33.757 回答