Struts2系列:(16)ActionContext

本文解决下面几个问题:

1、ActionContext是什么?

2、如何获取到ActionContext对象?

3、ActionContext的核心结构是什么?

4、ActionContext定义的一些常量

5、ActionContext的完整源代码

1、ActionContext是什么?

ActionContext是Action运行的上下文环境。本质上来说,每一个context就是对象的容器。

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.

2、如何获取到ActionContext对象?

ActionContext context = ActionContext.getContext();

3、ActionContext的核心结构是什么?

ActionContext的核心结构是维护了一个Map<String, Object>类型的context变量。数据的存储和读取都是通过context对象来实现了。

下面是一个简略版本的ActionContext类的实现:

/**
 * ActionContext是Action运行的上下文环境。本质上来说,每一个context就是对象的容器。
 * 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. 
 * 
 * Action是thread local,是线程安全的。
 * The ActionContext is thread local which means that values stored in the ActionContext are
 * unique per thread. 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
 * @author Bill Lynch (docs)
 */
public class ActionContext implements Serializable {

	/**
	 * 1、为了线程安全,而做一些准备工作
	 **/
    static ThreadLocal<ActionContext> actionContext = new ThreadLocal<ActionContext>();

	//1.1、设置或获取ActionContext对象
    public static void setContext(ActionContext context) {
        actionContext.set(context);
    }

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

	/**
	 * 2、ActionContext的核心存储结构:Map<String, Object> context
	 **/
    private Map<String, Object> context;

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

	//2.1、设置和获取context对象
    public void setContextMap(Map<String, Object> contextMap) {
        getContext().context = contextMap;
    }

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

	//2.2、向context对象中存储数据和从context对象中读取数据
    public Object get(String key) {
        return context.get(key);
    }

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

4、ActionContext定义的一些常量

4.1、ACTION_NAME常量

    //Constant for the name of the action being executed.
    public static final String ACTION_NAME = "com.opensymphony.xwork2.ActionContext.name";

    /**
     * Sets the name of the current Action in the ActionContext.
     *
     * @param name the name of the current action.
     */
    public void setName(String name) {
        put(ACTION_NAME, name);
    }

    /**
     * Gets the name of the current Action.
     *
     * @return the name of the current action.
     */
    public String getName() {
        return (String) get(ACTION_NAME);
    }

4.2、VALUE_STACK常量

    //Constant for the {@link com.opensymphony.xwork2.util.ValueStack OGNL value stack}.
    public static final String VALUE_STACK = ValueStack.VALUE_STACK;

    /**
     * Sets the OGNL value stack.
     *
     * @param stack the OGNL value stack.
     */
    public void setValueStack(ValueStack stack) {
        put(VALUE_STACK, stack);
    }

    /**
     * Gets the OGNL value stack.
     *
     * @return the OGNL value stack.
     */
    public ValueStack getValueStack() {
        return (ValueStack) get(VALUE_STACK);
    }

4.3、ACTION_INVOCATION常量

    //Constant for the action‘s {@link com.opensymphony.xwork2.ActionInvocation invocation} context.
    public static final String ACTION_INVOCATION = "com.opensymphony.xwork2.ActionContext.actionInvocation";

    /**
     * Sets the action invocation (the execution state).
     *
     * @param actionInvocation the action execution state.
     */
    public void setActionInvocation(ActionInvocation actionInvocation) {
        put(ACTION_INVOCATION, actionInvocation);
    }

    /**
     * Gets the action invocation (the execution state).
     *
     * @return the action invocation (the execution state).
     */
    public ActionInvocation getActionInvocation() {
        return (ActionInvocation) get(ACTION_INVOCATION);
    }

4.4、APPLICATION常量

    //Constant for the action‘s application context.
    public static final String APPLICATION = "com.opensymphony.xwork2.ActionContext.application";

    /**
     * Sets the action‘s application context.
     *
     * @param application the action‘s application context.
     */
    public void setApplication(Map<String, Object> application) {
        put(APPLICATION, application);
    }

