简体   繁体   中英

How to Set Java jsp Arrays to my Index.html Table

I am an engineering student. I have just started Java with the school. We have an exam next week. I have some questions about our preparing project. I'd be very happy IF you could patiently reply to them.

  1. Create a Java class called Lane with three fields: o days: a read-only string collection containing the names of days [ How to make a read-only string ] 车道类 主图

  2. How to put into my index file? ( Normally I learned like <% between those %> but get some error messages..

  3. How do I make reservation Array?

    reservations: a collection to store the reservations (eg a twodimensional string array or a list of string lists), where the number of rows equals the number of days, and the number of columns equals the number of times. The i. row - j. column cell's value is „available” if the i-th day's j-th time interval is still available, or the username of the reserving person otherwise.

  4. Last Question; How to Make such as below the picture.

    Reserved text if the lane is already reserved by someone else for that day and time (different username than the user's), oa Reserve link if the lane is available, oa Cancel link if the lane is reserved by this user.

4.问题

If I understand correctly you are stuck on creating the reservations table? This is the class I came up with that is pretty close to yours.

I'm not sure what you mean by read-only string, I just used constants ( final keyword)

We create a 2d array the same way we would a normal array (using [] ), except we do it twice. When using 2d arrays in Java the first value refers to rows an the second one to columns ( new String[number_of_rows][number_of_columns] ).

We than set up 3 possible values for ReservedState

We populate our array in the constructor

We also set up 3 methods we can use to change values in the table.

import java.util.Arrays;
public class Lane {
    final String[] days = {"Monday", "Tuesday", "..."};
    final String[] times = {"5-6 PM", "6-7 PM"};
    //this is where we create our reservations table
    String[][] reservations = new String[days.length][times.length];

    enum ReservedState {
        RESERVED("Reserved"),
        RESERVE("Reserve"),
        CANCEL("Cancel");

        private String name;

        ReservedState(final String name){
            this.name = name;
        }
    }

    //constructor
    public Lane(){
        //Fill the table with default values
        for (String[] row: reservations)
            Arrays.fill(row, ReservedState.RESERVE.name);
    }

    public void SetReservationToReserved(int dayIndex, int timeIndex){
        reservations[dayIndex][timeIndex] = ReservedState.RESERVED.name;
    }

    public void SetReservationToCancel(int dayIndex, int timeIndex){
        reservations[dayIndex][timeIndex] = ReservedState.CANCEL.name;
    }
    public void SetReservationToReserve(int dayIndex, int timeIndex){
    reservations[dayIndex][timeIndex] = ReservedState.RESERVE.name;
}
}

About Including your code in the index, It would be very useful if you could show us the error you are getting. I think you just forgot to instantiate your class before using it.

<%
    Lane lane = new Lane();
    //lane.SetReservationToReserved(1,1); Call this to set a reservation
    //lane.SetReservationToCancel(row number, column number); Call this to cancel a reservation
    //lane.SetReservationToReserve(1,1);...
 %>

As for rendering the table with html and jsp I would recommend this resource. It has several good examples on the topic. https://www3.ntu.edu.sg/home/ehchua/programming/java/JSPByExample.html

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