简体   繁体   中英

Java JSP/JSTL Loop through objects 2d boolean array

Problem:

I want to iterate through the array of the "Seating" class.

"Seating" Code:

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

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

An instance of "Seating" is created by the "EventBooking" class shown below.

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

And the .jsp that is supposed to be looping through these values.

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

I am trying to send my "Seating" object to the .jsp . The .jsp will then loop through the values of Seating's 2D array. Each of the array's values will be printed.

Error:

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

And line 8.

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

Any help is appreciated.

Edit Full "Seating" code

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

Please check this and correct the innermost loop output - Two dimensional arraylist with c:foreach jstl tag

Moreover, you can print 2d array via index such as a[1][0] to see whether you got any value present in them.

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