Types of Actions class in Struts 2

 
Action class in Struts 2
 
The functionality of the action class is to retrieve resource bundle, hold the data, provide validation, perform business logic and select the view result page that should be sent back to the user.
 
Request Processing Life Cycle Workflow in Struts 2

There are four different ways to write struts 2 action class , which are as follows
 

1. Action

 
For Struts 2 actions, it is not mandatory to implement any interface or extend any class. It is only required to implement execute() method that returns a string which is to be used in struts.xml, to indicate the result page that has to be rendered(return)
 
** UPDATE: Struts 2 Complete tutorial now available here.
 

package com.action;
public class LoginAction
{
     public String execute()
     {
     return "success";
     }
}

 
In the struts.xml, configure the action class with action tag and class attribute. Define which result page should be returned to the user with result tag and the name of the action you can use to access this action class with name attribute.
 

<package name="default" extends="struts-default">
  <action name="login" class="com.action.LoginAction">
      <result name="success">success.jsp</result>
  </action>
<package>

 
Now you can access the action via

http://localhost:8089/StrutsLogin/login

[box]
Note :
To change the extension to any value that suits your need, view – Struts2 Custom extension example.
[/box]
 

2. Action interface

 
The second way of creating Action class on Struts 2 is to implement an optional action interface (com.opensymphony.xwork2.Action).
 
This interface , comes with 5 common used constant values : success, error, none, input and logic. By implements this interface, the action class can use the constant value directly.
 
The Source code for Action interface :

package com.opensymphony.xwork2;
public interface Action
{
    public static final String SUCCESS = "success";
    public static final String NONE = "none";
    public static final String ERROR = "error";
    public static final String INPUT = "input";
    public static final String LOGIN = "login";
    public String execute() throws Exception;
}

 
Example: Struts 2 Action class implementing Action interface
 

package com.action;
import com.opensymphony.xwork2.Action;
public class LoginAction implements Action
{
    public String execute()
    {
	return SUCCESS;
    }
}

 

3. ActionSupport

 
The third way of creating Action class on Struts 2 is to extend the ActionSupport class (com.opensymphony.xwork2.ActionSupport). The ActionSupport class is a very powerful and convenient class that provides default implementation of few of the important interfaces :
 

public class ActionSupport implements Action, Validateable,
   ValidationAware, TextProvider, LocaleProvider, Serializable
{
 ...
}

 
[box ]
Note:

Instead of implementing Action interface, it is better to extend the ActionSupport, Since the ActionSupport class implements Action interface along with other useful interfaces.
[/box]
 
The ActionSupport class give you the ability to do :
1. Validation Declare a validate() method and put the validation code inside.
2. Text localization Use GetText() method to get the message from resource bundle.
 
LoginAction.properties, (The resource bundle name must be same as Action class name with the extension .properties), and should be in the same folder where the action class lies
 
[box ]
Note :

We will get to know about what is property file and the use of addFiendError , and use of other functionality like addActionError and addActionMessage in our upcoming tutorials.
[/box]
 
Example: Struts 2 Action class extending ActionSupport class
 

package com.action;
import com.opensymphony.xwork2.ActionSupport;

public class LoginAction extends ActionSupport
{
private static final long serialVersionUID = 6677091252031583948L;
private String userName;

public String execute()
       {
	return SUCCESS;
       }

	public String getUserName()
        {
		return userName;
	}

	public void setUserName(String userName)
        {
		this.userName = userName;
	}

	public void validate()
        {
	   if (!userName.isEmpty())
           {
	   addFieldError("userName", getText("username.required"));
	   }
       }
}

 
** UPDATE: Android Complete tutorial now available here.
 

4. Action annotation

 
Struts 2 has very good support for annotations, you can get rid of the XML file and replace with @action in your action class.
 

package com.simplecode.action;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.ResultPath;
import com.opensymphony.xwork2.ActionSupport;
@ResultPath(value="/")
public class LoginAction extends ActionSupport
{
@Action(value="login", results={@Result(name="success", location="success.jsp")})
	public String execute()
        {
 	return SUCCESS;
 	}
}

 

About Mohaideen Jamil