primefaces dataTable上的selectionMode

时间:2012-10-05 09:55:06

标签: jsf jsf-2 primefaces

我在confirmDialog中有一个数据表,我尝试在其上放置多个选择复选框,但导致错误&数据不可见

页:

<h:form>
<p:commandButton update="msgfiles" actionListener="#{globalCounter.reloadList()}"
   oncomplete="choose.show()"/>

                <p:confirmDialog widgetVar="choose">
                    <p:dataTable id="msgfiles" var="obj" rows="5" value="#{globalCounter.objectList}"
                                 rowKey="#{obj[0]}" selection="#{globalCounter.selectedObjectList}">
                        <p:column selectionMode="multiple"/>
                        <p:column headerText="id">
                            <h:outputText value="#{obj[0]}"/>
                        </p:column>
                        <p:column headerText="name">
                            <h:outputText value="#{obj[1]}"/>
                        </p:column>
                    </p:dataTable>
                </p:confirmDialog>
            </h:form>

豆:

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

@ManagedBean(name = "globalCounter")
@SessionScoped
public class TestBean implements Serializable {

   private List<Object[]> objectList;
   public List<Object[]> getObjectList() {
        return objectList;
    }
   public void setObjectList(List<Object[]> objectList) {
        this.objectList = objectList;
    }

    public void reloadList() {
        objectList = new ArrayList<Object[]>();
        objectList.add(new Object[]{"1", "one"});
        objectList.add(new Object[]{"2", "two"});
        objectList.add(new Object[]{"3", "three"});
        objectList.add(new Object[]{"4", "four"});
        objectList.add(new Object[]{"5", "five"});
    }

    private List<Object[]> selectedObjectList;
    public List<Object[]> getSelectedObjectList() {
        return selectedObjectList;
    }
    public void setSelectedObjectList(List<Object[]> selectedObjectList) {
        this.selectedObjectList = selectedObjectList;
    }
}

错误消息:

    [#|2012-10-03T16:28:36.390+0700|SEVERE|glassfish3.0.1|javax.enterprise.system.std.com.sun.enterprise.v3.services.impl|_ThreadID=29;_ThreadName=Thread-1;|javax.faces.FacesException
   at com.sun.faces.lifecycle.ApplyRequestValuesPhase.execute(ApplyRequestValuesPhase.java:86)
   at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
   at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
   at javax.faces.webapp.FacesServlet.service(FacesServlet.java:312)
   at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1523)
   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:343)
   at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:215)
   at org.primefaces.webapp.filter.FileUploadFilter.doFilter(FileUploadFilter.java:79)
   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:256)
   at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:215)
   at atiits.filter.AuthenticateAuthorize.doFilter(AuthenticateAuthorize.java:100)
   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:256)
   at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:215)
   at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:277)
   at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:188)
   at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:641)
   at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:97)
   at com.sun.enterprise.web.PESessionLockingStandardPipeline.invoke(PESessionLockingStandardPipeline.java:85)
   at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:185)
   at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:325)
   at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:226)
   at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:165)
   at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:791)
   at com.sun.grizzly.comet.CometEngine.executeServlet(CometEngine.java:473)
   at com.sun.grizzly.comet.CometEngine.handle(CometEngine.java:341)
   at com.sun.grizzly.comet.CometAsyncFilter.doFilter(CometAsyncFilter.java:84)
   at com.sun.grizzly.arp.DefaultAsyncExecutor.invokeFilters(DefaultAsyncExecutor.java:161)
   at com.sun.grizzly.arp.DefaultAsyncExecutor.interrupt(DefaultAsyncExecutor.java:137)
   at com.sun.grizzly.arp.AsyncProcessorTask.doTask(AsyncProcessorTask.java:88)
   at com.sun.grizzly.http.TaskBase.run(TaskBase.java:189)
   at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:330)
   at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:309)
   at java.lang.Thread.run(Thread.java:619)
Caused by: java.lang.NullPointerException
   at java.lang.reflect.Array.newArray(Native Method)
   at java.lang.reflect.Array.newInstance(Array.java:52)
   at org.primefaces.component.datatable.feature.SelectionFeature.decodeMultipleSelection(SelectionFeature.java:53)
   at org.primefaces.component.datatable.feature.SelectionFeature.decode(SelectionFeature.java:39)
   at org.primefaces.component.datatable.DataTableRenderer.decode(DataTableRenderer.java:53)
   at javax.faces.component.UIComponentBase.decode(UIComponentBase.java:790)
   at org.primefaces.component.api.UIData.processDecodes(UIData.java:224)
   at javax.faces.component.UIComponentBase.processDecodes(UIComponentBase.java:1042)
   at javax.faces.component.UIForm.processDecodes(UIForm.java:216)
   at javax.faces.component.UIComponentBase.processDecodes(UIComponentBase.java:1042)
   at javax.faces.component.UIComponentBase.processDecodes(UIComponentBase.java:1042)
   at javax.faces.component.UIViewRoot.processDecodes(UIViewRoot.java:941)
   at com.sun.faces.lifecycle.ApplyRequestValuesPhase.execute(ApplyRequestValuesPhase.java:78)
   ... 32 more
|#]

