Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Serializable Action Forms? Why?

 
Charles McGuire
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Struts documentation (http://struts.apache.org/api/index.html) says we should serialize ActionForms:

Because ActionForms are JavaBeans, subclasses should also implement Serializable, as required by the JavaBean specification. Some containers require that an object meet all JavaBean requirements in order to use the introspection API upon which ActionForms rely." (16Oct2004)


Why?

The ActionForm class already implements java.io.Serializable. Are not the subclasses extended from this class automatically serialized by inheritance? In other words, why implement serializable again?
 
Marc Peabody
pie sneak
Sheriff
Posts: 4727
Mac Ruby VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wow. Great question. I never noticed it before and it's right there in the ActionForm's javadoc.

Yes, if ActionForm is Serializable its subclasses implement Serializable.

Speople specify "implements Serializable" in bean subclasses as a gentle reminder. It's still weird that the Javadoc says that. Maybe it was intended as a reminder any non-Serializable class variable should be declared transient.
 
Charles McGuire
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for acknowledging the question. The date on the JavaDoc is relatively recent, too. It is weird, and no one has a good answer; everyone I ask seems equally stumped, even my Struts instructor.

Oh, the humanity!

 
Jason Menard
Sheriff
Posts: 6450
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can tell you that awhile ago a version of Tomcat I used threw a stack trace on shutdown and startup (only after the first time the app had been started) complaining that some of my ActionForm classes, which I had kept in session scope, were not serializable. I believe this had something to do with the way Tomcat tried to save and restore sessions; just a guess, I didn't really look into it too hard. Implementing serializable took care of the situation.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34422
347
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Charles,
I took that to mean that the subclass should observe the contract of Serializable, not just implement the interface. For example, all of the fields must be Serializable.

Personally, I like to implement Serializable explicitly as a reminder.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic