wood burning stoves 2.0*
The moose likes Struts and the fly likes Serializable Action Forms?  Why? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Serializable Action Forms?  Why?" Watch "Serializable Action Forms?  Why?" New topic
Author

Serializable Action Forms? Why?

Charles McGuire
Ranch Hand

Joined: Jan 18, 2005
Posts: 99
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?


There's no place like 127.0.0.1
Marc Peabody
pie sneak
Sheriff

Joined: Feb 05, 2003
Posts: 4727

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.


A good workman is known by his tools.
Charles McGuire
Ranch Hand

Joined: Jan 18, 2005
Posts: 99
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

Joined: Nov 09, 2000
Posts: 6450
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
internet detective
Marshal

Joined: May 26, 2003
Posts: 29287
    
140

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.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Serializable Action Forms? Why?
 
Similar Threads
Serialization via Inheritance
ActionForm subclasses have to implement Serializable?
Serializable
y to implement serializable in getter & setter ?
POJO implements Serializable - Why ???