• No results found

ActionContext Class

In document Struts2 Black Book (Page 72-76)

An action context can be defined here as a container, which contains objects that an action requires for its execution. This context is represented by ActionContext class and can provide objects, like request, response, session, parameters locale, etc. The objects stored in the ActionContext are unique per thread as this class is thread locale. We can obtain the reference of this class using its own static getContext() method as shown here:

ActionContext context = ActionContext.getContext();

The execute() method of Struts 2 action classes does not take any argument and, hence, we need some technique to access objects, like request, response, session, etc. ActionContext helps in obtaining these objects in our action class. The following code sample shows the use of ActionContext class:

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpSession;

import com.opensymphony.xwork2.ActionSupport;

import com.opensymphony.xwork2.ActionContext;

import org.apache.struts2.ServletActionContext;

public class SomeAction extends ActionSupport { . . . .

public String execute() throws Exception { . . . .

. . . .

ActionContext acx=ActionContext.getContext();

HttpServletRequest request=

(HttpServletRequest)acx.get(ServletActionContext.HTTP_REQUEST);

request.setAttribute("name", "John");

HttpSession session=

(HttpSession)acx.get(ServletActionContext.SESSION);

session.setAttribute("user", "kogent");

return SUCCESS;

} }

After obtaining an ActionContext object using getContext() method, we can use the get() method on this object to obtain some other useful objects. The ActionContext.get() method returns an object stored in the current ActionContext by looking up the value for the key passed as an argument to it. Tables 3.6 and 3.7 show the fields and methods, respectively, of the ActionContext class.

Table 3.6: Fields of ActionContext class

Field Name Description static String

ACTION_INVOCATION

It is constant for the action’s invocation context

static String ACTION_NAME It is constant for the name of the action being executed (package private) static

ThreadLocal actionContext

It is a static thread local variable which is used by different threads independently

static String APPLICATION It is constant for the action’s application context (package private)

Map context

It is a Map type of field to be used to store key/value pairs for a given context.

static String CONVERSION_ERRORS

It is constant for the map of type conversion errors

static String LOCALE It is constant for the action’s locale static String PARAMETERS It is constant for the action’s parameters static String SESSION It is constant for the action’s session static String

TYPE_CONVERTER

It is constant for the action’s type converter

static String VALUE_STACK It is constant for the OGNL Value Stack

Table 3.7: Methods of ActionContext class

Method Description

Object get(Object key) It returns a value that is stored in the current ActionContext by doing a lookup using the value’s key ActionInvocation

getActionInvocation()

It gets the action invocation (the execution state)

Map getApplication() It returns a Map of the ServletContext when in a Servlet environment or a generic application level Map otherwise

static ActionContext getContext()

It returns the ActionContext specific to the current thread

Map getContextMap() It gets the context map

Table 3.7: Methods of ActionContext class

Method Description

Map getConversionErrors() It gets the map of conversion errors, which occurred while executing the action

Locale getLocale() It gets the Locale of the current action String getName() It gets the name of the current Action

Map getParameters() It returns a Map of the HttpServletRequest parameters when in a Servlet environment or a generic Map of parameters, otherwise

Map getSession() It gets the Map of HttpSession values when in a Servlet environment or a generic session map, otherwise

ValueStack getValueStack()

It gets the OGNL Value Stack

void put(Object key, Object value)

It stores a value in the current ActionContext

void setActionInvocation (ActionInvocation

actionInvocation)

It sets the action invocation (the execution state)

void setApplication (Map application)

It sets the action’s application context

static void setContext (ActionContext context)

It sets the action context for the current thread

void setContextMap (Map contextMap)

It sets the action’s context map

void setConversionErrors (Map conversionErrors)

It sets conversion errors which occurred when executing the action

void setLocale (Locale locale)

It sets the Locale for the current action

void setName(String name) It sets the name of the current Action in the ActionContext

void setParameters (Map parameters)

It sets the action parameters

void setSession (Map session)

It sets a map of action session values

void setValueStack (ValueStack stack)

It sets the OGNL Value Stack

Another class, which stores web-specific context information for actions is org.apache.struts2.ServletActionContext class. ServletActionContext class is a subclass of ActionContext. In addition to extending fields and methods from ActionContext, the ServletActionContext class also implements org.apache.struts2.StrutsStatics interface.

Tables 3.8 and 3.9 show the fields and methods, respectively, of ServletActionContext class.

Table 3.8: Fields of ServletActionContext class Field Name Description static String

ACTION_MAPPING

A static final String type field having value

"struts.actionMapping"

private static long serialVersionUID

Serial version id of the ServletActionContext class and its value is

-666854718275106687L static String

STRUTS_VALUESTACK_KEY

Key for the associated Value Stack and its value is

"struts.valueStack"

Table 3.9: Methods of ServletActionContext class Methods Description static ActionContext

getActionContext(HttpServ letRequest req)

It gets the current action context

static ActionMapping getActionMapping()

It gets the action mapping for this context

static PageContext getPageContext()

It returns the HTTP page context

static HttpServletRequest getRequest()

It gets the HTTP Servlet request object

static

HttpServletResponse getResponse()

It gets the HTTP servlet response object

static ServletContext getServletContext()

It gets the Servlet context

static ValueStack

getValueStack(HttpServlet Request req)

It gets the current Value Stack for this request

static void setRequest (HttpServletRequest request)

It sets the HTTP Servlet request object

Table 3.9: Methods of ServletActionContext class Methods Description static void setResponse

(HttpServletResponse response)

It sets the HTTP Servlet response object

static void

setServletContext(Servlet Context servletContext)

It sets the current Servlet context object

In addition to these fields and methods, ServletActionContext class inherits all fields and methods from its parent class, i.e. ActionContext and the StrutsStatics interface implemented by it. The StrutsStatics interface contains constants used by Struts. Table 3.10 shows these constants fields.

Table 3.10: Constants fields of StrutsStatics interface Field Name Description static String

HTTP_REQUEST

It is constant for the HTTP request object

static String HTTP_RESPONSE

It is constant for the HTTP response object

static String PAGE_CONTEXT

It is constant for the JSP page context

static String SERVLET_CONTEXT

It is constant for the Servlet context object

static String SERVLET_DISPATCHER

It is constant for an HTTP request dispatcher

static String

STRUTS_PORTLET_CONTEXT

It is constant for the PortletContext object

In document Struts2 Black Book (Page 72-76)