java.lang.UnsupportedOperationException:当不使用基本rowKey算法时,必须实现getRowData(String rowKey) [英] java.lang.UnsupportedOperationException: getRowData(String rowKey) must be implemented when basic rowKey algorithm is not used

查看:226
本文介绍了java.lang.UnsupportedOperationException:当不使用基本rowKey算法时,必须实现getRowData(String rowKey)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已将PrimeFaces从5.1最终版升级到5.2最终版(社区版本).我有一个<p:dataTable>,它按如下方式延迟加载(一个仅出于纯粹测试目的而重现该问题的最小示例).

I have upgraded PrimeFaces from 5.1 final to 5.2 final (the Community Release). I have a <p:dataTable> which is lazily loaded as follows (a minimal example to reproduce the problem for a pure testing purpose only).

<p:dataTable  var="row"
              value="#{testManagedBean}"
              lazy="true"
              editable="true"
              rowKey="#{row.fruitId}"
              selection="#{testManagedBean.selectedValues}"
              rows="50">

    <p:column selectionMode="multiple"/>

    <p:ajax event="rowEdit" listener="#{testManagedBean.onRowEdit}"/>

    <p:column headerText="Id">
        <h:outputText value="#{row.fruitId}"/>
    </p:column>

    <p:column headerText="Fruit Name">
        <p:cellEditor>
            <f:facet name="output">
                <h:outputText value="#{row.fruitName}"/>
            </f:facet>
            <f:facet name="input">
                <p:inputText value="#{row.fruitName}"/>
            </f:facet>
        </p:cellEditor>
    </p:column>

    <p:column headerText="Price">
        <p:cellEditor>
            <f:facet name="output">
                <h:outputText value="#{row.price}"/>
            </f:facet>
            <f:facet name="input">
                <p:inputText value="#{row.price}"/>
            </f:facet>
        </p:cellEditor>
    </p:column>

    <p:column headerText="Edit">
        <p:rowEditor/>
    </p:column>
</p:dataTable>

相应的托管bean:

@Named
@ViewScoped
public class TestManagedBean extends LazyDataModel<Fruit>  implements Serializable {

    private List<Fruit> selectedValues; // Getter & setter.
    private static final long serialVersionUID = 1L;

    public TestManagedBean() {}

    private List<Fruit> init() {
        List<Fruit> fruits = new ArrayList<Fruit>();

        Fruit fruit = new Fruit();
        fruit.setFruitId(1);
        fruit.setFruitName("Mango");
        fruit.setPrice(500D);
        fruits.add(fruit);

        fruit = new Fruit();
        fruit.setFruitId(2);
        fruit.setFruitName("Guava");
        fruit.setPrice(300D);
        fruits.add(fruit);

        fruit = new Fruit();
        fruit.setFruitId(3);
        fruit.setFruitName("Apple");
        fruit.setPrice(600D);
        fruits.add(fruit);
        return fruits;
    }

    @Override
    public List<Fruit> load(int first, int pageSize, String sortField, SortOrder sortOrder, Map<String, Object> filters) {
        List<Fruit> fruits = init();
        setRowCount(fruits.size());
        return fruits;
    }

    public void onRowEdit(RowEditEvent event) {
        System.out.println("id : "+((Fruit)event.getObject()).getFruitId());
    }
}

在编辑onRowEdit()方法时,应该调用绑定到该方法的

While editing the onRowEdit() method is supposed to be invoked which is bound to,

<p:ajax event="rowEdit" listener="#{testManagedBean.onRowEdit}"/>

当一行处于编辑模式下并单击带有勾号的更新链接时,将引发以下异常.

When a row is in an edit mode and the update link marked by a tick is clicked, it causes the following exception to be thrown.

