3

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

当我查看 managedbean 的范围时,datatable selectedRow 给出了空指针异常。如果我使用会话范围,那么我可以在 managedbean 中获得 selectedRow。

我正在使用 CDI Spring 注释来指定范围。我已经使用这种方法来创建视图范围。

更新 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}" />  

托管豆

@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方法的这一行得到空指针异常

 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)
4

2 回答 2

3

我已经设法通过使用@ViewAccessScopedwhich is supported in CDI 解决了这个问题。在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>

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

于 2012-12-26T18:37:59.003 回答
1

如果您想使用选择,我认为您必须在数据表上提供 rowKey 属性。

于 2012-12-26T13:16:45.870 回答