jackrobert
jackrobert

Reputation: 131

java.io.NotSerializableException: javax.faces.component.html.HtmlForm

I used view scope for bean class. when i run my application i got the error (JSF2.0,Richfaces3.3.3).

If i use session scope, the following code works fine.

sample.jsp

<f:view>
   <html>
      <head>
          <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
      </head>
      <body>
         <h:form id="sampleForm" binding="#{Sample.initForm}">
            <h:outputText value="This is sample form"/>
        </h:form>
    </body>
</html>  
</f:view>

Sample.java

package com.sample;

import java.io.Serializable;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.component.html.HtmlForm;

@ManagedBean(name="Sample")
@ViewScoped
public class Sample implements Serializable
{
    private HtmlForm initForm;   

    public HtmlForm getInitForm()
    {
        System.out.println("Sample initilaized....");       
        return initForm;
    }

public void setInitForm(HtmlForm initForm)
{
    this.initForm = initForm;
}
}

After executing this code, i got the following error

java.io.NotSerializableException: javax.faces.component.html.HtmlForm
java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1180)
java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1528)
java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1493)
java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1416)
java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1174)
java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:346)
java.util.HashMap.writeObject(HashMap.java:1018)
sun.reflect.GeneratedMethodAccessor46.invoke(Unknown Source)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
java.lang.reflect.Method.invoke(Method.java:616)
java.io.ObjectStreamClass.invokeWriteObject(ObjectStreamClass.java:962)
java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1480)
java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1416)
java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1174)
java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1528)
java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1493)
java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1416)
java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1174)
java.io.ObjectOutputStream.writeArray(ObjectOutputStream.java:1362)
java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1170)
java.io.ObjectOutputStream.writeArray(ObjectOutputStream.java:1362)
java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1170)
java.io.ObjectOutputStream.writeArray(ObjectOutputStream.java:1362)
java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1170)
java.io.ObjectOutputStream.writeArray(ObjectOutputStream.java:1362)
java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1170)
java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:346)
com.sun.faces.renderkit.ClientSideStateHelper.doWriteState(ClientSideStateHelper.java:293)
com.sun.faces.renderkit.ClientSideStateHelper.writeState(ClientSideStateHelper.java:167)
com.sun.faces.renderkit.ResponseStateManagerImpl.writeState(ResponseStateManagerImpl.java:123)
com.sun.faces.application.StateManagerImpl.writeState(StateManagerImpl.java:155)
javax.faces.application.StateManagerWrapper.writeState(StateManagerWrapper.java:143)
org.ajax4jsf.application.AjaxStateManager.writeState(AjaxStateManager.java:57)
com.sun.faces.application.view.WriteBehindStateWriter.flushToWriter(WriteBehindStateWriter.java:221)
com.sun.faces.application.view.JspViewHandlingStrategy.renderView(JspViewHandlingStrategy.java:225)
com.sun.faces.application.view.MultiViewHandler.renderView(MultiViewHandler.java:126)
org.ajax4jsf.application.ViewHandlerWrapper.renderView(ViewHandlerWrapper.java:100)
org.ajax4jsf.application.AjaxViewHandler.renderView(AjaxViewHandler.java:176)
com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:127)
com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:139)
javax.faces.webapp.FacesServlet.service(FacesServlet.java:313)
org.ajax4jsf.webapp.BaseXMLFilter.doXmlFilter(BaseXMLFilter.java:206)
org.ajax4jsf.webapp.BaseFilter.handleRequest(BaseFilter.java:290)
org.ajax4jsf.webapp.BaseFilter.processUploadsAndHandleRequest(BaseFilter.java:388)
org.ajax4jsf.webapp.BaseFilter.doFilter(BaseFilter.java:515)

Help me.. Thanks in advance

Upvotes: 1

Views: 5117

Answers (1)

BalusC
BalusC

Reputation: 1108782

java.io.NotSerializableException: javax.faces.component.html.HtmlForm

The HtmlForm is indeed not Serializable. You have 2 options:

  1. Just don't bind the form component to the view scoped bean at all. There are definitely other ways to achieve the functional requirement for which you thought that binding the form component would be the right solution.

  2. Make the property transient like so

    private transient HtmlForm initForm;   
    

    This way the property will be skipped upon (de)serialization.

Upvotes: 4

Related Questions