    /**
     * Returns a Map of the ServletContext when in a servlet environment or a generic application level Map otherwise.
     *
     * @return a Map of ServletContext or generic application level Map
     */
    public Map<String, Object> getApplication() {
        return (Map<String, Object>) get(APPLICATION);
    }

4.5、SESSION常量

    //Constant for the action‘s session.
    public static final String SESSION = "com.opensymphony.xwork2.ActionContext.session";

    /**
     * Sets a map of action session values.
     *
     * @param session  the session values.
     */
    public void setSession(Map<String, Object> session) {
        put(SESSION, session);
    }

    /**
     * Gets the Map of HttpSession values when in a servlet environment or a generic session map otherwise.
     *
     * @return the Map of HttpSession values when in a servlet environment or a generic session map otherwise.
     */
    public Map<String, Object> getSession() {
        return (Map<String, Object>) get(SESSION);
    }

4.6、PARAMETERS常量

    //Constant for the action‘s parameters.
    public static final String PARAMETERS = "com.opensymphony.xwork2.ActionContext.parameters";

    /**
     * Sets the action parameters.
     *
     * @param parameters the parameters for the current action.
     */
    public void setParameters(Map<String, Object> parameters) {
        put(PARAMETERS, parameters);
    }

    /**
     * Returns a Map of the HttpServletRequest parameters when in a servlet environment or a generic Map of
     * parameters otherwise.
     *
     * @return a Map of HttpServletRequest parameters or a multipart map when in a servlet environment, or a
     *         generic Map of parameters otherwise.
     */
    public Map<String, Object> getParameters() {
        return (Map<String, Object>) get(PARAMETERS);
    }

4.7、LOCALE常量

	//Constant for the action‘s locale.
    public static final String LOCALE = "com.opensymphony.xwork2.ActionContext.locale";

    /**
     * Sets the Locale for the current action.
     *
     * @param locale the Locale for the current action.
     */
    public void setLocale(Locale locale) {
        put(LOCALE, locale);
    }

    /**
     * Gets the Locale of the current action. If no locale was ever specified the platform‘s
     * {@link java.util.Locale#getDefault() default locale} is used.
     *
     * @return the Locale of the current action.
     */
    public Locale getLocale() {
        Locale locale = (Locale) get(LOCALE);

        if (locale == null) {
            locale = Locale.getDefault();
            setLocale(locale);
        }

        return locale;
    }

5、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;

/**
 * The ActionContext is the context in which an {@link Action} is executed. Each context is basically a
 * container of objects an action needs for execution like the session, parameters, locale, etc. <p>
 * <p/>
 * The ActionContext is thread local which means that values stored in the ActionContext are
 * unique per thread. See the {@link 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:
 * <p/>
 * <ul><code>ActionContext context = ActionContext.getContext();</code></ul>
 * <p/>
 * Finally, because of the thread local usage you don‘t need to worry about making your actions thread safe.
 *
 * @author Patrick Lightbody
 * @author Bill Lynch (docs)
 */
public class ActionContext implements Serializable {

    static ThreadLocal<ActionContext> actionContext = new ThreadLocal<ActionContext>();

    /**
     * Constant for the name of the action being executed.
     */
    public static final String ACTION_NAME = "com.opensymphony.xwork2.ActionContext.name";

    /**
     * Constant for the {@link com.opensymphony.xwork2.util.ValueStack OGNL value stack}.
     */
    public static final String VALUE_STACK = ValueStack.VALUE_STACK;

    /**
     * Constant for the action‘s session.
     */
    public static final String SESSION = "com.opensymphony.xwork2.ActionContext.session";

    /**
     * Constant for the action‘s application context.
     */
    public static final String APPLICATION = "com.opensymphony.xwork2.ActionContext.application";

    /**
     * Constant for the action‘s parameters.
     */
    public static final String PARAMETERS = "com.opensymphony.xwork2.ActionContext.parameters";

