Sunday, March 1, 2009

Creating Action Classes In Struts 2

This blog post intends to answer the following questions : -
  1. How to create Action classes in Struts 2?
  2. Can we use POJO's as action classes in Struts 2?
  3. How to use a POJO as an action class in Struts 2?
  4. What are the interfaces or classes one needs to extend to create an action class in Struts 2?
The Struts 2 programming model makes it very easy to create action classes. The Dependency Injection pattern enables us to add Struts 2 functionality to a variety of classes.

Basically Struts 2 allows you to create Action classes in three different ways :-
  1. Implement the org.apache.struts.action.Action interface.
  2. Extend the com.opensymphony.xwork2.ActionSupport class.
  3. Or simply use any POJO as an Action class.
Implementing the Action interface is very useful in cases when your class belongs to some other class hierarchy. Extending the ActionSupport class enables us to to make use of a large number of features and constants that are available through the various interfaces implemented by the ActionSupport class. ActionSupport provides empty implementations of a large number of abstract methods for many interfaces.

Using a POJO is quite simple as well. All that is required of you is to create method in the POJO with the following signature : -

public String execute()
{
//method code
}

So finally, getting down to the actual business details is not gonna take millions of years! Have fun!

Happy Programming ;)

Signing Off
Ryan

No comments: