In Struts 2 Action, Action class is a POJO (Plain Old Java Object ) class. POJO class contain setters and getters methods and constructors. The Action class contain some setters, getters, execute() method. In Struts 2 Action class use the FilterDispatcher class. Action class is used to transfer the data from the request to the View pages like JSP, Freemarker. It map the URL which is mapped to each Action class and execute the Action class and forward the result type or response on View pages. Action is placed in struts.xml file where action name and class name with package extension will be written and one more important point is under the action class result tag will be used which uses constant values such as success, login.
Conceptual
figure
Struts 2 Action class contain one execute() method. Whenever, Action class object create the container, at the same time the time execute() will be executed and it returns result in String type.
[java]
package com.splesson;
public class ActionClass
{
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String execute()
{
return "success";
}
}[/java]
Methods
If one want to implement the Action interface, then need to import the com.opensymphony.xwork2.Actionpackage. Action class contains 5 constant values and 1 execute() method.
SUCCESS
ERROR
LOGIN
INPUT
NONE
If one write the SUCCESS in Action class, that action execution is successful and a success result should be displayed on Browser.
If one write the ERROR in Action class, that action execution is failed and Error result should be displayed on Browser.
If one write the LOGIN in Action class, that user is not logged-in and a login result should be displayed on Browser.
If one write the INPUT in Action class, that Action class contain validate() method not executed and given the input values will be displayed on Browser.
If one write the NONE in Action class, that Action class is successfully executed. But, can't display the result on the browser.
Example
An example of Action interfaces implementation.
[java]
package com.splesson;
import com.opensymphony.xwork2.Action;
public class SPLessons implements Action
{
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";
public String execute()
{
return "success";
}
}
[/java]
Action Class is provided from Action Support class. If one extened the ActionSupport class then it will be provided more interfaces like Action, ValidationAware,Validateable, TextProvider, LocaleProvider and Serializable. AcctionSupport class contain com.opensymphony.xwork2.ActionSupport package.
Example
Example of ActionSupport interface implementation.
[java]
package com.splesson;
import com.opensymphony.xwork2.ActionSupport;
public class SPLessons extends ActionSupport
{
public String execute()
{
return "SUCCESS";
}
}
[/java]
Summary
Key Points
The action element is utilized in struts.xml .
Execute() method will be used by action element.
Struts 2 Action class is used to transfer the data from the request to the View pages .