日期:2014-05-17  浏览次数:20715 次

javascript遍历struts2传过来的List集合(急)
问题如下:
  struts2提供到页面一个List结合,比如valueList
  想在javascript中遍历处其内容,请问怎么写?最好有代码,谢谢!
  foreach和for循环都试过了,不行

------解决方案--------------------
Java code
        <logic:notEmpty property="dataList1" name="XY0201Form">
            <bean:define  name="XY0201Form" property="dataList1" id="sData" type="java.util.List" /> 
        <%
            List<Map<String, List<String>>> data = sData;
            Map<String, List<String>> map = new HashMap<String, List<String>>();
            for(int i=0;i<data.size();i++){
                map = data.get(i);
                Object key = map.keySet().toArray()[0];
                List<String> value = map.get(key);
        %>
                document.getElementById("zg1_<%=key%>").className="zg_<%=key%> tooltips";
                var span_1 = "<div>"; 
        <%
                for(int j=0;j<value.size();j++){
                    String o = value.get(j);
        %>
                span_1 += "<p><%=o%>人</p>";
        <%
                }
        %>
                span_1 += "</div>";        
                document.getElementById("zg_<%=key%>").innerHTML=span_1;
        <%
            }
        %>
        </logic:notEmpty>

------解决方案--------------------
先要转换成javascript能够识别的数据类型
才能够遍历
------解决方案--------------------
List<BaseUser> list = this.userMng.findUserList(orgId);
JSONArray arr = new JSONArray();
JSONObject o;
for(BaseUser user : list){
o = new JSONObject();
o.put("id", user.getId());
o.put("name", user.getUsername());
arr.put(o);
}

将arr.toString()通过response返回到页面:
response.setContentType(contentType);
response.setHeader("Pragma", "No-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
try {
response.getWriter().write(text);
} catch (IOException e) {
e.printStackTrace();
}

页面遍历json:
$.getJSON("../group/group_loadUser.do?orgId="+orgId, function(json){
for(var i=0;i<json.length;i++){
alert("id:" + json[i].id + " name:" + json[i].name);
}
}); 



------解决方案--------------------
response.setContentType(contentType);
改为:
response.setContentType("text/xml;charset=UTF-8");

response.getWriter().write(text);
改为:
response.getWriter().write(arr.toString());