    /**
     * Constant for the action‘s locale.
     */
    public static final String LOCALE = "com.opensymphony.xwork2.ActionContext.locale";

    /**
     * Constant for the action‘s type converter.
     */
    public static final String TYPE_CONVERTER = "com.opensymphony.xwork2.ActionContext.typeConverter";

    /**
     * Constant for the action‘s {@link com.opensymphony.xwork2.ActionInvocation invocation} context.
     */
    public static final String ACTION_INVOCATION = "com.opensymphony.xwork2.ActionContext.actionInvocation";

    /**
     * Constant for the map of type conversion errors.
     */
    public static final String CONVERSION_ERRORS = "com.opensymphony.xwork2.ActionContext.conversionErrors";

    /**
     * Constant for the container
     */
    public static final String CONTAINER = "com.opensymphony.xwork2.ActionContext.container";
    
    private Map<String, Object> context;

    /**
     * Creates a new ActionContext initialized with another context.
     *
     * @param context a context map.
     */
    public ActionContext(Map<String, Object> context) {
        this.context = context;
    }

    /**
     * Sets the action invocation (the execution state).
     *
     * @param actionInvocation the action execution state.
     */
    public void setActionInvocation(ActionInvocation actionInvocation) {
        put(ACTION_INVOCATION, actionInvocation);
    }

    /**
     * Gets the action invocation (the execution state).
     *
     * @return the action invocation (the execution state).
     */
    public ActionInvocation getActionInvocation() {
        return (ActionInvocation) get(ACTION_INVOCATION);
    }

    /**
     * Sets the action‘s application context.
     *
     * @param application the action‘s application context.
     */
    public void setApplication(Map<String, Object> application) {
        put(APPLICATION, application);
    }

    /**
     * Returns a Map of the ServletContext when in a servlet environment or a generic application level Map otherwise.
     *
     * @return a Map of ServletContext or generic application level Map
     */
    public Map<String, Object> getApplication() {
        return (Map<String, Object>) get(APPLICATION);
    }

    /**
     * Sets the action context for the current thread.
     *
     * @param context the action context.
     */
    public static void setContext(ActionContext context) {
        actionContext.set(context);
    }

    /**
     * Returns the ActionContext specific to the current thread.
     *
     * @return the ActionContext for the current thread, is never <tt>null</tt>.
     */
    public static ActionContext getContext() {
        return actionContext.get();
    }

    /**
     * Sets the action‘s context map.
     *
     * @param contextMap the context map.
     */
    public void setContextMap(Map<String, Object> contextMap) {
        getContext().context = contextMap;
    }

    /**
     * Gets the context map.
     *
     * @return the context map.
     */
    public Map<String, Object> getContextMap() {
        return context;
    }

    /**
     * Sets conversion errors which occurred when executing the action.
     *
     * @param conversionErrors a Map of errors which occurred when executing the action.
     */
    public void setConversionErrors(Map<String, Object> conversionErrors) {
        put(CONVERSION_ERRORS, conversionErrors);
    }

    /**
     * Gets the map of conversion errors which occurred when executing the action.
     *
     * @return the map of conversion errors which occurred when executing the action or an empty map if
     *         there were no errors.
     */
    public Map<String, Object> getConversionErrors() {
        Map<String, Object> errors = (Map) get(CONVERSION_ERRORS);

        if (errors == null) {
            errors = new HashMap<String, Object>();
            setConversionErrors(errors);
        }

        return errors;
    }

    /**
     * Sets the Locale for the current action.
     *
     * @param locale the Locale for the current action.
     */
    public void setLocale(Locale locale) {
        put(LOCALE, locale);
    }

    /**
     * Gets the Locale of the current action. If no locale was ever specified the platform‘s
     * {@link java.util.Locale#getDefault() default locale} is used.
     *
     * @return the Locale of the current action.
     */
    public Locale getLocale() {
        Locale locale = (Locale) get(LOCALE);

        if (locale == null) {
            locale = Locale.getDefault();
            setLocale(locale);
        }

        return locale;
    }

