Struts 2 Action Interface

Struts 2 Action Interface

In this article, I am going to discuss the Struts 2 Action Interface. Please read our previous article, where we discussed the Struts 2 Core Components. At the end of this article, you will understand the following pointers in detail.

  1. Action Interface
  2. 5 Constants of Action Interface
  3. Example of Struts Action that implements Action interface
  4. ActionSupport class
Struts 2 Action Interface

The com.opensymphony.xwork2.Action interface defines 5 constants and one execute() method, so it will be a better approach to use it.

Constants of Action Interface

The 5 constants are provided by the Action Interface which can be returned from the action class.

Those are:

  1. SUCCESS – it specifies that the action execution became successful as well as the user can see the success result.
  2. ERROR – it specifies that the action execution go failed and an error result is displayed to the user.
  3. LOGIN – it specifies that the user is not logged in and a login related result is shown to the user.
  4. INPUT – it specifies that the validation is failed and an input result must be shown to the user again.
  5. NONE – it denotes that the action execution became successful but no result is shown to the user.

Following are the values that are assigned to the constants –

public static final String SUCCESS = "success";  
public static final String ERROR = "error";  
public static final String LOGIN = "login";  
public static final String INPUT = "input";  
public static final String NONE = "none";
Method of Action Interface

Action interface contains only one method execute that should be implemented overridden by the action class even if you are not forced.

public String execute();

Example of Struts Action That Implements Action Interface

If the Action interface is implemented then instead of values we can directly use the constants.

Welcome.java

package com.info;
import com.opensymphony.xwork2.Action;
public class Welcome implements Action
{
    public String execute ()
    {
        return SUCCESS;
    }
}
ActionSupport Class

Instead of using Action, it is a suitable class that implements many interfaces like Serializable, TextProvider, LocaleProvider, Validateable, ValidationAware. In the below example, the Action class is extending the ActionSupport class.

Welcome.java

package com.info;
import com.opensymphony.xwork2.ActionSupport;
public class Welcome extends ActionSupport
{
    public String execute ()
    {
        return SUCCESS;
    }
}

In the next article, I am going to discuss Struts 2 Configuration Files. Here, in this article, I try to explain the Struts 2 Action Interface and I hope you enjoy this Struts 2 Action Interface article.

Leave a Reply

Your email address will not be published. Required fields are marked *