Introduction🤔
While using struts2 to create web applications, we should be familiar with five struts2 core concepts, which are Interceptors, ValueStack, ActionContext, ActionInvocation and OGNL. Here in this article, out of these five struts2 core components, we will discuss Action Context.
Are you ready to start❓
Action Context🎯
Definition🤓
The context in which an Action is executed is known as the ActionContext. Each context is simply a container of objects (that are related to the current request-response of a web application) that action requires for execution like the locale, parameters, session etc.
The objects are stored in a specific order.
Example🧐
Temporary Objects > Model Object > Action Object > Named Object.
The context map of an ActionContext has the following objects –
- ValueStack
- application
- session
- action
- parameters
- attr
- request
The ActionContext is thread local, which means that values stored in it are unique per thread. Because of this, you don't have to worry about a user-specific action context and no need to take extra care about making your actions thread-safe because of the thread local usage.
By using the getContext() function of the ActionContext class, we can refer to the current ActionContext. Therefore, the following method is used to refer to the current ActionContext -
ActionContext context = ActionContext.getContext();
Use the following to access objects from ValueStack -
ActionContext context = ActionContext.getContext().getValueStack();
Field and Description🧱
- ACTION_INVOCATION - It is the constant for the action's invocation context.
- ACTION_NAME - It is the constant for the name of the action being executed.
- APPLICATION - It is the constant for the action's application context.
- CONTAINER - It is the constant for the container
- CONVERSION_ERRORS - It is the constant for the map of type conversion errors.
- LOCALE - It is the constant for the action's locale.
- PARAMETERS - It is the constant for the action's parameters.
- SESSION - It is the constant for the action's session.
- VALUE_STACK - It is the constant for the OGNL value stack.
Note - Here Modifier and Type for each field is static String.
Class Declaration💭
Class ActionContext
java.lang.Object
com.opensymphony.xwork2.ActionContext
All Implemented Interfaces - Serializable
Direct Known Subclasses - ServletActionContext
Where com.opensymphony.xwork2 (Main XWork interfaces and classes) is the package, Action is interface and ActionContext is class.
public class ActionContext
extends Object
implements Serializable
Constructor and Description💬
The following code creates a new ActionContext initialized with another context.
ActionContext(Map<String,Object> context)
ActionContext Methods✍
Methods inherited from class java.lang.Object are given below
wait, toString, notifyAll, notify, hashCode, getClass, finalise, equals, clone.