Mr.Chowdary
Mr.Chowdary

Reputation: 3407

Any use of struts 2 action class multiple methods?

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 ???

Upvotes: 0

Views: 1979

Answers (2)

Umesh Awasthi
Umesh Awasthi

Reputation: 23587

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.

Upvotes: 2

Uchenna Nwanyanwu
Uchenna Nwanyanwu

Reputation: 3204

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.

Upvotes: 1

Related Questions