传递值并在javascript中迭代arraylist

时间:2013-08-26 05:13:03

标签: javascript jsp

我需要将一个arraylist从jsp传递给javascript,然后在javascript中遍历列表。有人可以帮助我使用jsp synatax以及javascript迭代代码吗?

TIA

1 个答案:

答案 0 :(得分: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);