1

I get the below error, when setting the scope for the managedBean as ViewScoped. Below is the exception Im getting when trying to invoke the page

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)  

Any pointers or help on resolving the issue is much appericiated. Thanks in Advance.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
vr3w3c9
  • 1,118
  • 8
  • 32
  • 57

1 Answers1

6

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

Your view scoped bean has apparently a ListDataModel property. This is indeed not serializable as its state is per definition dependent on the current HTTP request (which is usually not to be saved/shared anywhere --which would in turn require serialization).

A view scoped bean spans across multiple HTTP requests and is by an unique key stored the HTTP session. Some but not all servletcontainers stores sessions on harddisk instead of on memory and this requires all Java objects which are (in)directly stored in the session to implement Serializable, including view scoped beans and all of its properties.

You can fix this particular issue in 2 ways:

  1. Mark the property transient, get hold of the wrapped list as another property, and use lazy loading in the 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. Don't use DataModel, but use an alternative instead. A common requirement for having DataModel was in JSF 1.x being able to obtain the current row. But since EL 2.2, you could just pass that as method argument. See also How can I pass selected row to commandLink inside dataTable?

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Thanks a lot for the detailed explanation Balusc. I have set the DataModel as transient.But Still, it throws not java.io.NotSerializableException. I have included spring in my project and it throws this exception in the jar file in the below line."SEVERE: Error Rendering View[/ui/ScheduleDetails.xhtml] javax.faces.FacesException: java.io.NotSerializableException: org.springframework.web.context.support.XmlWebApplicationContext". can you please let me know what I can do to get this issue resolved. Thanks in Advance – vr3w3c9 Oct 03 '12 at 13:08
  • That part is beyond me. I can only tell that it's not the first time I see that Spring doesn't properly deal with serialization. The Java EE 6 standard CDI/EJB (which Spring was supposed to supplant in the old J2EE ages) does. – BalusC Oct 03 '12 at 13:14