What is the need for Action classes to be serializable? When and how does it happen, if at all.
-
1I would be interested in the answer. Probably best to ask the struts2 developers on the use mailing list. My guess would be that there are multiple types of output some of which treat the action as the view layer such as is found when using the struts2-json-plugin. If you get a definitive answer and post it I will upvote it, and you may be able to get "self learner" badge.– QuaternionCommented Jan 25, 2011 at 23:31
3 Answers
As far as I can tell, it doesn't need to be Serializable, and it was a mistake to make ActionSupport implement that interface.
Here is the best reasoning I have found on the subject (taken from here):
It's very common in web frameworks to use Serializable objects for a couple or reasons, such as being able to preserve state across a server restart and for shipping objects around in a cluster.
With that said, (IMHO) I believe it was a design mistake to have ActionSupport implement Serializable. I don't believe that either of the above really apply to Action objects since they are short-lived. The choice of making Actions Serializable should have been left to the developer and not "forced" by the framework.
This question might not be relevant anymore but I thought this might help.
From Sun developer network:
Object serialization is the process of saving an object's state to a sequence of bytes, as well as the process of rebuilding those bytes into a live object at some future time.
So why you might want to serialize your objects? That's when you need to persist their state
so you can use them later or in another JVM. The JVM might be on the same machine or over the network on another machine. I think that's the same case for ActionSupport
class. If you extend ActionSupport
you'll get the chance to serialize your action and send it over the network to be used in another JVM.
i don't know why it must be so. but action classes must extends ActionSupport. and according to http://struts.apache.org/2.0.6/struts2-core/apidocs/com/opensymphony/xwork2/ActionSupport.html , ActionSupport implements Serializable. so the answer to the when question, it happens all the time :p
-
2Not true, actions don't have to extend ActionSupport. They can implement the Action interface or just adhere to convention. Commented Apr 8, 2011 at 15:47
-
1I think he was asking more along the lines of "Does the Struts framework ever actually Serialize ActionSupport", rather than "How often do Struts Action classes implement Serializable". Commented Aug 31, 2011 at 0:22