[#|2012-10-03T16:28:36.390+0700|WARNING|glassfish3.0.1|javax.enterprise.system.container.web.com.sun.enterprise.web|_ThreadID=29;_ThreadName=http-thread-pool-8080-(1);|StandardWrapperValve[Faces Servlet]: PWC1406: Servlet.service() for servlet Faces Servlet threw exception
java.lang.NullPointerException
   at java.io.Writer.write(Writer.java:140)
   at com.sun.faces.context.AjaxExceptionHandlerImpl.handlePartialResponseError(AjaxExceptionHandlerImpl.java:200)
   at com.sun.faces.context.AjaxExceptionHandlerImpl.handle(AjaxExceptionHandlerImpl.java:119)
   at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:119)
   at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
   at javax.faces.webapp.FacesServlet.service(FacesServlet.java:312)
   at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1523)
   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:343)
   at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:215)
   at org.primefaces.webapp.filter.FileUploadFilter.doFilter(FileUploadFilter.java:79)
   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:256)
   at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:215)
   at atiits.filter.AuthenticateAuthorize.doFilter(AuthenticateAuthorize.java:100)
   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:256)
   at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:215)
   at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:277)
   at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:188)
   at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:641)
   at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:97)
   at com.sun.enterprise.web.PESessionLockingStandardPipeline.invoke(PESessionLockingStandardPipeline.java:85)
   at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:185)
   at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:325)
   at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:226)
   at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:165)
   at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:791)
   at com.sun.grizzly.comet.CometEngine.executeServlet(CometEngine.java:473)
   at com.sun.grizzly.comet.CometEngine.handle(CometEngine.java:341)
   at com.sun.grizzly.comet.CometAsyncFilter.doFilter(CometAsyncFilter.java:84)
   at com.sun.grizzly.arp.DefaultAsyncExecutor.invokeFilters(DefaultAsyncExecutor.java:161)
   at com.sun.grizzly.arp.DefaultAsyncExecutor.interrupt(DefaultAsyncExecutor.java:137)
   at com.sun.grizzly.arp.AsyncProcessorTask.doTask(AsyncProcessorTask.java:88)
   at com.sun.grizzly.http.TaskBase.run(TaskBase.java:189)
   at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:330)
   at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:309)
   at java.lang.Thread.run(Thread.java:619)
|#]

[#|2012-10-03T16:28:36.390+0700|WARNING|glassfish3.0.1|javax.enterprise.system.container.web.com.sun.enterprise.web|_ThreadID=29;_ThreadName=http-thread-pool-8080-(1);|StandardWrapperValve[Faces Servlet]: PWC1406: Servlet.service() for servlet Faces Servlet threw exception
java.lang.NullPointerException
   at java.io.Writer.write(Writer.java:140)
   at com.sun.faces.context.AjaxExceptionHandlerImpl.handlePartialResponseError(AjaxExceptionHandlerImpl.java:200)
   at com.sun.faces.context.AjaxExceptionHandlerImpl.handle(AjaxExceptionHandlerImpl.java:119)
   at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:119)
   at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
   at javax.faces.webapp.FacesServlet.service(FacesServlet.java:312)
   at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1523)
   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:343)
   at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:215)
   at org.primefaces.webapp.filter.FileUploadFilter.doFilter(FileUploadFilter.java:79)
   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:256)
   at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:215)
   at atiits.filter.AuthenticateAuthorize.doFilter(AuthenticateAuthorize.java:100)
   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:256)
   at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:215)
   at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:277)
   at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:188)
   at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:641)
   at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:97)
   at com.sun.enterprise.web.PESessionLockingStandardPipeline.invoke(PESessionLockingStandardPipeline.java:85)
   at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:185)
   at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:325)
   at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:226)
   at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:165)
   at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:791)
   at com.sun.grizzly.comet.CometEngine.executeServlet(CometEngine.java:473)
   at com.sun.grizzly.comet.CometEngine.handle(CometEngine.java:341)
   at com.sun.grizzly.comet.CometAsyncFilter.doFilter(CometAsyncFilter.java:84)
   at com.sun.grizzly.arp.DefaultAsyncExecutor.invokeFilters(DefaultAsyncExecutor.java:161)
   at com.sun.grizzly.arp.DefaultAsyncExecutor.interrupt(DefaultAsyncExecutor.java:137)
   at com.sun.grizzly.arp.AsyncProcessorTask.doTask(AsyncProcessorTask.java:88)
   at com.sun.grizzly.http.TaskBase.run(TaskBase.java:189)
   at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:330)
   at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:309)
   at java.lang.Thread.run(Thread.java:619)
|#]

如果我从页面中删除<p:column selectionMode="multiple" style="width:18px" />,一切正常并显示数据

我哪里出错了?

Primefaces 3.4 / Mojarra 2.0.2(FCS b10)/ Glassfish 3.0.1

3 个答案:

答案 0 :(得分:1)

我遇到了同样的问题,我使用<h:form><p:ajax>

解决了这个问题
<p:dataTable id="msgfiles" var="obj" rows="5" value="#{globalCounter.objectList}" rowKey="#{obj[0]}" selection="#{globalCounter.selectedObjectList}">
    <p:ajax event="rowSelectCheckbox" process="@this"/>
    <p:ajax event="rowUnselectCheckbox" process="@this"/>
    <p:ajax event="toggleSelect" process="@this"/>
</p:dataTable>

这应该强制你的组件填充bean中的数组

答案 1 :(得分:1)

我有类似的问题,我使用<p:ajax>解决了这个问题。

首先我会这样做,因为akoskm说并且有一个独特的行键。

然后我会为selectedItems使用一个数组(List [])(List比ArrayList更通用)

最后你可以添加:

<p:dataTable ...>
    <p:ajax event="rowSelectCheckbox" process="@this"/>
    <p:ajax event="rowUnselectCheckbox" process="@this"/>
    <p:ajax event="toggleSelect" process="@this"/>
</p:dataTable>

确保桌子周围有<h:form>

这将确保组件将数据推送到bean。

答案 2 :(得分:0)

您的问题非常类似于此问题: Primefaces datatable: Unable to retrieve selected rows and selection get lost with pagination。 我会尝试使用 rowKey="#{obj}"为关键。