Info:   java.lang.UnsupportedOperationException: getRowData(String rowKey) must be implemented when basic rowKey algorithm is not used.
java.lang.UnsupportedOperationException: getRowData(String rowKey) must be implemented when basic rowKey algorithm is not used.
    at org.primefaces.model.LazyDataModel.getRowData(LazyDataModel.java:95)
    at org.primefaces.component.datatable.DataTable.getRowData(DataTable.java:1214)
    at org.primefaces.component.datatable.feature.SelectionFeature.decodeMultipleSelection(SelectionFeature.java:90)
    at org.primefaces.component.datatable.feature.SelectionFeature.decode(SelectionFeature.java:48)
    at org.primefaces.component.datatable.DataTableRenderer.decode(DataTableRenderer.java:62)
    at javax.faces.component.UIComponentBase.decode(UIComponentBase.java:834)
    at org.primefaces.component.api.UIData.processDecodes(UIData.java:281)
    at com.sun.faces.context.PartialViewContextImpl$PhaseAwareVisitCallback.visit(PartialViewContextImpl.java:573)
    at com.sun.faces.component.visit.PartialVisitContext.invokeVisitCallback(PartialVisitContext.java:183)
    at org.primefaces.component.api.UIData.visitTree(UIData.java:821)
    at javax.faces.component.UIComponent.visitTree(UIComponent.java:1690)
    at javax.faces.component.UIForm.visitTree(UIForm.java:380)
    at javax.faces.component.UIComponent.visitTree(UIComponent.java:1690)
    at javax.faces.component.UIComponent.visitTree(UIComponent.java:1690)
    at com.sun.faces.context.PartialViewContextImpl.processComponents(PartialViewContextImpl.java:403)
    at com.sun.faces.context.PartialViewContextImpl.processPartial(PartialViewContextImpl.java:266)
    at org.primefaces.context.PrimePartialViewContext.processPartial(PrimePartialViewContext.java:60)
    at javax.faces.component.UIViewRoot.processDecodes(UIViewRoot.java:930)
    at com.sun.faces.lifecycle.ApplyRequestValuesPhase.execute(ApplyRequestValuesPhase.java:78)
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
    at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:198)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:660)
    at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1682)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:318)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160)
    at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:734)
    at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:673)
    at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:174)
    at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:415)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:282)
    at com.sun.enterprise.v3.services.impl.ContainerMapper$HttpHandlerCallable.call(ContainerMapper.java:459)
    at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:167)
    at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:201)
    at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:175)
    at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:235)
    at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:284)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:201)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:133)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:112)
    at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
    at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:561)
    at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:112)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:117)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:56)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:137)
    at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:565)
    at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:545)
    at java.lang.Thread.run(Thread.java:745)

Warning:   java.lang.UnsupportedOperationException: getRowData(String rowKey) must be implemented when basic rowKey algorithm is not used.
javax.faces.FacesException: java.lang.UnsupportedOperationException: getRowData(String rowKey) must be implemented when basic rowKey algorithm is not used.
    at com.sun.faces.context.PartialViewContextImpl.processPartial(PartialViewContextImpl.java:273)
    at org.primefaces.context.PrimePartialViewContext.processPartial(PrimePartialViewContext.java:60)
    at javax.faces.component.UIViewRoot.processDecodes(UIViewRoot.java:930)
    at com.sun.faces.lifecycle.ApplyRequestValuesPhase.execute(ApplyRequestValuesPhase.java:78)
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
    at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:198)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:660)
    at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1682)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:318)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160)
    at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:734)
    at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:673)
    at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:174)
    at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:415)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:282)
    at com.sun.enterprise.v3.services.impl.ContainerMapper$HttpHandlerCallable.call(ContainerMapper.java:459)
    at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:167)
    at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:201)
    at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:175)
    at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:235)
    at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:284)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:201)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:133)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:112)
    at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
    at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:561)
    at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:112)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:117)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:56)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:137)
    at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:565)
    at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:545)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.UnsupportedOperationException: getRowData(String rowKey) must be implemented when basic rowKey algorithm is not used.
    at org.primefaces.model.LazyDataModel.getRowData(LazyDataModel.java:95)
    at org.primefaces.component.datatable.DataTable.getRowData(DataTable.java:1214)
    at org.primefaces.component.datatable.feature.SelectionFeature.decodeMultipleSelection(SelectionFeature.java:90)
    at org.primefaces.component.datatable.feature.SelectionFeature.decode(SelectionFeature.java:48)
    at org.primefaces.component.datatable.DataTableRenderer.decode(DataTableRenderer.java:62)
    at javax.faces.component.UIComponentBase.decode(UIComponentBase.java:834)
    at org.primefaces.component.api.UIData.processDecodes(UIData.java:281)
    at com.sun.faces.context.PartialViewContextImpl$PhaseAwareVisitCallback.visit(PartialViewContextImpl.java:573)
    at com.sun.faces.component.visit.PartialVisitContext.invokeVisitCallback(PartialVisitContext.java:183)
    at org.primefaces.component.api.UIData.visitTree(UIData.java:821)
    at javax.faces.component.UIComponent.visitTree(UIComponent.java:1690)
    at javax.faces.component.UIForm.visitTree(UIForm.java:380)
    at javax.faces.component.UIComponent.visitTree(UIComponent.java:1690)
    at javax.faces.component.UIComponent.visitTree(UIComponent.java:1690)
    at com.sun.faces.context.PartialViewContextImpl.processComponents(PartialViewContextImpl.java:403)
    at com.sun.faces.context.PartialViewContextImpl.processPartial(PartialViewContextImpl.java:266)
    ... 34 more