    /**
     * Sets the name of the current Action in the ActionContext.
     *
     * @param name the name of the current action.
     */
    public void setName(String name) {
        put(ACTION_NAME, name);
    }

    /**
     * Gets the name of the current Action.
     *
     * @return the name of the current action.
     */
    public String getName() {
        return (String) get(ACTION_NAME);
    }

    /**
     * Sets the action parameters.
     *
     * @param parameters the parameters for the current action.
     */
    public void setParameters(Map<String, Object> parameters) {
        put(PARAMETERS, parameters);
    }

    /**
     * Returns a Map of the HttpServletRequest parameters when in a servlet environment or a generic Map of
     * parameters otherwise.
     *
     * @return a Map of HttpServletRequest parameters or a multipart map when in a servlet environment, or a
     *         generic Map of parameters otherwise.
     */
    public Map<String, Object> getParameters() {
        return (Map<String, Object>) get(PARAMETERS);
    }

    /**
     * Sets a map of action session values.
     *
     * @param session  the session values.
     */
    public void setSession(Map<String, Object> session) {
        put(SESSION, session);
    }

    /**
     * Gets the Map of HttpSession values when in a servlet environment or a generic session map otherwise.
     *
     * @return the Map of HttpSession values when in a servlet environment or a generic session map otherwise.
     */
    public Map<String, Object> getSession() {
        return (Map<String, Object>) get(SESSION);
    }

    /**
     * Sets the OGNL value stack.
     *
     * @param stack the OGNL value stack.
     */
    public void setValueStack(ValueStack stack) {
        put(VALUE_STACK, stack);
    }

    /**
     * Gets the OGNL value stack.
     *
     * @return the OGNL value stack.
     */
    public ValueStack getValueStack() {
        return (ValueStack) get(VALUE_STACK);
    }
    
    /**
     * Gets the container for this request
     * 
     * @param cont The container
     */
    public void setContainer(Container cont) {
        put(CONTAINER, cont);
    }
    
    /**
     * Sets the container for this request
     * 
     * @return The container
     */
    public Container getContainer() {
        return (Container) get(CONTAINER);
    }
    
    public <T> T getInstance(Class<T> type) {
        Container cont = getContainer();
        if (cont != null) {
            return cont.getInstance(type);
        } else {
            throw new XWorkException("Cannot find an initialized container for this request.");
        }
    }

    /**
     * Returns a value that is stored in the current ActionContext by doing a lookup using the value‘s key.
     *
     * @param key the key used to find the value.
     * @return the value that was found using the key or <tt>null</tt> if the key was not found.
     */
    public Object get(String key) {
        return context.get(key);
    }

