Cheap VPS & Xen Server

Residential Proxy Network - Hourly & Monthly Packages

Struts 2 Action


In struts 2, action class is POJO (Plain Old Java Object).

POJO means you are not forced to implement any interface or extend any class.

Generally, execute method should be specified that represents the business logic. The simple action class may look like:

Welcome.java

  1. package com.Kreationnext;
  2. public class Welcome {
  3. public String execute(){
  4.     return “success”;
  5. }
  6. }

Action Interface

A convenient approach is to implement the com.opensymphony.xwork2.Action interface that defines 5 constants and one execute method.

5 Constants of Action Interface

Action interface provides 5 constants that can be returned form the action class. They are:

  1. SUCCESS indicates that action execution is successful and a success result should be shown to the user.
  2. ERROR indicates that action execution is failed and a error result should be shown to the user.
  3. LOGIN indicates that user is not logged-in and a login result should be shown to the user.
  4. INPUT indicates that validation is failed and a input result should be shown to the user again.
  5. NONE indicates that action execution is successful but no result should be shown to the user.

Let’s see what values are assigned to these constants:

  1. public static final String SUCCESS = “success”;
  2. public static final String ERROR = “error”;
  3. public static final String LOGIN  = “login”;
  4. public static final String INPUT = “input”;
  5. 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.

  1. public String execute();

Example of Struts Action that implements Action interface

If we implement the Action interface, we can directly use the constants instead of values.

Welcome.java

  1. package com.Kreationnext;
  2. import com.opensymphony.xwork2.Action;
  3. public class Welcome implements Action{
  4. public String execute(){
  5.     return SUCCESS;
  6. }
  7. }

ActionSupport class

It is a convenient class that implements many interfaces such as Action, Validateable, ValidationAware, TextProvider, LocaleProvider and Serializable . So it is mostly used instead of Action.

Example of Struts Action that extends ActionSupport class

Let’s see the example of Action class that extends the ActionSupport class.

Welcome.java

  1. package com.Kreationnext;
  2. import com.opensymphony.xwork2.ActionSupport;
  3. public class Welcome extends ActionSupport{
  4. public String execute(){
  5.     return SUCCESS;
  6. }
  7. }

Comments

comments