Primefaces数据表选定的行无法使用viewcope工作 [英] Primefaces datatable selected row not working using viewscope
问题描述
我正在使用JSF 2.0和Primefaces 3.4.2,我有一个使用延迟加载填充的数据表。
I am using JSF 2.0 and Primefaces 3.4.2, I have a datatable populated using lazy load.
当我查看managedbean的范围时,数据表selectedRow给出null指针异常。
如果我使用会话范围,那么我可以在managedbean中获得selectedRow。
When I view scope for managedbean, then datatable selectedRow gives null pointer exception. If I use session scope then I could get selectedRow in managedbean.
我正在使用CDI Spring注释来指定范围。我使用了这个方法创建视图范围。
I am using CDI Spring annotations for specifying scope. I have used this method to create view scope.
更新1
我注意到另一件事是在我使用视野范围时分页到第二页然后回到第一页,然后我可以得到selectedRow。如果我选择没有分页的行,那么我会得到空指针异常。
I have noticed another thing is using view scope when I paginate to second page and then comes back to first page, then I could get the selectedRow. If I select a row without paginating then I get null pointer exception.
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
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;
}
我在<$ c的这一行得到nullpointer异常$ c> 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)
推荐答案
我设法通过使用解决了这个问题CDI支持 @ViewAccessScoped
。在maven中添加依赖项或直接从 http://myfaces.apache.org/extensions/下载cdi / download.html 并将其放在类路径中。
I have managed to resolve this issue by using @ViewAccessScoped
which is supported in CDI. Add the dependency in maven or directly download from http://myfaces.apache.org/extensions/cdi/download.html and put it in 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>
仍然存在一个小问题,即第一次部署应用程序时,我仍然没有被选中行值,对于后续选择,我可以获得选定的行值。
One small problem still remains i.e. when application is deployed for the first time, I still do not get selected row value, for the subsequent selections I am able to get selected row value.
这篇关于Primefaces数据表选定的行无法使用viewcope工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!