转:ServletContext,ActionContext,ServletActionContext
发布日期:2022-03-29 14:04:54 浏览次数:38 分类:博客文章

本文共 7642 字,大约阅读时间需要 25 分钟。

ServletContextServletContext从他的package信息可以看出,它是标准的JavaEE WebApplication类库javax.servlet.ServletContextServletContext提供了标准的Servlet运行环境,其实就是一些servlet和web container进行通信的方法public interface ServletContext { // Returns the URL prefix for the ServletContext. public String getServletContextName(); //Returns the ServletContext for the uri. public ServletContext getContext(String uri);  //Returns the context-path for the web-application. public String getContextPath();  //Returns the real file path for the given uri. public String getRealPath(String uri);  public RequestDispatcher getRequestDispatcher(String uri); public RequestDispatcher getNamedDispatcher(String servletName);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)处理所有的*.dostruts2时,由一个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();          //Sets the action context for the current thread.     public static void setContext(ActionContext context) {         actionContext.set(context);     }     //Returns the ActionContext specific to the current thread.     public static ActionContext getContext() {         return (ActionContext) actionContext.get();     } ///////////////定义放置"名/值对"的Map容器,这是ActionContext的主要功能///////////////        Map
context; // constractor // Creates a new ActionContext initialized with another context. public ActionContext(Map
context) { this.context = context; } public void setContextMap(Map
contextMap) { getContext().context = contextMap; } public Map
getContextMap() { return context; }//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); } //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); }///////////////////将各种功能属性放置入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请求包含的内容//////////////////// //Returns a Map of the HttpServletRequest parameters public Map
getParameters() { return (Map
) get(PARAMETERS); } public void setParameters(Map
parameters) { put(PARAMETERS, parameters); } public void setSession(Map
session) { put(SESSION, session); } public Map
getSession() { return (Map
) get(SESSION); } public void setApplication(Map
application) { put(APPLICATION, application); } public Map
getApplication() { return (Map
) get(APPLICATION); } public void setConversionErrors(Map
conversionErrors) { put(CONVERSION_ERRORS, conversionErrors); } public Map
getConversionErrors() { Map
errors = (Map) get(CONVERSION_ERRORS); if (errors == null) { errors = new HashMap
(); setConversionErrors(errors); } return errors; } //Sets the Locale for the current action. public void setLocale(Locale locale) { put(LOCALE, locale); } public Locale getLocale() { Locale locale = (Locale) get(LOCALE); if (locale == null) { locale = Locale.getDefault(); setLocale(locale); } return locale; } public void setContainer(Container cont) { put(CONTAINER, cont); } public Container getContainer() { return (Container) get(CONTAINER); } public
T getInstance(Class
type) { Container cont = getContainer(); if (cont != null) { return cont.getInstance(type); } else { throw new XWorkException("Cannot find an initialized container for this request."); } } }ServletActionContext 其实是ActionContext的子类,其功能脱胎于ActionContext,对ActionContext的方法做了一定的包装,提供了更简便直观的方法 public class ServletActionContext extends ActionContext implements StrutsStatics {/////////////////Servlet Context 提供了多种操作ActionContext的静态方法,使获得Web对象更方便 //HTTP servlet request public static void setRequest(HttpServletRequest request) { ActionContext.getContext().put(HTTP_REQUEST, request); } public static HttpServletRequest getRequest() { return (HttpServletRequest) ActionContext.getContext().get(HTTP_REQUEST); } //HTTP servlet response public static void setResponse(HttpServletResponse response) { ActionContext.getContext().put(HTTP_RESPONSE, response); } public static HttpServletResponse getResponse() { return (HttpServletResponse) ActionContext.getContext().get(HTTP_RESPONSE); } //servlet context. public static ServletContext getServletContext() { return (ServletContext) ActionContext.getContext().get(SERVLET_CONTEXT); } public static void setServletContext(ServletContext servletContext) { ActionContext.getContext().put(SERVLET_CONTEXT, servletContext); }

转载地址:https://www.cnblogs.com/1-Admin/p/6200910.html 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:Junit Rdeis No tests found matching 单机版安装
下一篇:Could not find a getter for orderItems in class

发表评论

最新留言

做的很好,不错不错
[***.243.131.199]2024年03月19日 01时34分48秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章

python中for可以做变量名吗_Python中使用动态变量名的方法 2019-04-21
mysql 日期转换天数_MySQL 日期操作 增减天数、时间转换、时间戳 2019-04-21
java对象去重复_JAVA中List对象去除重复值的方法 2019-04-21
java bss_[转] .bss段和.data段的区别 2019-04-21
java上传图片损坏_大神求助 上传图片后 图片损坏 2019-04-21
java socket唯一标识符_Java Socket编程之常识网络基础知识 2019-04-21
java给xyz大小排序_java递归实现string xyz排序 2019-04-21
arctime必须要java_Arctime使用教程 Arctime常见问题解答 2019-04-21
mysql pxc mysql5.7_mysql之PXC5.7.18集群系列——1. Percona XtraDB Cluster 搭建 2019-04-21
mysql 自适应字段宽度_box-sizing解决自适应布局容器宽度问题 2019-04-21
java 配置文件配置路径_Java读取配置文件路径设置 2019-04-21
vux 选择器_vue中的scoped分析以及在element-UI和vux中的应用 2019-04-21
java cache 有效期_springboot cache 自定义过期时间及自定义缓存key前缀 2019-04-21
java实验一目的_Java实验报告(实验一) 2019-04-21
java+native+字段_+Java中的native关键字浅析(Java+Native+Interface)++ 2019-04-21
php 内存泄露检测工具,php - 诊断内存泄漏 - 允许#bytes的内存大小耗尽 2019-04-21
Java 去除空格获取文件路径 2019-04-21
python 批量修改文件名称去除文件名中空格 2019-04-21
python 将文件名写入 txt文件 2019-04-21
python 3 读取文件txt 打印print 2019-04-21