Home » Struts » Action Class in Struts Framework

Action Class in Struts Framework

Introduction :

Action Class in Struts framework defines the business logic. An action class handles the client request and prepares the response. It also decides where the response should be forwarded. Basically an action class receives data from the presentation layer and forwards the data to the corresponding business layer. It also processes the data which comes from the business layer and forwards them back to the presentation layer.

Action classes are used to invoke the classes at the business layer or data access layer to get the data from the bean and store the processed data and return the result or error depending upon the situation.

These classes are multi threaded. Hence a developer has to be careful while handling the action variable as they are not thread safe.

Types Of Action Class :

We have following types of Action Classes in struts:

  • Action
  • DispatchAction
  • LoookUpDispatchAction
  • MappingDispatchAction
  • IncludeAction
  • ForwardAction
  • SwitchAction
  • LocaleAction
  • DownloadAction


Action :

Action class is available in org.apache.struts.action package.

When you extends Action class you need to overwrite execute() method with following four parameters.

  1. ActionMapping
  2. ActionForm
  3. HttpServletRequest
  4. HttpServletResponse

For the entire application only one instance will be created for the Action class and the same instance will be accessed by multiple threads concurrently because of this we should not declare any instance variable inside the Action class which is going to store Client specific data.

When you use Action class as super class we need to write multiple java class for multiple requirements.

In struts 1.0 Action class has perform() method instead of execute() method ,with the following signature :


Public ActionForward perform(AM,AF,H.S.R,H.S.R)throws ServletException, IOException
{
...
}

perform() method was deprecated in struts 1.1 because Perform() method is throwing ServletException, IOException , when you are overriding that in sub class you can throw only ServletException and IOException and its sub classes. It should not throw any other new Exception whereas in the case of execute() method we can throw any kind of exception because its method level exception is java.lang.Exception.

DispatchAction :

Struts DispatchAction can group similar action classes into a single action class having different methods.

In this action excluding the execute method we can write our own user defined methods.The parameters are same given by struts framework (mapping,form,request,response).

Problem with DispatchAction:

Because of writing action class method name inside the Jsp Action classes will be tightly coupled in JSP’s i.e when action classes method name is changed JSP should be modified this gives you maintenance problems remaining same as action class.

LookupDispatchAction :

LookupDispatchAction class can group the similar functions in to a single action class by using propeties file and overriding getKeyMethodMap() of HashMap.

MappingDispatchAction :

This class is available in org.apache.struts.action package.

This class functionality is similar to DispatchAction. But DispatchAction has the maintenance problem .to avoid that problem LookupDispatchAction was introduced but LookupDispatchAction implementation is bit complicated (unnecessary you had to write many keys inside the property file and had to override get KeyMethodMap() method in all the Action class.

To avoid the complication with LookupDispatchAction , MappingDispatchAction was introduced in Struts 1.2

IncludeAction :

This is available in org.apache.struts.action package.

This action will be used to include one JSP in other JSP.

ForwardAction :

It is used to forward the request from one JSP to another Jsp.

In page ForwardAction is used to forward the request from one page to another page.

LocaleAction :

It is available in org.apache.struts.action package and is added in struts 1.2.

This action will be used to change the current Locale to required Locale dynamically.

DownloadAction :

it is available in org.apache.struts.action package and is added in struts 1.2.

DownloadAction will be used to download the file from the server..

SwitchAction :

It is available in org.apache.struts.action package and is available from struts 1.1.

SwitchAction will be used to switch the control from one struts module to another struts module.


Previous Next Article

comments powered by Disqus