0

我在 Tomcat 7.0.23 上尝试这个例子。单击一行时不会填充对话框“dialog”和“multiDialog”,因为 getWrappedData() 返回 null。这是List<Car> cars = (List<Car>) getWrappedData();CarDataModel 的方法 getRowData 中的一行。 在此处输入图像描述

如果我使用 rowKey 属性而不是绑定,它可以工作。

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

问题是什么?

这是我的 pom.xml 中的一个片段

<dependency>
    <groupId>org.primefaces</groupId>
    <artifactId>primefaces</artifactId>
    <version>3.4</version>
</dependency>
<dependency>
    <groupId>com.sun.faces</groupId>
    <artifactId>jsf-api</artifactId>
    <version>2.1.13</version>
    <scope>compile</scope>
</dependency>
<dependency>
    <groupId>com.sun.faces</groupId>
    <artifactId>jsf-impl</artifactId>
    <version>2.1.13</version>
    <scope>compile</scope>
</dependency>

我的汽车数据模型

package data;
import java.io.Serializable;
import java.util.List;
import javax.faces.model.ListDataModel;
import org.primefaces.model.SelectableDataModel;

public class CarDataModel extends ListDataModel<Car> implements SelectableDataModel<Car>, Serializable{    
    private static final long serialVersionUID = -6094333899604122284L;

    public CarDataModel() {
    }  

    public CarDataModel(List<Car> data) {
        super(data);  
    }

    @Override  
    @SuppressWarnings("unchecked")
    public Car getRowData(String rowKey) {
        //In a real app, a more efficient way like a query by rowKey should be implemented to deal with huge data  
        List<Car> cars = (List<Car>) getWrappedData();
        for (Car car : cars) {
            if (car.getModel().equals(rowKey))
                return car;
        }
        return null; 
    }

    @Override  
    public Object getRowKey(Car car) {
        return car.getModel();  
    }  
}

我的 TableBean

package data;
import java.io.Serializable;  
import java.util.ArrayList;  
import java.util.List;  
import java.util.UUID;  
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;

@ManagedBean
@ViewScoped
public class TableBean implements Serializable {
    private static final long serialVersionUID = 2213388781051004157L;

    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;  

    private CarDataModel mediumCarsModel;  

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

        populateRandomCars(cars, 50);  

        mediumCarsModel = new CarDataModel(cars);  
    }  

    public Car[] getSelectedCars() {  
        return selectedCars;  
    }  
    public void setSelectedCars(Car[] selectedCars) {  
        this.selectedCars = selectedCars;  
    }  

    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 CarDataModel getMediumCarsModel() {  
        return mediumCarsModel;  
    }  

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

我的 datatableRowSelectionRadioCheckbox.xhtml

<?xml version="1.0" encoding="UTF-8"?>
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:p="http://primefaces.org/ui"
    template="/WEB-INF/templates/template.xhtml">
    <ui:define name="title">DataTable - RadioCheckbox</ui:define> 
    <ui:define name="content">
        <h:form id="form">

            <p:dataTable id="cars" var="car" value="#{TableBean.mediumCarsModel}" paginator="true" rows="10"  
                 selection="#{TableBean.selectedCar}"> 

                <f:facet name="header">
                    RadioButton Based Selection  
                </f:facet>

                <p:column selectionMode="single" style="width:18px" />

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

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

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

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

                <f:facet name="footer">
                    <p:commandButton id="viewCommand" value="View" icon="ui-icon-search"  
                                     update=":form:displaySingle" oncomplete="singleCarDialog.show()"/>  
                </f:facet>  
            </p:dataTable>  

            <p:dataTable id="multiCars" var="car" value="#{TableBean.mediumCarsModel}" paginator="true" rows="10"  
                 selection="#{TableBean.selectedCars}">  

                <f:facet name="header">  
                    Checkbox Based Selection  
                </f:facet>  

                <p:column selectionMode="multiple" style="width:18px" />  

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

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

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

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

                <f:facet name="footer">  
                    <p:commandButton id="multiViewButton" value="View" icon="ui-icon-search"  
                                     update=":form:displayMulti" oncomplete="multiCarDialog.show()"/>  
                </f:facet>  
            </p:dataTable>  

            <p:dialog id="dialog" header="Car Detail" widgetVar="singleCarDialog" resizable="false"  
                      showEffect="fade" hideEffect="explode">  

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

                    <f:facet name="header">  
                        <ui:param name="resourceName" value="images:cars/#{TableBean.selectedCar.manufacturer}.jpg"/>
                        <p:graphicImage value="#{resource[resourceName]}"/>  
                    </f:facet>  

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

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

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

                    <h:outputText value="Color:" />  
                    <h:outputText value="#{TableBean.selectedCar.color}" />  
                </h:panelGrid>  
            </p:dialog>  

            <p:dialog id="multiDialog" header="Car Detail" widgetVar="multiCarDialog"  
                      height="300" showEffect="fade" hideEffect="explode">  

                <p:dataList id="displayMulti"  
                        value="#{TableBean.selectedCars}" var="selectedCar">  
                    Model: #{selectedCar.model}, Year: #{selectedCar.year}  
                </p:dataList>  

            </p:dialog>  

        </h:form>
    </ui:define>
</ui:composition>

我的模板.xhtml

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
[
    <!ENTITY nbsp "&#160;"> 
]>
<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:ui="http://java.sun.com/jsf/facelets">
<ui:insert name="metadata"/>
<h:head>
    <title>My Primefaces</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <h:outputStylesheet name="css/default.css" />
    <h:outputStylesheet name="css/primefaces.css" />
    <h:outputStylesheet name="css/syntaxhighlighter.css" />
    <h:outputStylesheet name="css/theme.css" />
</h:head>
<h:body>
    <div id="header" class="ui-widget ui-widget-header">
            <div id="logo">
                <img src="#{resource['images:logo.png']}" alt="Logo" />
            </div>
    </div>
    <div id="page" class="ui-widget">
        <ui:include src="/sidebar/mainmenu.xhtml"/>

        <div id="content">
            <div class="post">
                <h1 class="title ui-widget-header ui-corner-all"><ui:insert name="title">Title</ui:insert></h1>
                <div class="entry" style="line-height:200%">
                    <ui:insert name="content">Main Content</ui:insert>

                </div>
            </div>
        </div>
        <div style="clear: both;">&nbsp;</div>
    </div>

    <div id="footer" class="ui-widget ui-widget-header ui-corner-all">
        <p class="copyright">My test of PrimeFaces-3.4</p>
    </div>
</h:body>
</html>
4

1 回答 1

1

它是由上下文参数 STATE_SAVING_METHOD = client 引起的。它必须是“服务器”。

于 2013-01-31T20:57:20.930 回答