Action classes in Struts
In this post we will explore the features of Struts 2 action class and its capabilities.
Struts2 Actions are the most important and main part of struts2 Framework. They work as Model of MVC framework. Following article gives you more deep idea about Action classes role, configuration etc.
Action class Role:
1. Acts as Model encapsulates the business Logic: The main responsibility of Action class is to include business logic. The business logic will be encapsulated in execute method of the Action class.
2. The class helps the controller in result routing: The action class returns String, which helps to determine the view to be loaded. Example if struts.xml has following action definition
<action name=”Calculate”>
<result name=”SUCCESS”>/page1.jsp</result>
<result name=”ERROR”>/page2.jsp</result>
</action>
If the execute() method (of example.Calulator class) returns String “SUCCESS” then the page1.jsp will be chosen by controller to render and if execute() method returns “ERROR”, then page2.jsp will be chosen.
This behaviour is bit different than Struts 1. Struts 1 action classes used specify the routing information using ActionForward objects. This new approach of struts2 has made struts action classes much simpler and clean.
3. Action class acts as data carrier in Struts 2. This is a different behaviour and major difference from struts 1. Struts 1 Action classes were singleton, where one single instance of action class serves all the requests. The ActionBean carries data.
The struts2 action classes get instantiated per request. The ActionBeans have disappeared. The Action classes itself acts as data carrier. This approach again makes the action classes much cleaner and reusable.
Another major difference is the Action class execute method signature. The exculte method depends upon the servlet API. But the struts 2 action classes are simpler like any other POJO. The unit testing of Action classes have become easier as they dont rely on the HttpRequest and HttpResponse objects.
Next section will describe how the Action classes and be configured and their packaging.


Smitha,in struts2,is there no struts-config?
in struts1, it is there. and we define action,formbean etc here.
so struts.xml and struts-config.xml file is same?
struts.xml is same as stuts-config.xml file of struts 1. But the its format (defning action classes) has been changed.