java-Primefaces datatable选定的行无法使用viewscope工作

我正在使用JSF 2.0和Primefaces 3.4.2,我有一个使用延迟加载填充的数据表.

当我查看managedbean的范围时,则数据表selectedRow给出了空指针异常.
如果使用会话作用域,则可以在managedbean中获得selectedRow.

我正在使用CDI Spring批注指定范围.我已使用this方法创建视图范围.

更新1

我注意到另一件事是当我分页到第二页然后返回第一页时使用视图范围,那么我可以得到selectedRow.如果我选择没有分页的行,则会出现空指针异常.

JSF页面

<p:dataTable id="dataTable" var="req" lazy="true" value="#{emp.lazyModel}"
                paginator="true" rows="10" 
                             selection="#{emp.selectedRequest}"
                            selectionMode="single">                         
          <p:ajax event="rowSelect" listener="#{emp.onRowSelect}" />  

ManagedBean

@Named("emp")
@Scope("view")
public class EmployeesManagedBean implements Serializable {

    @PostConstruct
    public void init() {
        initTable();
    }

    private void initTable() {
        lazyModel = new LazyRequestDataModel(requestList, requestService);
    }

    public LazyDataModel<Employee> getLazyModel() {
        return lazyModel;

    }

我在onRowSelect方法的这一行收到nullpointer异常

 Emp emp = (Emp) event.getObject()); 
 System.out.println(emp.getEmpNo() );

完整错误堆栈跟踪

java.lang.NullPointerException
    at net.test.managed.bean.RequestManagedBean.onRowSelect(RequestManagedBean.java:134)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    at com.sun.el.parser.AstValue.invoke(AstValue.java:187)
    at com.sun.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:297)
    at org.primefaces.component.behavior.ajax.AjaxBehaviorListenerImpl.processAjaxBehavior(AjaxBehaviorListenerImpl.java:52)
    at org.primefaces.event.SelectEvent.processListener(SelectEvent.java:40)
    at javax.faces.component.behavior.BehaviorBase.broadcast(BehaviorBase.java:106)
    at javax.faces.component.UIComponentBase.broadcast(UIComponentBase.java:760)
    at javax.faces.component.UIData.broadcast(UIData.java:1071)
    at javax.faces.component.UIData.broadcast(UIData.java:1093)
    at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:794)
    at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1259)
    at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81)
    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:593)
    at weblogic.servlet.internal.StubSecurityHelper$ServletServiceAction.run(StubSecurityHelper.java:227)
    at weblogic.servlet.internal.StubSecurityHelper.invokeServlet(StubSecurityHelper.java:125)
    at weblogic.servlet.internal.ServletStubImpl.execute(ServletStubImpl.java:301)
    at weblogic.servlet.internal.TailFilter.doFilter(TailFilter.java:26)
    at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:56)
    at weblogic.servlet.internal.RequestEventsFilter.doFilter(RequestEventsFilter.java:27)
    at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:56)
    at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.wrapRun(WebAppServletContext.java:3730)
    at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.run(WebAppServletContext.java:3696)
    at weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubject.java:321)
    at weblogic.security.service.SecurityManager.runAs(SecurityManager.java:120)
    at weblogic.servlet.internal.WebAppServletContext.securedExecute(WebAppServletContext.java:2273)
    at weblogic.servlet.internal.WebAppServletContext.execute(WebAppServletContext.java:2179)
    at weblogic.servlet.internal.ServletRequestImpl.run(ServletRequestImpl.java:1490)
    at weblogic.work.ExecuteThread.execute(ExecuteThread.java:256)
    at weblogic.work.ExecuteThread.run(ExecuteThread.java:221)

解决方法:

我设法使用CDI支持的@ViewAccessScoped解决了此问题.在maven中添加依赖项,或直接从http://myfaces.apache.org/extensions/cdi/download.html下载该依赖项并将其放在classpath中.

<dependency>
    <groupId>org.apache.myfaces.extensions.cdi.core</groupId>
    <artifactId>myfaces-extcdi-core-api</artifactId>
    <version>1.0.5</version>
    <scope>compile</scope>
</dependency>

仍然存在一个小问题,即在第一次部署应用程序时,我仍然没有获得选定的行值,对于随后的选择,我仍然能够获得选定的行值.

上一篇:java – SXSSFWorkbook上的AutosizeColumns


下一篇:java-向向量中添加值时抛出NullPointerexception