在action方法在session中保存一个list集合变量,集合中的每一个元素是对象,现在要在jsp页面中访问集合中的每个对象,输出对象中的属性值,代码为:var length ='<%=(String)(request.getSession()).getAttribute(AttributeKeys.gymblist).size()
>';         
for (k=0; k<length;k++) {
oOption = document.createElement("option");
//便利取每一个的值
oOption.text='<%=(String)(request.getSession()).getAttribute(AttributeKeys.gymblist).get("+k+").getMbmc()%>';
oOption.value='<%=(String)(request.getSession()).getAttribute(AttributeKeys.gymblist).get("+k+").getInfoStr()%>';
//添加到select下拉框中
obj1.add(oOption);
}
这种写法有啥不对??请高手指教

解决方案 »

  1.   

    Object temp = request.getAttribute(AttributeKeys.gymblist);
    if(temp!=null){
    List list = (List)temp;
    for(Iterator it = list.iterator();it.hasNext();){
    你list里的对象 bean = (你list里的对象)it.next();
    oOption = document.createElement("option"); 
    //便利取每一个的值 
    oOption.text=' <%=bean.getMbmc()%>'; 
    oOption.value=' <%=bean.getInfoStr()%>'; 
    //添加到select下拉框中 
    obj1.add(oOption); 

    是不是list中的对象必须实例化啊?
    我猜的~
    你试试这样行不?
      

  2.   

    oOption.text=' <%=(String)(request.getSession()).getAttribute(AttributeKeys.gymblist).get("+k+").getMbmc()%>'; 
    你最好先把它从集合中取出来强转成对象,然后在通过get取值,就像2楼说的那样
    因为你连写的语句里面并没有指明对象的具体类型,可能没有识别,所以取不出值,
    你也可以用alert(<%=(String)(request.getSession()).getAttribute(AttributeKeys.gymblist).get("+k+").getMbmc()%>);
    把值打出来看看有没有!