Struts - SPLessons
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Struts 2 Action

Struts 2 Action

shape Description

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.

shape 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]

shape Methods

If one want to implement the Action interface, then need to import the com.opensymphony.xwork2.Action package. Action class contains 5 constant values and 1 execute() method.
  • SUCCESS
  • ERROR
  • LOGIN
  • INPUT
  • NONE

shape 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.

shape 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

shape 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 .