Severe:   java.lang.UnsupportedOperationException: getRowData(String rowKey) must be implemented when basic rowKey algorithm is not used.
    at org.primefaces.model.LazyDataModel.getRowData(LazyDataModel.java:95)
    at org.primefaces.component.datatable.DataTable.getRowData(DataTable.java:1214)
    at org.primefaces.component.datatable.feature.SelectionFeature.decodeMultipleSelection(SelectionFeature.java:90)
    at org.primefaces.component.datatable.feature.SelectionFeature.decode(SelectionFeature.java:48)
    at org.primefaces.component.datatable.DataTableRenderer.decode(DataTableRenderer.java:62)
    at javax.faces.component.UIComponentBase.decode(UIComponentBase.java:834)
    at org.primefaces.component.api.UIData.processDecodes(UIData.java:281)
    at com.sun.faces.context.PartialViewContextImpl$PhaseAwareVisitCallback.visit(PartialViewContextImpl.java:573)
    at com.sun.faces.component.visit.PartialVisitContext.invokeVisitCallback(PartialVisitContext.java:183)
    at org.primefaces.component.api.UIData.visitTree(UIData.java:821)
    at javax.faces.component.UIComponent.visitTree(UIComponent.java:1690)
    at javax.faces.component.UIForm.visitTree(UIForm.java:380)
    at javax.faces.component.UIComponent.visitTree(UIComponent.java:1690)
    at javax.faces.component.UIComponent.visitTree(UIComponent.java:1690)
    at com.sun.faces.context.PartialViewContextImpl.processComponents(PartialViewContextImpl.java:403)
    at com.sun.faces.context.PartialViewContextImpl.processPartial(PartialViewContextImpl.java:266)
    at org.primefaces.context.PrimePartialViewContext.processPartial(PrimePartialViewContext.java:60)
    at javax.faces.component.UIViewRoot.processDecodes(UIViewRoot.java:930)
    at com.sun.faces.lifecycle.ApplyRequestValuesPhase.execute(ApplyRequestValuesPhase.java:78)
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
    at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:198)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:660)
    at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1682)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:318)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160)
    at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:734)
    at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:673)
    at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:174)
    at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:415)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:282)
    at com.sun.enterprise.v3.services.impl.ContainerMapper$HttpHandlerCallable.call(ContainerMapper.java:459)
    at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:167)
    at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:201)
    at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:175)
    at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:235)
    at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:284)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:201)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:133)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:112)
    at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
    at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:561)
    at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:112)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:117)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:56)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:137)
    at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:565)
    at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:545)
    at java.lang.Thread.run(Thread.java:745)

这不应该发生,因为rowKey="#{row.fruitId}"与给定的<p:dataTable>一起使用.

