struts2的ActionContext类分析(action执行时所需对象的环境)

        ActionContext是Action执行的一个环境。每一个ActionContext对象基本上是一个action执行所需

对象的容器,如session、parameters、locale等。(来自ActionContext API)

        ActionContext是thread local的,这意味着保存在ActionContext中的值在每个线程中都是独有的。

这样就不用担心用户具体的action context,而且自己编写的actions也是线程安全的。

public class ActionContext implements Serializable {
    static ThreadLocal actionContext = new ThreadLocal();
    /**
     * Constant for the name of the action being executed.
     */
    public static final String ACTION_NAME = "com.opensymphony.xwork2.ActionContext.name";
    public static final String VALUE_STACK = ValueStack.VALUE_STACK;
    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";
    
	//用于存放对应Action执行时需要的对象,可以看出使用了Map来存放,上面的常量都是Map中key
    Map<String, Object> context; 

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

	 /**
     * Stores a value in the current ActionContext. The value can be looked up using the key.
     */
    public void put(String key, Object value) {
        context.put(key, value);
    }

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

     /**
     * Returns a value that is stored in the current ActionContext by doing a lookup using the value's key.
     */
    public Object get(String key) {
        return context.get(key);
    }

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

        上面的代码不是完整的,仅仅保留了ActionContext类中的成员变量定义和几个方法。成员

变量中最重要的是Map<String, Object>类型 context。从下面的get方法可以看出,context成员

变量以键值对的方式存放Action执行时所需要的对象。其他的常量成员变量表示的是Map对象

context中的键值。像action执行所需的session,application,locale,valueStack,converter等

都可以从这个类中找到。

        对应的ActionContext对象放置了一个ThreadLocal对象中,保证action执行环境是thread local

的,是线程安全的。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值