数据表不显示值

问题描述 投票:1回答:1

我有一个酒店对象列表,我想在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;
 }
}

方法hotels = db.getAllHotels();从我的SQL数据库返回所有酒店。我已经检查过,我的arraylist中的值是正确的。所以我真的不知道为什么我会遇到这个问题....它看起来很简单但不可能。

java jsf datatable managed-bean
1个回答
0
投票

看起来你的outputText标签中有3个第一列的错误。 >在开始标记上的错误放置。

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

将其更改为

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

并再次检查。

© www.soinside.com 2019 - 2024. All rights reserved.