Class ActionContext

java.lang.Object
org.apache.struts2.ActionContext
All Implemented Interfaces:
Serializable

public class ActionContext extends Object implements Serializable

The ActionContext is the context in which an Action is executed. Each context is basically a container of objects an action needs for execution like the session, parameters, locale, etc.

The ActionContext is thread local which means that values stored in the ActionContext are unique per thread. See the ThreadLocal class for more information. The benefit of this is you don't need to worry about a user specific action context, you just get it:

ActionContext context = ActionContext.getContext();

Finally, because of the thread local usage you don't need to worry about making your actions thread safe.

Author:
Patrick Lightbody, Bill Lynch (docs)
See Also:
  • Constructor Details

    • ActionContext

      protected ActionContext(Map<String,Object> context)
      Creates a new ActionContext initialized with another context.
      Parameters:
      context - a context map.
  • Method Details

    • of

      public static ActionContext of(Map<String,Object> context)
      Creates a new ActionContext based on passed in Map
      Parameters:
      context - a map with context values
      Returns:
      new ActionContext
    • of

      public static ActionContext of()
      Creates a new ActionContext based on empty Map
      Returns:
      new ActionContext
    • bind

      public static ActionContext bind(ActionContext actionContext)
      Binds the provided context with the current thread
      Parameters:
      actionContext - context to bind to the thread
      Returns:
      context which was bound to the thread
    • containsValueStack

      public static boolean containsValueStack(Map<String,Object> context)
    • bind

      public ActionContext bind()
      Binds this context with the current thread
      Returns:
      this context which was bound to the thread
    • clear

      public static void clear()
      Wipes out current ActionContext, use wisely!
    • getContext

      public static ActionContext getContext()
      Returns the ActionContext specific to the current thread.
      Returns:
      the ActionContext for the current thread, is never null.
    • withActionInvocation

      public ActionContext withActionInvocation(ActionInvocation actionInvocation)
      Sets the action invocation (the execution state).
      Parameters:
      actionInvocation - the action execution state.
    • getActionInvocation

      public ActionInvocation getActionInvocation()
      Gets the action invocation (the execution state).
      Returns:
      the action invocation (the execution state).
    • withApplication

      public ActionContext withApplication(Map<String,Object> application)
      Sets the action's application context.
      Parameters:
      application - the action's application context.
    • getApplication

      public Map<String,Object> getApplication()
      Returns a Map of the ServletContext when in a servlet environment or a generic application level Map otherwise.
      Returns:
      a Map of ServletContext or generic application level Map
    • getContextMap

      public Map<String,Object> getContextMap()
      Gets the context map.
      Returns:
      the context map.
    • withConversionErrors

      public ActionContext withConversionErrors(Map<String,ConversionData> conversionErrors)
      Sets conversion errors which occurred when executing the action.
      Parameters:
      conversionErrors - a Map of errors which occurred when executing the action.
    • getConversionErrors

      public Map<String,ConversionData> getConversionErrors()
      Gets the map of conversion errors which occurred when executing the action.
      Returns:
      the map of conversion errors which occurred when executing the action or an empty map if there were no errors.
    • withLocale

      public ActionContext withLocale(Locale locale)
      Sets the Locale for the current action.
      Parameters:
      locale - the Locale for the current action.
    • getLocale

      public Locale getLocale()
      Gets the Locale of the current action. If no locale was ever specified the platform's default locale is used.
      Returns:
      the Locale of the current action.
    • withActionName

      public ActionContext withActionName(String actionName)
      Sets the name of the current Action in the ActionContext.
      Parameters:
      actionName - the name of the current action.
    • getActionName

      public String getActionName()
      Gets the name of the current Action.
      Returns:
      the name of the current action.
    • withParameters

      public ActionContext withParameters(HttpParameters parameters)
      Sets the action parameters.
      Parameters:
      parameters - the parameters for the current action.
    • getParameters

      public HttpParameters getParameters()
      Returns a Map of the HttpServletRequest parameters when in a servlet environment or a generic Map of parameters otherwise.
      Returns:
      a Map of HttpServletRequest parameters or a multipart map when in a servlet environment, or a generic Map of parameters otherwise.
    • withSession

      public ActionContext withSession(Map<String,Object> session)
      Sets a map of action session values.
      Parameters:
      session - the session values.
    • getSession

      public Map<String,Object> getSession()
      Gets the Map of HttpSession values when in a servlet environment or a generic session map otherwise.
      Returns:
      the Map of HttpSession values when in a servlet environment or a generic session map otherwise.
    • withValueStack

      public ActionContext withValueStack(ValueStack valueStack)
      Sets the OGNL value stack.
      Parameters:
      valueStack - the OGNL value stack.
    • getValueStack

      public ValueStack getValueStack()
      Gets the OGNL value stack.
      Returns:
      the OGNL value stack.
    • withContainer

      public ActionContext withContainer(Container container)
      Gets the container for this request
      Parameters:
      container - The container
    • getContainer

      public Container getContainer()
      Sets the container for this request
      Returns:
      The container
    • getInstance

      public <T> T getInstance(Class<T> type)
    • get

      public Object get(String key)
      Returns a value that is stored in the current ActionContext by doing a lookup using the value's key.
      Parameters:
      key - the key used to find the value.
      Returns:
      the value that was found using the key or null if the key was not found.
    • put

      public void put(String key, Object value)
      Stores a value in the current ActionContext. The value can be looked up using the key.
      Parameters:
      key - the key of the value.
      value - the value to be stored.
    • getServletContext

      public jakarta.servlet.ServletContext getServletContext()
      Gets ServletContext associated with current action
      Returns:
      current ServletContext
    • withServletContext

      public ActionContext withServletContext(jakarta.servlet.ServletContext servletContext)
      Assigns ServletContext to action context
      Parameters:
      servletContext - associated with current request
      Returns:
      ActionContext
    • getServletRequest

      public jakarta.servlet.http.HttpServletRequest getServletRequest()
      Gets ServletRequest associated with current action
      Returns:
      current ServletRequest
    • withServletRequest

      public ActionContext withServletRequest(jakarta.servlet.http.HttpServletRequest request)
      Assigns ServletRequest to action context
      Parameters:
      request - associated with current request
      Returns:
      ActionContext
    • getServletResponse

      public jakarta.servlet.http.HttpServletResponse getServletResponse()
      Gets ServletResponse associated with current action
      Returns:
      current ServletResponse
    • withServletResponse

      public ActionContext withServletResponse(jakarta.servlet.http.HttpServletResponse response)
      Assigns ServletResponse to action context
      Parameters:
      response - associated with current request
      Returns:
      ActionContext
    • getPageContext

      public jakarta.servlet.jsp.PageContext getPageContext()
      Gets PageContext associated with current action
      Returns:
      current PageContext
    • withPageContext

      public ActionContext withPageContext(jakarta.servlet.jsp.PageContext pageContext)
      Assigns PageContext to action context
      Parameters:
      pageContext - associated with current request
      Returns:
      ActionContext
    • getActionMapping

      public ActionMapping getActionMapping()
      Gets ActionMapping associated with current action
      Returns:
      current ActionMapping
    • withActionMapping

      public ActionContext withActionMapping(ActionMapping actionMapping)
      Assigns ActionMapping to action context
      Parameters:
      actionMapping - associated with current request
      Returns:
      ActionContext
    • withExtraContext

      public ActionContext withExtraContext(Map<String,Object> extraContext)
      Assigns an extra context map to action context
      Parameters:
      extraContext - to add to the current action context
      Returns:
      ActionContext
    • with

      public ActionContext with(String key, Object value)
      Adds arbitrary key to action context
      Parameters:
      key - a string
      value - an object
      Returns:
      ActionContext
    • equals

      public final boolean equals(Object obj)
      Overrides:
      equals in class Object
    • hashCode

      public final int hashCode()
      Overrides:
      hashCode in class Object