ServletContext
- ServletContext从他的package信息可以看出,它是标准的JavaEE WebApplication类库
javax.servlet.ServletContext |
- ServletContext提供了标准的Servlet运行环境,其实就是一些servlet和web container进行通信的方法
public interface ServletContext {
// Returns the URL prefix for the ServletContext. //Returns the ServletContext for the uri. |
public Object getAttribute(String name); public Enumeration getAttributeNames(); public void setAttribute(String name, Object value); public void removeAttribute(String name); |
- 注意:一个ServletContext对应一个命名空间的servlet( 比如/struts下的所有servlet),是被所有servlet共享的.
There is one context per "web application" per Java Virtual Machine.
(A "web application" is a collection of servlets and content installed under a specific subset of the server's URL namespace such as /catalog and possibly installed via a .war file.)
- ServletContext被包含在ServletConfig对象中,ServletConfig对象通常被servlet或filter的init()方法读取
ServletConfig.getServletContext()
filterconfig.getServletContext()
ActionContext来源于Struts2 与 Struts1的本质不同.
struts1时,由一个servlet (servlet org.apache.struts.action.ActionServlet)处理所有的*.do
struts2时,由一个filter(org.apache.struts2.dispatcher.FilterDispatcher)处理所有的请求
struts1 仍旧属于servlet范畴,struts1 action 其本质仍是servlet.
struts2 action 已经是普通的java bean了,已经跳出了servlet 框架
ActionContext就是为了弥补strtus2 action跳出标准servlet框架而造成的和WEB环境失去联系的缺陷
ActionContext的主要作用:
- 提供Web环境Context
- 解决线程安全问题
- 解决一些和其他框架或容器(siteMesh,webLogic)的兼容问题
分析ActionContext源码
public class ActionContext implements Serializable { |
//////////ThreadLocal模式下的ActionContext实例,实现多线程下的线程安全///////////////
static ThreadLocal actionContext = new ThreadLocal(); |
///////////////定义放置"名/值对"的Map容器,这是ActionContext的主要功能/////////////// Map<String, Object> context; // constractor // Creates a new ActionContext initialized with another context. public ActionContext(Map<String, Object> context) { this.context = context; } public void setContextMap(Map<String, Object> contextMap) { getContext().context = contextMap; } public Map<String, Object> getContextMap() { return context; } //Returns a value that is stored in the current ActionContext by doing a lookup using the value's key. |
///////////////////将各种功能属性放置入Map容器中///////////////////// //action name, Constant for the name of the action being executed. public static final String ACTION_NAME = "com.opensymphony.xwork2.ActionContext.name"; // ognl value stack 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主属性:ActionName, ActionInvocation(调用action的相关信息), ognl value stack/// //Gets the name of the current Action. public String getName() { return (String) get(ACTION_NAME); } //Sets the name of the current Action in the ActionContext. public void setName(String name) { put(ACTION_NAME, name); } //Sets the action invocation (the execution state). public void setActionInvocation(ActionInvocation actionInvocation) { put(ACTION_INVOCATION, actionInvocation); } public ActionInvocation getActionInvocation() { return (ActionInvocation) get(ACTION_INVOCATION); } // Sets the OGNL value stack. public void setValueStack(ValueStack stack) { put(VALUE_STACK, stack); } //Gets the OGNL value stack. public ValueStack getValueStack() { return (ValueStack) get(VALUE_STACK); } ////////////////各种 request请求包含的内容//////////////////// |
ServletActionContext 其实是ActionContext的子类,其功能脱胎于ActionContext,对ActionContext的方法做了一定的包装,提供了更简便直观的方法
public class ServletActionContext extends ActionContext implements StrutsStatics { |
/////////////////Servlet Context 提供了多种操作ActionContext的静态方法,使获得Web对象更方便
//HTTP servlet request |