2012-08-09 42 views
1

我試圖執行Primefaces Showcase上可用的DataTable示例。所有功能都能正常工作,但是當我選擇一行時,所選行的值不顯示在我的<p:dialog>上。Primefaces數據表 - 複雜的例子:沒有選定的行上的值

我已經檢查了所有的替代品,沒有任何作品。有人能幫助我嗎?

我使用Primefaces 3.3和glassfish 3.0.1。這裏去我的代碼:

dataTableComplex.xhtml

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 

<html xmlns="http://www.w3.org/1999/xhtml" 
     xmlns:h="http://java.sun.com/jsf/html" 
     xmlns:p="http://primefaces.org/ui" 
     xmlns:f="http://java.sun.com/jsf/core"> 

    <h:head> 

    </h:head> 

    <body> 

     <h:form id="form"> 

      <p:dataTable var="car" value="#{tableBean.cars}" rowKey="#{car.model}" paginator="true" rows="10" 
         selection="#{tableBean.selectedCar}" selectionMode="single" id="carsTable"> 

       <p:ajax event="rowSelect" update=":form:display" oncomplete="carDialog.show()" /> 

       <f:facet name="header"> 
        List of Cars 
       </f:facet> 

       <p:column headerText="Model" sortBy="#{car.model}" filterBy="#{car.model}" id="model"> 
        #{car.model} 
       </p:column> 

       <p:column headerText="Year" sortBy="#{car.year}" filterBy="#{car.year}" id="year"> 
        #{car.year} 
       </p:column> 

       <p:column headerText="Manufacturer" sortBy="#{car.manufacturer}" filterBy="#{car.manufacturer}" id="manufacturer"> 
        #{car.manufacturer} 
       </p:column> 

       <p:column headerText="Color" sortBy="#{car.color}" filterBy="#{car.color}" id="color"> 
        #{car.color}" 
       </p:column> 

      </p:dataTable> 

      <p:dialog header="Car Detail" widgetVar="carDialog" resizable="false" 
         width="200" showEffect="explode" hideEffect="explode"> 

       <h:panelGrid id="display" columns="2" cellpadding="4">     

        <h:outputText value="Model:" /> 
        <h:outputText value="#{tableBean.selectedCar.model}" id="model"/> 

        <h:outputText value="Year:" /> 
        <h:outputText value="#{tableBean.selectedCar.year}" id="year"/> 

        <h:outputText value="Manufacturer:" /> 
        <h:outputText value="#{tableBean.selectedCar.manufacturer}" id="manufacturer"/> 

        <h:outputText value="Color:" /> 
        <h:outputText value="#{tableBean.selectedCar.color}" id="color"/> 
       </h:panelGrid> 
      </p:dialog> 
     </h:form>    
    </body> 

</html> 

TableBean.java

import java.io.Serializable; 
import java.util.ArrayList; 
import java.util.List; 
import java.util.UUID; 
import javax.enterprise.context.SessionScoped; 
import javax.faces.bean.ManagedBean; 
import javax.faces.bean.ViewScoped; 

@ViewScoped 
@ManagedBean(name = "tableBean") 
@SessionScoped 

public class TableBean implements Serializable { 

private final static String[] colors; 

private final static String[] manufacturers; 

static { 
    colors = new String[10]; 
    colors[0] = "Black"; 
    colors[1] = "White"; 
    colors[2] = "Green"; 
    colors[3] = "Red"; 
    colors[4] = "Blue"; 
    colors[5] = "Orange"; 
    colors[6] = "Silver"; 
    colors[7] = "Yellow"; 
    colors[8] = "Brown"; 
    colors[9] = "Maroon"; 

    manufacturers = new String[10]; 
    manufacturers[0] = "Mercedes"; 
    manufacturers[1] = "BMW"; 
    manufacturers[2] = "Volvo"; 
    manufacturers[3] = "Audi"; 
    manufacturers[4] = "Renault"; 
    manufacturers[5] = "Opel"; 
    manufacturers[6] = "Volkswagen"; 
    manufacturers[7] = "Chrysler"; 
    manufacturers[8] = "Ferrari"; 
    manufacturers[9] = "Ford"; 
} 

private List<Car> cars; 

private Car selectedCar; 

private Car[] selectedCars; 

public TableBean() { 
    cars = new ArrayList<Car>(); 

    populateRandomCars(cars, 50); 
} 

public Car getSelectedCar() { 
    return selectedCar; 
} 

public void setSelectedCar(Car selectedCar) { 
    this.selectedCar = selectedCar; 
} 

private void populateRandomCars(List<Car> list, int size) { 
    for(int i = 0 ; i < size ; i++) 
     list.add(new Car(getRandomModel(), getRandomYear(), getRandomManufacturer(), getRandomColor())); 
} 

private int getRandomYear() { 
    return (int) (Math.random() * 50 + 1960); 
} 

private String getRandomColor() { 
    return colors[(int) (Math.random() * 10)]; 
} 

private String getRandomManufacturer() { 
    return manufacturers[(int) (Math.random() * 10)]; 
} 

private String getRandomModel() { 
    return UUID.randomUUID().toString().substring(0, 8); 
} 

public List<Car> getCars() { 
    return cars; 
} 
} 

