同样生成一个Table
<table id="checkTable" align="center" cellpadding="5" cellspacing="0" border="0" width="100%">
<tbody>
<logic:iterate id="item" name="list" type="com.worthtech.app.bean.User" indexId="
index">
<%
//int index=((java.lang.Integer)pageContext.findAttribute("index")).intValue();//自增的索引,相当于for里面的i,logic:iterate里用起来比较麻烦,红色部分必须一致
if((
index%2)==0){//斑马线
%>
<tr class="SelcetOff">
<%}else{%>
<tr class="SelcetOn">
<%}%>
<td><div align="center"><bean:write name="item" property="userName"/></div></td>
<td><div align="center"><bean:write name="item" property="address"/></div></td>
</tr>
</logic:iterate>
</tbody>
</table>
<table id="checkTable" align="center" cellpadding="5" cellspacing="0" border="0" width="100%">
<tbody>
<%
if (request.getAttribute("list") != null) {
java.util.List l = (java.util.List) request.getAttribute("list");
for (int i = 0; i < l.size(); i++) {
com.worthtech.app.bean.User user = (com.worthtech.app.bean.User) l.get(i);
if(i%2==0){//斑马线
out.print("<tr class='SelcetOff'>");
}else{
out.print("<tr class='SelcetOn'>");
}
out.print("<td><div align='center'>"+ user.getUserName() + "</div></td>");
out.print("<td><div align='center'>"+ user.getAddress() + "</div></td>");
out.print("</tr>");
}
}
%>
</tbody>
</table>
用for循环比较自由度大,方便。比如要对user.getAddress()进行操作,可以out.print("<td><div align='center'>"+
Util.parse(user.getAddress()) + "</div></td>");
如果使用<logic:iterate>里的<bean:write name="item" property="
address"/>就必须这样:
<bean:define id="var" name="item" property="
address" type="java.lang.String"/>
<% java.lang.String address=var;%>
<td><div align="center"><%=Util.parse(address) %></div></td>
这样显得很麻烦。
总结:使用<logic:iterate>灵活度不高