vr3w3c9
vr3w3c9

Reputation: 1178

Changing request to view scope results in java.io.NotSerializableException: javax.faces.model.ListDataModel

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.

Upvotes: 1

Views: 6361

Answers (1)

BalusC
BalusC

Reputation: 1108782

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?

Upvotes: 6

Related Questions