Car.java

import java.io.Serializable; 
import javax.enterprise.context.SessionScoped; 
import javax.faces.bean.ManagedBean; 

@ManagedBean(name = "car") 
@SessionScoped 

public class Car implements Serializable { 

private String model; 
private int year; 
private String manufacturer; 
private String color; 
private int price; 

public Car(){ 

} 
public Car(String model, int year, String manufacturer, String color) { 
      this.model = model; 
      this.year = year; 
      this.manufacturer = manufacturer; 
      this.color = color; 
    } 

    public Car(String model, int year, String manufacturer, String color, int price) { 
      this.model = model; 
      this.year = year; 
      this.manufacturer = manufacturer; 
      this.color = color; 
    this.price = price; 
    } 

    public String getModel() { 
      return model; 
    } 

    public void setModel(String model) { 
      this.model = model; 
    } 

    public int getYear() { 
      return year; 
    } 

    public void setYear(int year) { 
      this.year = year; 
    } 

    public String getManufacturer() { 
      return manufacturer; 
    } 

    public void setManufacturer(String manufacturer) { 
      this.manufacturer = manufacturer; 
    } 

    public String getColor() { 
      return color; 
    } 

    public void setColor(String color) { 
      this.color = color; 
    } 

public int getPrice() { 
    return price; 
} 

public void setPrice(int price) { 
    this.price = price; 
} 

    @Override 
    public boolean equals(Object obj) { 
      if(obj == null) 
        return false; 

      if(!(obj instanceof Car)) 
        return false; 

      Car compare = (Car) obj; 

      return compare.model.equals(this.model); 
    } 

    @Override 
    public int hashCode() { 
      int hash = 1; 

     return hash * 31 + model.hashCode(); 
    } 

@Override 
public String toString() { 
    return "Car{" + "model=" + model + ", year=" + year + ", manufacturer=" + manufacturer + ", color=" + color + ", price=" + price + '}'; 
} 
} 

編輯:要解決這個問題只需添加@ViewScoped上TableBean.java。

回答

1

您的TableBean應該是ViewScoped。

在TableBean的頂部添加@ViewScoped或使用faces-config.xml文件對其進行配置。

+0

你是對的!謝謝!這解決了我的問題:) @RinaldoPJr感謝您的耐心! – 2012-08-10 02:43:08

0

您使用的是Showcase Labs的示例,該示例在PrimeFaces 3.4版上運行(在發佈時仍在開發中)。

由於您使用PrimeFaces 3.3,您應該使用常見的Showcase中的示例。

請記住,Labs版本的Showcase始終運行在仍在開發的版本上,而常見的Showcase始終在最新的最終版本上運行。

您可以通過檢查頁面底部始終查看Showcase後面正在運行的版本。

+0

謝謝您的回答@ RinaldoPJr。 但是,DataTable代碼沒有區別。 Showcase Lab的DataTable具有相同的Showcase代碼。 即使如此,我使用常見的Showcase示例重新構建了代碼,問題仍然存在。 – 2012-08-09 20:20:27

+0

@JoãoAlmeida他們之間有很多不同之處。你能否用新代碼更新你的問題?而且,如果可能的話,複製/粘貼您的代碼,而不是Showcase中的代碼。順便說一下,CarDataModel並未用於複雜數據表示示例中,因此您不需要將其包含在項目中。 – RinaldoPJr 2012-08-09 20:34:52

+0

您是否還檢查過瀏覽器控制檯(大多數瀏覽器中的F12)是否沒有JavaScript錯誤消息,並且IDE控制檯上沒有JSF錯誤消息? – RinaldoPJr 2012-08-09 20:39:57

0

由於類汽車只是一個普通類,而不是一個管理的bean,你不需要聲明:

@ManagedBean(name = 「汽車」) @SessionScoped