[英]help with java for loop in jsp with jaxb
您好我正在尝试在 Java 中创建一个 for 循环:
<%
for (int i = 0; i < webList.size(); i++) {
WebBean WebBean = (WebBean) webList.get(i);
System.out.println (i);
//for (int x = 0; x < webList.size(); x++) {
out.println( "<h2>SouthEast Teams</h2> " );
//}
%>
我想做的是在索引 5 处拆分数组,以便它创建两个不同的列表。 我目前正在使用 XML 创建列表,它工作正常,除非我在第二个列表的标题中添加“东南团队”出现在“东北团队”标题上方 8 次,而不是在索引 5 的团队之后。
以下是我的完整 JSP 代码:
<%
ArrayList webList = (ArrayList) request
.getAttribute(ConstantKeys.WEB_LIST);
%>
<h2 tabindex="0" id="contentBody">NorthEast Teams</h2>
<%
if (webList != null) {
%>
<table>
<%
for (int i = 0; i < webList.size(); i++) {
WebBean WebBean = (WebBean) webList.get(i);
System.out.println (i);
//for (int x = 0; x < webList.size(); x++) {
out.println( "<h2>SouthEast Teams</h2> " );
//}
%>
<tr>
<td class="col1">
<div class="buttonWrap"
title="<%=WebBean.getTeamName()%>" class="button"><%=WebBean.getTeamName()%></a></div>
</td>
<td tabindex="0"><%=WebBean.getLocation()%></td>
</tr>
<%
}
%>
</table>
<%
}
%>
添加一个 if 语句,它将检查您在循环中迭代了多远。 (在这种情况下,您要检查 5)
编辑:添加一个变量,该变量将是“numberOfTeams”,这样您就可以在那里更改值,而不是在 if 语句中,以防将来发生更改。
<%
for (int i = 0; i < webList.size(); i++) {
WebBean WebBean = (WebBean) webList.get(i);
if(i == 5){
System.out.println (i);
//for (int x = 0; x < webList.size(); x++) {
out.println( "<h2>SouthEast Teams</h2> " );
//}
}
%>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.