Search code examples
javajsfdatatablemanaged-bean

Datatable does not show values


I have a list of Hotel objects and I want to display the values of each object in a jsf datatable. The problem I am having is that although the table creates a row for each hotel object, the values are not shown.

datatable

This is my Hotel class:

public class Hotel {
private int id;
private String hotelname;
private String address;
private String information;
private int number_rooms_total;
private String pathimage;

public Hotel() {
}

public Hotel(int id, String hotelname, String address, String information, int number_rooms_total, String pathimage) {
    this.id = id;
    this.hotelname = hotelname;
    this.address = address;
    this.information = information;
    this.number_rooms_total = number_rooms_total;
    this.pathimage = pathimage;
}

public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public String getHotelname() {
    return hotelname;
}

public void setHotelname(String hotelname) {
    this.hotelname = hotelname;
}

public String getAddress() {
    return address;
}

public void setAddress(String address) {
    this.address = address;
}

public String getInformation() {
    return information;
}

public void setInformation(String information) {
    this.information = information;
}

public int getNumber_rooms_total() {
    return number_rooms_total;
}

public void setNumber_rooms_total(int number_rooms_total) {
    this.number_rooms_total = number_rooms_total;
}

public String getPathimage() {
    return pathimage;
}

public void setPathimage(String pathimage) {
    this.pathimage = pathimage;
}



@Override
public int hashCode() {
    int hash = 3;
    hash = 83 * hash + this.id;
    return hash;
}

@Override
public boolean equals(Object obj) {
    if (this == obj) {
        return true;
    }
    if (obj == null) {
        return false;
    }
    if (getClass() != obj.getClass()) {
        return false;
    }
    final Hotel other = (Hotel) obj;
    if (this.id != other.id) {
        return false;
    }
    return true;
}

@Override
public String toString() {
    return "Hotel{" + "id=" + id + ", hotelname=" + hotelname + ", address=" + address + ", information=" + information + ", number_rooms_total=" + number_rooms_total + ", image=" + pathimage + '}';
  }
}

This is my JSF Page:

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 <html xmlns="http://www.w3.org/1999/xhtml"
   xmlns:h="http://xmlns.jcp.org/jsf/html"
   xmlns:f="http://xmlns.jcp.org/jsf/core">
 <h:head>
    <title>Facelet Title</title>
</h:head>
<h:body>
    <h2>List Hotels</h2>
    <legend>Hotels Form</legend>
    <h:form>
        <h:dataTable value="#{listHotels.hotels}" var="hotel" border="3">
            <h:column>
                <f:facet name="header">ID Hotel</f:facet>
                <h:outputText> value="#{hotel.id}"</h:outputText>
            </h:column>
            <h:column>
                <f:facet name="header">Name</f:facet>
                <h:outputText> value="#{hotel.hotelname}"</h:outputText>
            </h:column>
            <h:column>
                <f:facet name="header">Address</f:facet>
                <h:outputText> value="#{hotel.address}"</h:outputText>
            </h:column>
            <h:column>
                <f:facet name="header">Image</f:facet>
                <h:graphicImage value="#{hotel.pathimage}" />
            </h:column>
            <h:column>
                <f:facet name="header">New Booking</f:facet>
                <h:commandButton value="NewBooking" action="#{listHotels.newBooking(hotel)}"></h:commandButton>
            </h:column>
            <h:column>
                <f:facet name="header">List Booking</f:facet>
                <h:commandButton value="ListBooking" action="#{listHotels.listBookings(hotel)}"></h:commandButton>
            </h:column>
        </h:dataTable>
        <div id="message">
            == #{listHotels.message} ==
        </div>  
    </h:form>
</h:body>

And this my Bean Class:

@ManagedBean
@SessionScoped
public class ListHotels implements Serializable {

private Database db = null;
private String message = null;
private ArrayList<Hotel> hotels = new ArrayList<>();
private Hotel currHotel;

public ListHotels() {
    try {
        db = Database.getInstance();
    } catch (Exception ex) {
        System.out.println("ex: " + ex.getMessage());
    }
}

public ArrayList<Hotel> getHotels() {
    System.out.println("hallofff");

    hotels.clear();
    try {
        hotels = db.getAllHotels();
        System.out.println(hotels);
        message = "all hotels listed";
        System.out.println("message  " + message);
    } catch (Exception ex) {
        message = "ex :" + ex.getMessage();
    }

    return hotels;
}

public void setHotels(ArrayList<Hotel> hotels) {
    this.hotels = hotels;
}

public String newBooking(Hotel h) {
    String retUrl = "NewBooking";
    currHotel = h;
    return retUrl;
}

public String listBookings(Hotel h) {
    String retUrl = "ListBookings";
    currHotel = h;
    return retUrl;
}

public String getMessage() {
    return message;
}

public void setMessage(String message) {
    this.message = message;
}

public Hotel getCurrHotel() {
    return currHotel;
}

 public void setCurrHotel(Hotel currHotel) {
    this.currHotel = currHotel;
 }
}

The method hotels = db.getAllHotels(); returns all hotels from my sql database. I already checked and the values inside my arraylist are correct. So I don't really know why I am having the problem.... It seems so simple yet inpossible.


Solution

  • It would seem that you have an error in your outputText tags for the 3 first columns.
    An incorrect placement of the > on the opening tag.

    <h:outputText> value="#{hotel.id}"</h:outputText>
    

    Change it to

    <h:outputText value="#{hotel.id}" />
    

    and check again.