Primefaces。绑定数据模型不起作用。 ListDataModel.getWrappedData()返回null

时间:2013-01-22 03:11:49

标签: jsf-2 primefaces

我在Tomcat 7.0.23上尝试这个example。 单击一行时未填充对话框“dialog”和“multiDialog”,因为getWrappedData()返回null。这是CarDataModel的方法getRowData中的一行List<Car> cars = (List<Car>) getWrappedData();enter image description here

如果我使用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>

我的CarDataModel

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>

我的template.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>

1 个答案:

答案 0 :(得分:1)

它是由context-param STATE_SAVING_METHOD = client引起的。它必须是“服务器”。