Struts 2 Action


In Struts 2, activity class is POJO (Plain Old Java Object).

POJO means you are not compelled to implement any interface or broaden any class.

Generally, execute method ought to be indicated that speaks to the business rationale. The simple activity class may look like:

Welcome.java
  1. package com.Javacodegeeks;  
  2. public class Welcome {  
  3. public String execute(){  
  4.     return "success";  
  5. }  
  6. }  

Action Interface

A helpful methodology is to implement the com.opensymphony.xwork2.Action interface that characterizes 5 constants and one execute method.

5 Constants of Action Interface

Action interface gives 5 constants that can be returned structure the activity class. They are:

  1. SUCCESS indicates that activity execution is effective and a triumph result ought to be appeared to the user.
  2. ERROR indicates that activity execution is fizzled and a blunder result ought to be appeared to the user.
  3. LOGIN indicates that client isn't signed in and a login result ought to be appeared to the user.
  4. INPUT indicates that approval is fizzled and an information result ought to be appeared to the client again.
  5. NONE indicates that activity execution is effective however no outcome ought to be appeared to the user.

Let's see what esteems are alloted 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 just a single strategy execute that ought to be implement d superseded by the activity class regardless of whether you are not forced.

  1. public String execute();  

Example of Struts Action that implement s Action interface

If we implement the Action interface, we can legitimately utilize the constants rather than values.

Welcome.java
  1. package com.Javacodegeeks;  
  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 an advantageous class that implement s numerous interfaces, for example, Action, Validateable, ValidationAware, TextProvider, LocaleProvider and Serializable . So it is generally utilized rather than Action.

Example of Struts Action that expands ActionSupport class

Let's see the case of Action class that expands the ActionSupport class.

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





javacodegeeks is optimized for learning.© javacodegeeks .
All Right Reserved and you agree to have read and accepted our term and condition