This should not happen because rowKey="#{row.fruitId}" is used with the given <p:dataTable>.

仅当删除rowKey属性并在关联的托管bean中实现getRowKey()getRowData()方法时,该异常才会消失,如下所示.

The exception disappears only when the rowKey attribute is removed and the getRowKey() and the getRowData() methods are implemented in the associated managed bean as follows.

@Override
public Object getRowKey(Fruit fruit) {
    return fruit != null ? fruit.getFruitId() : null;
}

@Override
public Fruit getRowData(String rowKey) {
    List<Fruit> fruits = (List<Fruit>) getWrappedData();
    Integer value = Integer.valueOf(rowKey);

    for (Fruit fruit : fruits) {
        if (fruit.getFruitId().equals(value)) {
            return fruit;
        }
    }

    return null;
}

在最新版本的PrimeFaces(最终版5.2)中是强制性的吗?在以前版本的PrimeFaces(最终版5.1)中不需要吗?

Is it mandatory in the newest version of PrimeFaces (5.2 final) which was not needed in the previous version of PrimeFaces (5.1 final)?

在Mojarra 2.3.0-m02上测试.我以为这个版本的Mojarra应该没有任何意义.

推荐答案

链接指向PrimeFaces社区论坛,

As mentioned in this link pointing to the PrimeFaces Community Forum,

我们通过设计更改了该部分,导致rowKey属性处理 LazyDataModel是不正确的.

We changed that part by design cause the rowKey attribute handling with a LazyDataModel is not correct.

如问题所示,每当使用LazyDataModel<T>时,都需要在关联的受管bean中实现这两种方法getRowKey()getRowData().

Those two methods getRowKey() and getRowData() need to be implemented in the associated managed bean whenever LazyDataModel<T> is used as shown in the question.

来自 PrimeFaces用户指南(PDF)-(5.1(第164页)和5.2(第166页) ):

From PrimeFaces User Guide (PDF) - (5.1 (page 164) and 5.2 (page 166)) :

惰性加载是一种有效处理庞大数据集的方法, 常规基于ajax的分页仅通过渲染特定的 页,但仍需要将所有数据加载到内存中.懒惰的 加载数据表将类似地呈现特定页面,但也仅 将页面数据而不是整个数据集加载到内存中.为了 实施此操作,您需要绑定一个 org.primefaces.model.LazyDataModel作为值并实现负载 方法并启用懒惰选项. 还需要实施 getRowDatagetRowKey(如果已启用选择).

Lazy Loading is an approach to deal with huge datasets efficiently, regular ajax based pagination works by rendering only a particular page but still requires all data to be loaded into memory. Lazy loading datatable renders a particular page similarly but also only loads the page data into memory not the whole dataset. In order to implement this, you’d need to bind a org.primefaces.model.LazyDataModel as the value and implement load method and enable lazy option. Also it is required to implement getRowData and getRowKey if you have selection enabled.


禁用lazy时,可以使用属性rowKey -设置为false(默认值)并且启用行选择.


The attribute rowKey may be used, when lazy is disabled -- is set to false (default) and row selection is enabled.

来自 PrimeFaces用户指南(PDF)-(5.1(第159页)和5.2(第161页) ):

From PrimeFaces User Guide (PDF) - (5.1 (page 159) and 5.2 (page 161)) :

RowKey应该是数据模型中的唯一标识符,并由 数据表以查找选定的行.您可以通过以下方式定义此键 使用rowKey属性或通过绑定数据模型 实现org.primefaces.model.SelectableDataModel.

RowKey should a unique identifier from your data model and used by datatable to find the selected rows. You can either define this key by using the rowKey attribute or by binding a data model which implements org.primefaces.model.SelectableDataModel.

例如

<p:dataTable var="car"
             value="#{carBean.cars}"
             selection="#{carBean.selectedCars}"
             rowKey="#{car.id}">

    <p:column selectionMode="multiple"/>
    <!--columns-->
</p:dataTable>

这篇关于java.lang.UnsupportedOperationException:当不使用基本rowKey算法时,必须实现getRowData(String rowKey)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