Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction🤔
2.
Action Context🎯
2.1.
Definition🤓
2.2.
Example🧐
2.3.
Field and Description🧱
2.4.
Class Declaration💭
2.5.
Constructor and Description💬
2.6.
ActionContext Methods✍
3.
Frequently Asked Questions
3.1.
What is Struts?
3.2.
What is Web Framework?
3.3.
What is API?
3.4.
What is Web App?
3.5.
What are five struts2 core concepts?
4.
Conclusion
Last Updated: Mar 27, 2024
Easy

Action Context

Author Sanchit Kumar
0 upvote

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❓

Struts

Action Context🎯

ActionContext

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.

ActionContext Objects

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 and their Description

Methods inherited from class java.lang.Object are given below

wait, toString, notifyAll, notify, hashCode, getClass, finalise, equals, clone.

Frequently Asked Questions

What is Struts?

Struts is a free, open-source framework for web applications written in Java. Apache Software Foundation maintains this framework.

What is Web Framework?

It is a software framework, also known as a web application framework (WAF), which helps in the creation of web applications, including web APIs, web services and web resources.

What is API?

API (Application Programming Interface) is a means of communication between two or more computer programs. It is a kind of software interface that provides a service to other software programs.

What is Web App?

A web App is a program delivered via the Internet via a browser interface and kept on a remote server.

What are five struts2 core concepts?

Interceptors, ValueStack, ActionContext, ActionInvocation and OGNL.

Conclusion

In this article, we discussed Action Context out of five struts2 core components Interceptors, ValueStack, ActionContext, ActionInvocation and OGNL.

See the official website for documentation and other information about Struts, and to learn about various Java frameworks, click here.

Refer to our guided paths on Coding Ninjas Studio to learn more about DSA, Competitive Programming, JavaScript, System Design, etc. Enrol in our courses and refer to the mock test and problems available. Take a look at the interview experiences and interview bundle for placement preparations.

Do upvote our blog to help other ninjas grow.

Coding Ninjas

Happy Learning Ninja! 🥷

Live masterclass