数据表不显示值

时间:2019-03-27 09:19:13

标签: java jsf datatable managed-bean

我有一个Hotel对象的列表,我想在jsf数据表中显示每个对象的值。我遇到的问题是,尽管表为每个酒店对象创建了一行,但未显示值。

datatable

这是我的酒店班级:

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

这是我的JSF页面:

<?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>

这是我的Bean类:

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

方法hotel = db.getAllHotels();从我的sql数据库返回所有酒店。我已经检查过,并且我的arraylist中的值正确。所以我真的不知道为什么会有这个问题。。。似乎很简单,但不可能。

1 个答案:

答案 0 :(得分:0)

前三列的outputText标记中似乎有一个错误。
>在开始标签上的位置不正确。

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

将其更改为

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

然后再次检查。