ActionContext类的作用

1 ActionContext和ContextMap

ActionContext是Struts2的工具类,该工具类可以用于操作ContextMap对象以及提供对四个域对象的操作方法。以下ActionContext的源码:

package com.opensymphony.xwork2;

import com.opensymphony.xwork2.inject.Container;
import com.opensymphony.xwork2.util.ValueStack;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

public class ActionContext implements Serializable {
    static ThreadLocal<ActionContext> actionContext = new ThreadLocal();
    public static final String ACTION_NAME = "com.opensymphony.xwork2.ActionContext.name";
    public static final String VALUE_STACK = "com.opensymphony.xwork2.util.ValueStack.ValueStack";
    public static final String SESSION = "com.opensymphony.xwork2.ActionContext.session";
    public static final String APPLICATION = "com.opensymphony.xwork2.ActionContext.application";
    public static final String PARAMETERS = "com.opensymphony.xwork2.ActionContext.parameters";
    public static final String LOCALE = "com.opensymphony.xwork2.ActionContext.locale";
    public static final String TYPE_CONVERTER = "com.opensymphony.xwork2.ActionContext.typeConverter";
    public static final String ACTION_INVOCATION = "com.opensymphony.xwork2.ActionContext.actionInvocation";
    public static final String CONVERSION_ERRORS = "com.opensymphony.xwork2.ActionContext.conversionErrors";
    public static final String CONTAINER = "com.opensymphony.xwork2.ActionContext.container";
    private Map<String, Object> context;

    public ActionContext(Map<String, Object> context) {
        this.context = context;
    }

    public void setActionInvocation(ActionInvocation actionInvocation) {
        this.put("com.opensymphony.xwork2.ActionContext.actionInvocation", actionInvocation);
    }

    public ActionInvocation getActionInvocation() {
        return (ActionInvocation)this.get("com.opensymphony.xwork2.ActionContext.actionInvocation");
    }

    public void setApplication(Map<String, Object> application) {
        this.put("com.opensymphony.xwork2.ActionContext.application", application);
    }

    public Map<String, Object> getApplication() {
        return (Map)this.get("com.opensymphony.xwork2.ActionContext.application");
    }

    public static void setContext(ActionContext context) {
        actionContext.set(context);
    }

    public static ActionContext getContext() {
        return (ActionContext)actionContext.get();
    }

    public void setContextMap(Map<String, Object> contextMap) {
        getContext().context = contextMap;
    }

    public Map<String, Object> getContextMap() {
        return this.context;
    }

    public void setConversionErrors(Map<String, Object> conversionErrors) {
        this.put("com.opensymphony.xwork2.ActionContext.conversionErrors", conversionErrors);
    }

    public Map<String, Object> getConversionErrors() {
        Map<String, Object> errors = (Map)this.get("com.opensymphony.xwork2.ActionContext.conversionErrors");
        if (errors == null) {
            errors = new HashMap();
            this.setConversionErrors((Map)errors);
        }

        return (Map)errors;
    }

    public void setLocale(Locale locale) {
        this.put("com.opensymphony.xwork2.ActionContext.locale", locale);
    }

    public Locale getLocale() {
        Locale locale = (Locale)this.get("com.opensymphony.xwork2.ActionContext.locale");
        if (locale == null) {
            locale = Locale.getDefault();
            this.setLocale(locale);
        }

        return locale;
    }

    public void setName(String name) {
        this.put("com.opensymphony.xwork2.ActionContext.name", name);
    }

    public String getName() {
        return (String)this.get("com.opensymphony.xwork2.ActionContext.name");
    }

    public void setParameters(Map<String, Object> parameters) {
        this.put("com.opensymphony.xwork2.ActionContext.parameters", parameters);
    }

    public Map<String, Object> getParameters() {
        return (Map)this.get("com.opensymphony.xwork2.ActionContext.parameters");
    }

    public void setSession(Map<String, Object> session) {
        this.put("com.opensymphony.xwork2.ActionContext.session", session);
    }

    public Map<String, Object> getSession() {
        return (Map)this.get("com.opensymphony.xwork2.ActionContext.session");
    }

    public void setValueStack(ValueStack stack) {
        this.put("com.opensymphony.xwork2.util.ValueStack.ValueStack", stack);
    }

    public ValueStack getValueStack() {
        return (ValueStack)this.get("com.opensymphony.xwork2.util.ValueStack.ValueStack");
    }

    public void setContainer(Container cont) {
        this.put("com.opensymphony.xwork2.ActionContext.container", cont);
    }

    public Container getContainer() {
        return (Container)this.get("com.opensymphony.xwork2.ActionContext.container");
    }

    public <T> T getInstance(Class<T> type) {
        Container cont = this.getContainer();
        if (cont != null) {
            return cont.getInstance(type);
        } else {
            throw new XWorkException("Cannot find an initialized container for this request.");
        }
    }

    public Object get(String key) {
        return this.context.get(key);
    }

    public void put(String key, Object value) {
        this.context.put(key, value);
    }
}

通过源码,我们发现ActionContext提供了get和put方法,分别从ContextMap取出数据和存入数据。

public Object get(String key) {
  return this.context.get(key);
}

public void put(String key, Object value) {
  this.context.put(key, value);
}

2 如何获取ActionContext对象?

通过ActionContext的源码,看到以下方法:

public static ActionContext getContext() {
    return (ActionContext)actionContext.get();
}

通过ActionContext.getContext()静态方法可以获取一个线程安全的ActionContext对象。之所以需要线程安全,因为要保证每个用户请求都有自己的ActionContext对象,互不影响。

热门文章

优秀文章