Java JSP / JSTL遍历对象2d布尔数组

时间:2019-03-17 00:16:10

标签: java arrays jsp servlets jstl

问题:

我想遍历“座位”类的数组。

“座位”代码:

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

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

下面显示的“ 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数组的值。每个数组的值都将被打印。

错误:

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;
}

1 个答案:

答案 0 :(得分:0)

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

此外,您可以通过a [1] [0]之类的索引来打印2d数组,以查看其中是否有任何值。