Struts 2 Action


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

POJO implies you are not compelled to actualize any interface or broaden any class.

Generally, execute technique ought to be indicated that speaks to the business rationale. The straightforward 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 actualize 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 demonstrates that activity execution is effective and a triumph result ought to be appeared to the user.
  2. ERROR demonstrates that activity execution is fizzled and a blunder result ought to be appeared to the user.
  3. LOGIN demonstrates that client isn't signed in and a login result ought to be appeared to the user.
  4. INPUT demonstrates that approval is fizzled and an information result ought to be appeared to the client again.
  5. NONE demonstrates 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 actualized superseded by the activity class regardless of whether you are not forced.

  1. public String execute();  

Example of Struts Action that actualizes Action interface

If we actualize 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 actualizes 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