    /**
     * Stores a value in the current ActionContext. The value can be looked up using the key.
     *
     * @param key   the key of the value.
     * @param value the value to be stored.
     */
    public void put(String key, Object value) {
        context.put(key, value);
    }
}
时间: 2024-08-04 16:50:03

Struts2系列:(16)ActionContext的相关文章

Struts2中的ActionContext和ServletActionContext的区别和用法

今天学习Struts2的时候遇到"访问和添加属性"的问题,然后就学到了ActionContext和ServletActionContext之间的区别和用法,然后又在网上搜了下别人的文章大致了解了一下,就想着总结一下. 参考文章1:http://www.cnblogs.com/tanglin_boy/archive/2010/01/18/1650871.html 参考文章2:http://blog.csdn.net/woshixuye/article/details/8172777 相信

[Android学习系列16]Android把php输出的json加载到listview

首先写个php脚本输出json,注意,还要输出回车,方便android的bufferreader读取一行 <?php class Book { public $bookid; public $bookname; public $bookinfo; function __construct($id,$name,$info ){ $this->bookid = $id; $this->bookname = $name; $this->bookinfo = $info; } } $boo

struts2.3.16所需的基本的jar包---------SSH升级包不是整体全部都升级的

struts2.3.16所需的基本的jar包 jar包放多了就报Exception什么Unable to load....上网搜了半天也没有能解决的 下面所说的jar包放到WEB-INF/lib以及tomcat/lib中 通过我一个一个添加到tomcat/lib中,直到启动服务器的时候不再报ClassNotFoundException或者ClassDefNotFoundException为止,真TM不容易啊 commons-fileupload-1.3.1.jar commons-io-2.2.

struts2.3.16所需的基本的jar包

jar包放多了就报Exception什么Unable to load....上网搜了半天也没有能解决的 下面所说的jar包放到WEB-INF/lib以及tomcat/lib中 通过我一个一个添加到tomcat/lib中,直到启动服务器的时候不再报ClassNotFoundException或者ClassDefNotFoundException为止,真TM不容易啊 commons-fileupload-1.3.1.jar commons-io-2.2.jar commons-lang3-3.1.j

struts2.3.16中表单重复提交出现空指针异常

异常代码形式: 严重: Exception occurred during processing request: nulljava.lang.NullPointerException    at com.opensymphony.xwork2.util.LocalizedTextUtil.findText(LocalizedTextUtil.java:630)    at com.opensymphony.xwork2.util.LocalizedTextUtil.findText(Local

[转]Struts2.3.16.1+Hibernate4.3.4+Spring4.0.2 框架整合

原文地址:http://blog.csdn.net/ycb1689/article/details/22928519 最新版Struts2+Hibernate+Spring整合 目前为止三大框架最新版本是: struts2.3.16.1 hibernate4.3.4 spring4.0.2 其中struts2和hibernate的下载方式比较简单,但是spring下载有点麻烦,可以直接复制下面链接下载最新版spring http://repo.springsource.org/libs-rele

EMVTag系列16——AC响应数据

在一个联机交易中,要传送到发卡行的专有应用数据. 字段 长度(字节) 赋值 说明 长度 1 07 分散密钥索引 1 00 密文版本号 1 01 根据发卡行密钥版本设置 卡片验证结果(CVR) 4 03 00 bits 8–7: 00 = 第2个GENERATE AC返回AAC 01 = 第2个GENERATE AC返回TC 10 = 不请求第2个GENERATE AC 11 = RFU bits 6–5: 00 = 第1个GENERATE AC返回AAC 01 = 第1个GENERATE AC返

Java 集合系列 16 HashSet

java 集合系列目录: Java 集合系列 01 总体框架 Java 集合系列 02 Collection架构 Java 集合系列 03 ArrayList详细介绍(源码解析)和使用示例 Java 集合系列 04 LinkedList详细介绍(源码解析)和使用示例 Java 集合系列 05 Vector详细介绍(源码解析)和使用示例 Java 集合系列 06 Stack详细介绍(源码解析)和使用示例 Java 集合系列 07 List总结(LinkedList, ArrayList等使用场景和

Skype For Business 2015实战系列16:安装并配置监控存档服务器

Skype For Business 2015实战系列16:安装并配置监控存档服务器 前面的博文中为大家介绍了SFB2015的前段池.OWAS以及持久聊天服务器的部署.今天主要为大家介绍监控存档的部署,通过监控和存档我们可以了解到一段时间内Lync的在线人数以及登陆情况等.闲言少叙,下面我们就看一下如何部署监控存档服务器. 一:安装监控服务器 在前面的部署中其实我已经定义了监控和存档服务器的位置,如下图所示: 接下来我将会在SQL01.SS.LAB上部署监控和存档服务器: 登陆到SQL01,打开

Struts2.3.16.1+Hibernate4.3.4+Spring4.0.2 框架整合

最新版Struts2+Hibernate+Spring整合 目前为止三大框架最新版本是: struts2.3.16.1 hibernate4.3.4 spring4.0.2 其中struts2和hibernate的下载方式比较简单,但是spring下载有点麻烦,可以直接复制下面链接下载最新版spring http://repo.springsource.org/libs-release-local/org/springframework/spring/4.0.2.RELEASE/spring-f