ServletContext,ActionContext,ServletActionContext 梦里梦外; 2022-05-12 01:20 143阅读 0赞 **ServletContext** * ServletContext从他的package信息可以看出,它是标准的JavaEE WebApplication类库 <table> <tbody> <tr> <td>javax.servlet.ServletContext<br></td> </tr> </tbody> </table> * ServletContext提供了标准的Servlet运行环境,其实就是一些servlet和web container进行通信的方法 <table> <tbody> <tr> <td>public interface ServletContext { <br><br> // Returns the URL prefix for the ServletContext.<br> public String getServletContextName();<br><br> //Returns the ServletContext for the uri.<br> public ServletContext getContext(String uri);<br> <br> //Returns the context-path for the web-application.<br> public String getContextPath();<br> <br> //Returns the real file path for the given uri.<br> public String getRealPath(String uri);<br> <br> public RequestDispatcher getRequestDispatcher(String uri);<br> public RequestDispatcher getNamedDispatcher(String servletName);<br></td> </tr> <tr> <td>public Object getAttribute(String name);<br>public Enumeration getAttributeNames();<br>public void setAttribute(String name, Object value);<br>public void removeAttribute(String name);</td> </tr> </tbody> </table> * 注意:一个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源码 <table> <tbody> <tr> <td> public class ActionContext implements Serializable { <br></td> </tr> <tr> <td> //ThreadLocal模式下的ActionContext实例,实现多线程下的线程安全///<br><br> static ThreadLocal actionContext = new ThreadLocal();<br> <br> //Sets the action context for the current thread.<br> public static void setContext(ActionContext context) { <br> actionContext.set(context);<br> }<br> //Returns the ActionContext specific to the current thread.<br> public static ActionContext getContext() { <br> return (ActionContext) actionContext.get();<br> }</td> </tr> <tr> <td> ///定义放置”名/值对”的Map容器,这是ActionContext的主要功能/// <br> <br> Map<String, Object> context;<br> <br> // constractor <br> // Creates a new ActionContext initialized with another context.<br> public ActionContext(Map<String, Object> context) { <br> this.context = context;<br> }<br> <br> public void setContextMap(Map<String, Object> contextMap) { <br> getContext().context = contextMap;<br> }<br> public Map<String, Object> getContextMap() { <br> return context;<br> }<br><br>//Returns a value that is stored in the current ActionContext by doing a lookup using the value’s key.<br> public Object get(String key) { <br> return context.get(key);<br> }<br> //Stores a value in the current ActionContext. The value can be looked up using the key.<br> public void put(String key, Object value) { <br> context.put(key, value);<br> }</td> </tr> <tr> <td>///将各种功能属性放置入Map容器中/<br> <br> //action name, Constant for the name of the action being executed.<br> public static final String ACTION_NAME = “com.opensymphony.xwork2.ActionContext.name”;<br> <br> // ognl value stack<br> public static final String VALUE_STACK = ValueStack.VALUE_STACK;<br> <br> public static final String SESSION = “com.opensymphony.xwork2.ActionContext.session”;<br> public static final String APPLICATION = “com.opensymphony.xwork2.ActionContext.application”;<br> public static final String PARAMETERS = “com.opensymphony.xwork2.ActionContext.parameters”;<br> public static final String LOCALE = “com.opensymphony.xwork2.ActionContext.locale”;<br> public static final String TYPE_CONVERTER = “com.opensymphony.xwork2.ActionContext.typeConverter”;<br> public static final String ACTION_INVOCATION = “com.opensymphony.xwork2.ActionContext.actionInvocation”;<br> public static final String CONVERSION_ERRORS = “com.opensymphony.xwork2.ActionContext.conversionErrors”;<br> public static final String CONTAINER = “com.opensymphony.xwork2.ActionContext.container”;<br> <br> // 各种Action主属性:ActionName, ActionInvocation(调用action的相关信息), ognl value stack///<br> <br> //Gets the name of the current Action.<br> public String getName() { <br> return (String) get(ACTION_NAME);<br> }<br> //Sets the name of the current Action in the ActionContext.<br> public void setName(String name) { <br> put(ACTION_NAME, name);<br> }<br> <br> //Sets the action invocation (the execution state).<br> public void setActionInvocation(ActionInvocation actionInvocation) { <br> put(ACTION_INVOCATION, actionInvocation);<br> }<br> public ActionInvocation getActionInvocation() { <br> return (ActionInvocation) get(ACTION_INVOCATION);<br> }<br> <br> // Sets the OGNL value stack.<br> public void setValueStack(ValueStack stack) { <br> put(VALUE_STACK, stack);<br> }<br> //Gets the OGNL value stack.<br> public ValueStack getValueStack() { <br> return (ValueStack) get(VALUE_STACK);<br> }<br><br>各种 request请求包含的内容<br> //Returns a Map of the HttpServletRequest parameters<br> public Map<String, Object> getParameters() { <br> return (Map<String, Object>) get(PARAMETERS);<br> }<br> public void setParameters(Map<String, Object> parameters) { <br> put(PARAMETERS, parameters);<br> } <br> <br> public void setSession(Map<String, Object> session) { <br> put(SESSION, session);<br> }<br> public Map<String, Object> getSession() { <br> return (Map<String, Object>) get(SESSION);<br> } <br> public void setApplication(Map<String, Object> application) { <br> put(APPLICATION, application);<br> }<br> public Map<String, Object> getApplication() { <br> return (Map<String, Object>) get(APPLICATION);<br> }<br> <br> public void setConversionErrors(Map<String, Object> conversionErrors) { <br> put(CONVERSION_ERRORS, conversionErrors);<br> }<br> public Map<String, Object> getConversionErrors() { <br> Map<String, Object> errors = (Map) get(CONVERSION_ERRORS);<br> <br> if (errors == null) { <br> errors = new HashMap<String, Object>();<br> setConversionErrors(errors);<br> }<br> return errors;<br> }<br> <br> //Sets the Locale for the current action.<br> public void setLocale(Locale locale) { <br> put(LOCALE, locale);<br> }<br> public Locale getLocale() { <br> Locale locale = (Locale) get(LOCALE);<br> <br> if (locale == null) { <br> locale = Locale.getDefault();<br> setLocale(locale);<br> }<br> <br> return locale;<br> }<br> public void setContainer(Container cont) { <br> put(CONTAINER, cont);<br> }<br> public Container getContainer() { <br> return (Container) get(CONTAINER);<br> }<br> <br> public <T> T getInstance(Class<T> type) { <br> Container cont = getContainer();<br> if (cont != null) { <br> return cont.getInstance(type);<br> } else { <br> throw new XWorkException(“Cannot find an initialized container for this request.”);<br> }<br> }<br> }<br></td> </tr> </tbody> </table> ServletActionContext 其实是ActionContext的子类,其功能脱胎于ActionContext,对ActionContext的方法做了一定的包装,提供了更简便直观的方法 <table> <tbody> <tr> <td> public class ServletActionContext extends ActionContext implements StrutsStatics { <br></td> </tr> <tr> <td>/Servlet Context 提供了多种操作ActionContext的静态方法,使获得Web对象更方便<br><br> //HTTP servlet request<br> public static void setRequest(HttpServletRequest request) { <br> ActionContext.getContext().put(HTTP_REQUEST, request);<br> } <br> public static HttpServletRequest getRequest() { <br> return (HttpServletRequest) ActionContext.getContext().get(HTTP_REQUEST);<br> }<br> <br> //HTTP servlet response<br> public static void setResponse(HttpServletResponse response) { <br> ActionContext.getContext().put(HTTP_RESPONSE, response);<br> } <br> public static HttpServletResponse getResponse() { <br> return (HttpServletResponse) ActionContext.getContext().get(HTTP_RESPONSE);<br> }<br> <br> //servlet context.<br> public static ServletContext getServletContext() { <br> return (ServletContext) ActionContext.getContext().get(SERVLET_CONTEXT);<br> } <br> public static void setServletContext(ServletContext servletContext) { <br> ActionContext.getContext().put(SERVLET_CONTEXT, servletContext);<br> }<br></td> </tr> </tbody> </table>
还没有评论,来说两句吧...