Java JSP / JSTL循环对象2d布尔数组

问题描述 投票:0回答:1

Problem:

我想迭代“座位”类的数组。

"Seating" Code:

public class Seating {
private int nRow, nCol;
private boolean[][] seats;

public Seating () {
    nRow = 8;
    nCol = 8;
    seats = new boolean[nRow][nCol];
}

“Seating”的实例由下面显示的“EventBooking”类创建。

PrintWriter out = response.getWriter();
    seats = new Seating(8,8);

    seats.setSeatStatus(0,1);
    seats.setSeatStatus(1,1);

    request.setAttribute("seats", seats.seats);
    RequestDispatcher req = request.getRequestDispatcher("/index.jsp");
    req.forward(request,response);

和.jsp应该循环遍历这些值。

<c:forEach var = "row" items = "${seats}">
    <c:forEach var = "col" items = "${row}">
        <c:out value = "${seats}"/>
    </c:forEach>
</c:forEach>

我正在尝试将我的“座位”对象发送到.jsp。然后.jsp将遍历Seating的2D数组的值。将打印每个数组的值。

Error:

org.apache.jasper.JasperException: An exception occurred processing [/index.jsp] at line [8]

第8行。

8:  <c:forEach var = "row" items = "${seats}">

任何帮助表示赞赏。

编辑完整的“座位”代码

public class Seating {
private int nRow, nCol;
public boolean[][] seats;

public Seating () {
    nRow = 8;
    nCol = 8;
    seats = new boolean[nRow][nCol];
}

public Seating (int row, int col) {
    nRow = row;
    nCol = col;
    seats = new boolean[row][col];
}

public boolean seatStatus (int row, int col) {
    return seats[row][col];
}

public void setSeatStatus (int row, int col) {
    if (seats[row][col] == false) {
        seats[row][col] = true;
    }
    else if (seats[row][col] == true) {
        seats[row][col] = false;
    }
    //else
        //Error
}

public int getRowLength () {
    return nRow;
}

public int getColLength () {
    return nCol;
}
java arrays jsp servlets jstl
1个回答
0
投票

请检查并纠正最里面的循环输出 - Two dimensional arraylist with c:foreach jstl tag

此外,您可以通过索引(例如[1] [0])打印2d数组,以查看它们中是否存在任何值。

© www.soinside.com 2019 - 2024. All rights reserved.