更改查看范围的请求会导致java.io.NotSerializableException:javax.faces.model.ListDataModel

时间:2012-09-28 13:31:38

标签: jsf-2 primefaces

当将managedBean的范围设置为ViewScoped时,我得到以下错误。以下是我在尝试调用页面时遇到的异常

javax.faces.FacesException: java.io.NotSerializableException: javax.faces.model.ListDataModel  
at com.sun.faces.renderkit.ResponseStateManagerImpl.getViewState(ResponseStateManagerImpl.java:137)  
at javax.faces.application.StateManager.getViewState(StateManager.java:555)  
at com.sun.faces.context.PartialViewContextImpl.renderState(PartialViewContextImpl.java:416)  
at com.sun.faces.context.PartialViewContextImpl.processPartial(PartialViewContextImpl.java:300)  
at javax.faces.context.PartialViewContextWrapper.processPartial(PartialViewContextWrapper.java:183)  
at javax.faces.component.UIViewRoot.encodeChildren(UIViewRoot.java:981)  
at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1756)  
at com.sun.faces.application.view.FaceletViewHandlingStrategy.renderView(FaceletViewHandlingStrategy.java:390)  
at com.sun.faces.application.view.MultiViewHandler.renderView(MultiViewHandler.java:131)  
at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:121)  
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)  
at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:139)  
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:594)  
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)  
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)  
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)  
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)  
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)  
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)  
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)  
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:286)  
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:845)  
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)  
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)  
at java.lang.Thread.run(Unknown Source)  
Caused by: java.io.NotSerializableException: javax.faces.model.ListDataModel  
at java.io.ObjectOutputStream.writeObject0(Unknown Source)  



Sep 26, 2012 4:01:13 PM org.apache.catalina.core.StandardWrapperValve invoke  
SEVERE: Servlet.service() for servlet Faces Servlet threw exception  
java.lang.IllegalStateException: CDATA tags may not nest  
at         com.sun.faces.renderkit.html_basic.HtmlResponseWriter.startCDATA(HtmlResponseWriter.java:630)  
at javax.faces.context.ResponseWriterWrapper.startCDATA(ResponseWriterWrapper.java:172)  
at javax.faces.context.PartialResponseWriter.startError(PartialResponseWriter.java:342)  
at org.primefaces.context.PrimePartialResponseWriter.startError(PrimePartialResponseWriter.java:210)  
at com.sun.faces.context.AjaxExceptionHandlerImpl.handlePartialResponseError(AjaxExceptionHandlerImpl.java:200)  
at com.sun.faces.context.AjaxExceptionHandlerImpl.handle(AjaxExceptionHandlerImpl.java:123)  
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:119)  

任何有关解决问题的指示或帮助都非常重要。提前致谢。

1 个答案:

答案 0 :(得分:4)

  

java.io.NotSerializableException:javax.faces.model.ListDataModel

您的视图范围bean显然具有ListDataModel属性。这确实是不可序列化的,因为它的状态是每个定义依赖于当前的HTTP请求(通常不会在任何地方保存/共享 - 这反过来又需要序列化)。

视图范围的bean跨越多个HTTP请求,并由存储在HTTP会话中的唯一密钥组成。一些但不是所有的servletcontainers都将会话存储在硬盘而不是内存上,这需要所有直接存储在会话中的Java对象实现Serializable,包括视图范围bean及其所有属性。

您可以通过两种方式解决此特定问题:

  1. 标记属性transient,将包装列表作为另一个属性,并在getter中使用延迟加载。

    private transient DataModel<Foo> model;
    private List<Foo> list;
    
    public DataModel<Foo> getModel() {
        if (model == null) {
            model = new ListDataModel<Foo>(list);
        }
        return model;
    }
    
  2. 请勿使用DataModel,而是使用替代方法。拥有DataModel的常见要求是JSF 1.x能够获取当前行。但是从EL 2.2开始,您可以将其作为方法参数传递。另请参阅How can I pass selected row to commandLink inside dataTable?