简体   繁体   中英

help with java for loop in jsp with jaxb

Hi I am trying to create a for loop in Java:

<%
            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> " );
                            //}


    %>

What I would like to do is split up the array at index 5 so that It creates two different list. I am currently using XML to create the list and it works okay except when I added in the heading for the second list "southeast teams" is appearing 8 times above the "northeast teams" heading and not after the team at index 5.

Below is my full JSP code:

<%
    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>

<%
    } 
%>

Add A if statement that will check the how far you have iterated through the loop. (in this case you want to check 5)
edit: Add a variable that will be the "numberOfTeams" so you can change the value there and not in the if statement in case of future changes.

     <%
                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> " );
                                    //}
                                }

        %>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM