当前位置: 动力学知识库 > 问答 > 编程问答 >

jsf 2 - Primefaces DataTable - Complex example: No value on selected row

问题描述:

I'm trying execute the DataTable example avaiable on Primefaces Showcase. All functions works but when i select a row, the value of the selected row isn't displayed on my <p:dialog>.

I've already checked all alternatives and nothing works. Could someone help me?

I'm using Primefaces 3.3 and glassfish 3.0.1. Here goes my code:

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

}

}

Edited: to solve this problem just add @ViewScoped on TableBean.java.

网友答案:

Your TableBean should be ViewScoped.

Add @ViewScoped on top of TableBean or configure it using faces-config.xml file.

网友答案:

You are using the example from the Showcase Labs, which is running on PrimeFaces version 3.4 (still in development at time of posting).

Since you are using PrimeFaces 3.3, you should be using the examples from the common Showcase.

Remember that the Labs version of the Showcase is always running on the still-on-development versions, while the common Showcase is always on the lastest final release.

You can always see what version is running behind the Showcase by checking the bottom of the page.

网友答案:

Since class Car is just a common class, not a manage bean, You don't need the declaration:

@ManagedBean(name = "car") @SessionScoped

分享给朋友:
您可能感兴趣的文章:
随机阅读: