Take the 2-minute tour ×

I'm experienced struts1.x, In this all action classes are singleton behaviour by default.
We can write multiple methods in a single action class by extending DispatchAction class, so that any number of requests to the methods will create only one object and share all the instance members.

But in Struts2, There is a feature of writing multiple methods.
In struts2, For every request new instance will be created. Then what is the use of writing the multiple methods in a single Action class?
More over each method may have multiple instance members, then if you combine them and create a single object it will take so much memory for every time object creation..
Can you tell me what is the use of writing many methods in single Action Class in Struts2.x ???

share|improve this question
Your assertions regarding memory consumption are generally unfounded. Modern garbage collectors are pretty well optimized for objects with short life-spans, like Struts2 actions. –  Steven Benitez Aug 13 '12 at 13:31

2 Answers 2

up vote 2 down vote accepted

Well Struts1 and Struts2 are quite different the way they have been designed and the way there action classes are written.

Actions basically represents a user action for a set of related operations.This is a feature to module your application in good way.Though you are free to define only single method in your action class but i believe it will defeat the purpose of Action classes to a good way.

e.g Let's imagine we want to develop a functionality of User management which includes

  1. User registration.
  2. User Update
  3. Some other user functionality.

If we look closely we will going to create one User bean to hold the properties related to the user and now we need to think the design of ActionClasses. I will create a UserManagementAction with methods like

  1. createUser
  2. updateUser
  3. any other such methods

Such approach in my opinion will centralized the user management system as all those methods are related to the user management functionality,also all component f your action class are usable like user bean,user-service etc as they are specific to user and will be used in all method calls.

On the other hand with second appraoch we are left with one choice like create Action for each functionality like

  1. UserRegistrationAction
  2. UpdateUserAction

and in all those action we are duplicating most of the things biz DTO's service classes and any other utility methods beside the pain to have so many actions and some un-necessary configurations.

In short its the design decision which way and how you want to design and develop your application.

share|improve this answer
Thanks for nice example explanation... –  Mr.Chowdary Aug 13 '12 at 6:12
Keep in mind that UserRegistrationAction and UpdateUserAction could both extend a base class that encompasses common functionality. So, it's not necessary that splitting similar functions out into multiple actions will result in code duplication. –  Steven Benitez Aug 13 '12 at 13:34
@StevenBenitez:Nice point fully agree with this view –  Umesh Awasthi Aug 13 '12 at 18:13

The reason for allowing multiple methods in struts2 even in struts1 action classes has nothing to do with Object creation (Action class). The reason is to help developers group actions that perform similar actions in a single class. For example, if you have an Entity say product, you may want to create, modify or remove a product. For code organization purpose, it will be clean if you have these actions i.e create, modify and remove in a single action class. So for every request, an instance of your action class is created and it is destroyed after serving the request.

share|improve this answer
But the perfomance issues will come.Because each method may have 10 instance members, then memory for object creation will be 3times than with one method creation.. So how to overcome? –  Mr.Chowdary Aug 13 '12 at 4:58
@Laxmanchowdary I don't think there is performance issues following this paradigm. I might be wrong, i think it is easier to create and destroy objects than to maintain such objects in the memory for so long. Since such creation and destruction cycle takes a fraction of a second or more. –  Uchenna Nwanyanwu Aug 13 '12 at 5:09
Thanks for your time to resolve my doubt.. –  Mr.Chowdary Aug 13 '12 at 6:12
Whether the action has one method or 35 methods wont affect the number of instances of the action -- there will be one instance per request to an URL mapped to that action. Also, methods cannot have instance members, only classes can. –  Steven Benitez Aug 13 '12 at 13:37

Your Answer


