java - 数据表不显示值

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

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

最佳答案

前 3 列的 outputText 标记中似乎存在错误。
开始标记上的 > 位置不正确。

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

更改为

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

然后再次检查。

关于java - 数据表不显示值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55373582/

相关文章:

java - 在对话框的 returnListener 中设置导航规则在 ADF 10g 中不起作用(JSF faces 1.1)

jsf - OSGI 和 JSF 如何协同工作?

c# - DataTable load() 约束错误

java - 如何在 URLConnection 中发送 cookie?

java - 拦截正在捕获的异常

jsf - JSF 中组合页面内的命令按钮

javascript - Angular Material Table 不会在初始加载时填充外部数据

javascript - 使用工厂作为 Angular 数据表中的源数据不起作用

java - Playframework 2.x Apache Tika 问题

java - Ajax 请求记录数据 Google Maps Animate ()