Action interface vs ActionSupport class
Table of Contents
1. Action Interface:
- Definition: Â The Action interface is a part of the Struts framework and defines the contract that an action class must implement.
- Key Points:
- Defines methods (execute method primarily) that an action class must override.
- Provides flexibility as it allows the action class to extend any other class, not just those provided by Struts.
 2. ActionSupport Class:
- Definition: Â ActionSupport is an abstract base class provided by the Struts framework that implements the Action interface.
- Key Points:
- Implements common functionality like error handling and validation that most action classes typically require.
- Provides convenience methods and properties that simplify common tasks in web applications.
- Extending ActionSupport can save effort in handling common tasks, as it provides default implementations.
Example in Java
Let’s illustrate these concepts with a simple Java example using Struts.
Using Action Interface
java
import com.opensymphony.xwork2.Action;
public class MyAction implements Action {
private String message;
@Override
public String execute() throws Exception {
// Business logic to process the request
message = "Hello from MyAction!";
return SUCCESS; // SUCCESS is typically a constant defined in Struts
}
// Getter and Setter for message property
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
Explanation:
- The MyAction class implements the Action interface directly.
- It overrides the execute method where the main business logic for handling the request is implemented.
- It provides a message property along with its getter and setter.
Using ActionSupport Class
java
import com.opensymphony.xwork2.ActionSupport;
public class MyAction extends ActionSupport {
private String message;
@Override
public String execute() throws Exception {
// Business logic to process the request
message = "Hello from MyAction!";
return SUCCESS; // SUCCESS is typically a constant defined in ActionSupport
}
// Getter and Setter for message property
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
Explanation:
- The MyAction class extends ActionSupport, which already implements the Action interface.
- It inherits common functionality like validation, error handling, and result handling from ActionSupport.
- It provides a message property along with its getter and setter, similar to the previous example.
Conclusion
- Preference:
- Action Interface: Â Use when you need maximum flexibility and when the action class needs to extend another class that’s not ActionSupport.
- ActionSupport Class: Â Use when you want to leverage the common functionalities provided by Struts, such as error handling and result management, and when your action class does not need to extend another specific class.
In practice, unless you have a specific need to extend another class or require custom handling not provided by ActionSupport, using ActionSupport is often more convenient and aligns well with the typical requirements of Struts-based applications.