1

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

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

4

1 回答 1

6

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

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

一个视图范围的 bean 跨越多个 HTTP 请求,并通过一个唯一的键存储 HTTP 会话。一些但不是所有的 servlet 容器将会话存储在硬盘上而不是内存上,这需要所有直接存储在会话中的 Java 对象来实现Serializable,包括视图范围的 bean 及其所有属性。

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

  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 开始,您可以将其作为方法参数传递。另请参阅如何将选定的行传递给 dataTable 中的 commandLink?

于 2012-09-28T13:38:41.450 回答