簡體   English   中英

幫助 java 循環在 jsp 和 jaxb

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM