通常我们通过查询数据库获取到的数据最终会通过页面显示给用户,通常我们会将这些数据存放在list集合中使用,这里有多种方法来迭代显示数据
首先,我们需要在Action中将这个list集合存储起来,通过setAttribute()方法或放到session中或放到request中
1、在jsp页面中写java代码,遍历输出集合中的对象信息,如下
<%
List list = (List)request.getAttribute("list");
for(int j=0;j<list.size();j++)
{
out.print("<tr><td>"+j+"</td>");
out.print("<td>");
out.print(((TestActionForm)list.get(j)).getSoruser());
out.print("</td><td>");
out.println(((TestActionForm)list.get(j)).getNum());
out.print("</td></tr>");
}
%>
说明:这种方法虽然看起来可行,并且容易理解,但是日后很难维护,建议不要使用
2、使用struts中的<logic:iterate>标签,如下
<logic:iterate id="form" name="list" type=" example.TestActionForm ">
<tr><td width="50%">
name: <bean:write name="form" property="soruser"/>
<td/><td width="50%">
password: <bean:write name="form" property="num"/>
</td></tr>
</logic:iterate>
=========说明===========================
其中,id为一个bean的名字,同下面bean的name属性,用于输出信息
name为要遍历的对象的名字,对应于前面setAttribute("list",arraylist);中的list;
type为list集合中的对象的位置,我这里也就是类TestActionForm所在的位置
至于bean中的属性就不在多说了
=============================================================================
新人,写的不好大家请提出来,共同学习,提高!!!!!!!!!!!!!!
=============================================================================