ActionContext 一 | 学习笔记

简介: 快速学习 ActionContext 一 ,介绍了 ActionContext 一 系统机制, 以及在实际应用过程中如何使用。

开发者学堂课程【Java Web开发系列课程 - Struts2框架入门ActionContext 一】学习笔记,与课程紧密联系,让用户快速学习知识。

课程地址:https://developer.aliyun.com/learning/course/537/detail/7305


ActionContext 一

 

内容介绍:

一、ActionContext 对象笔记

二、ActionContext 理解


一、ActionContext 对象笔记

Struts1 的 Action 必须依赖于 web 容器,他的 execute 方法会自动获得HttpServletRequestHttpServletResponse 对象,从而可以跟 web 容器进行交互。

Struts2 的 Acticn 不用依赖于 web 容器,本身只是一个普通的 java 类而已。但是,在 web 开发中我们往往需要获得 request、session、applicatipr 等对象。这时,可以通过 Actincontext 米处理。      

ActianContext 正如其名,是 Actipn 执行的=下文。他内部有个 map 属性,它存放了 Action 执行时需要用到的对家。      

在每次执行 Action 之前者会剑建新的 Actioncontext 对象,所以 Actincontext 是线程安全的。新 new 的 ActionContext 是保存在一个 Threadloal 变量中,即买用Threadloca 模式 Threadlocal 变量为每个线程提供独立的变量值的副本,使每个线程都可以独立的使用自己的副本,而不会知其他线程发生冲突。      

通过 ActionContext 获取的 session,requestapplication 产不是直正的

HttpServletRequestHttpServletResponseServletContext 对象,而是将这三个对象里面的值重新包装成了 map 对家。

这样的封装,我们及获取了我们需要的值,司时避免了跟web容器直接打交道,实现了完全的解偶。

测试代码:

public class TestActionContextAction extends ActionSupport      

privates String uname ;      

public String execute( ) throws Exception {

ActionContext ac = ActionContext.getContext ( );          

System.cut.println(ac) ;      

//在此处定义断点

return this.SUCCESS;

}

 

二、ActionContext 理解

1、什么是 ActionContext?

ActionContext 是 map 结构的容器,ActionContext 是 Action 的上下文,存放Action 执行过程中的数据信息。

ActionContext 存放 Action 的数据、ActionInvocation request 的数据、session 的数据、application 的数据、locale 的数据、conversion errors 等。

每次请求时会为当前线程创建一个新的 ActionContext。而 ActionContext 采用了ThreadLocal 的方式来存放 ActionContext,所以 ActionContext 是线程安全。

protected void postInit( Dispatcher dispatcher,

FilterConfigfilterConfig)  {

} 

public void doFilter(Servlet Request req, ServletResponse res, FilterChain chain) throws IOEx

HttpServletRequest request = (HttpServlet Request)  req;

HttpServletResponse response = (HttpServlet Response) res;

try {

prepare. setEncodingAndlocale(request, response)

prepare. createActionContext (request, response);

prepare. assignDispatcherToThread (

if excludedPatterns ! = null && prepare.  isUrlExcluded 

(request, excludedPatterns) ) {

chain. doFilter(request, response);

}  else {

request = prepare. wrapRequest(request);

ActionMapping mapping prepare. findActionMapping

(request, response, true);

if (mapping == null) {

boolean handled execute. executeStaticResource

Request( request, response);

if (!handled)  {

chain.doFilter(request, response);

} else {

* Creates the action context and initializes the thread local

*/

publicActionContextcreateActionContext  (HttpServletRequest request, HttpServlet Response res

Actioncontext ctx; 

Integer counter = 1;

IntegeroldCounter= (Integer) request. getAttribute(CLEANUP      

RECURSION_ COUNTER)

if (oldcounter != null) {

counter = oldCounter + 1 

}

ActionContext oldContext = ActionContext. getContext( );

if (oldcontext ! = null) {

// detected existing context, so we are probably In a forward

ctx new ActionContext(new HashMap< string, Object> (oldContext. getContextMapo));

} else { 

Valuestack stack = dispatcher. getContainer( ) . get Instance  (ValuestackFactory. class) . cr

stack. getContext( ). putall(dispatcher. createContextMap

(request, response, null, servI

ctx = new ActionContext(stack. getContext());

}

request.setAttribute(CLEANUP RECURSION COUNTER,  counter)

ActionContext.setContext(ctx);

return ctx;

2、获取 ActionContext

ActionContext getContext()获取。由于ActionContext是线程安全的,并且是通过静态方法获取的,所以在本线程中的非Action类中也可以访问。(有些公共数据处理完可以直接放置)

*注意点:ActionContext 是基于请求创建的,所以在非请求的线程中是不能使用ActionContext 对象的。如:filter 的 init()方法。

* 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) actionContext. get();

//Don't do lazy context creation, as it requires container; the   creation of which may

//precede the context creation

//if (context == null) {

// Valuestack vs = ValuestackFactory. get Factory( ) . createvaluestack( );

// context new Actioncontext(vs, getContext())

// setContext(context);

//}

}

3、ActionContext 的简图

image.png

image.png

//Cleans up a request of thread locals

public void cleanupRequest(Httpservlet Request request) {

Integer counterval = (Integer) request.getAttribute(CLEANUP _RECURSION_COUNTER)

if  (counterval != null) {

counterval !=1;

request.setAttribute(CLEANUP_ RECURSION_  COUNTER, counterval);

if(counterval > e) {

if (log.isDebugEnabled( ) ) {

log.debug(skipping cleanup counter ="+counterval);

}

return; 

}

}

// always clean up the thread request, even if an action hasn't been

try {

dispatcher.cleanUpRequest (request);

}  catch (IOException e) {

if (LOG.isWarnEnabled( )) { 

LOG. warn( " Cannot clean up the request, some files can still

StrutsConstants. STRUTS_ MULTIPART SAVEDIR);

}

} finally { 

Actioncontext.setContext(null)

Dispatcher. setInstance(null);

}

相关文章
|
缓存 算法 NoSQL
【分布式详解】一致性算法、全局唯一ID、分布式锁、分布式事务、 分布式缓存、分布式任务、分布式会话
分布式系统通过副本控制协议,使得从系统外部读取系统内部各个副本的数据在一定的约束条件下相同,称之为副本一致性(consistency)。副本一致性是针对分布式系统而言的,不是针对某一个副本而言。强一致性(strong consistency):任何时刻任何用户或节点都可以读到最近一次成功更新的副本数据。强一致性是程度最高的一致性要求,也是实践中最难以实现的一致性。单调一致性(monotonic consistency):任何时刻,任何用户一旦读到某个数据在某次更新后的值,这个用户不会再读到比这个值更旧的值。
1438 0
|
存储 Kubernetes 安全
|
11月前
|
存储 缓存 NoSQL
缓存加速新玩法,让你的应用飞起来
本文主要叙述如何运用云数据库 Tair 构建缓存,助力应用提速、优化性能。
|
9月前
|
机器学习/深度学习 人工智能 量子技术
量子计算的商业化前景:未来科技的颠覆性力量
量子计算的商业化前景:未来科技的颠覆性力量
522 17
|
机器学习/深度学习 数据可视化 vr&ar
python根据历史数据预测
7月更文挑战第16天
|
数据采集 传感器 监控
如何在LabVIEW中使用FPGA模块
如何在LabVIEW中使用FPGA模块
672 1
|
10月前
|
存储 编解码 大数据
阿里云服务器实例选择参考:根据业务场景选择云服务器实例规格
对于初次接触阿里云服务器的用户来说,面对众多实例规格往往不知道如何选择,因为云服务器实例规格不同,价格也不一样,往往会感到无从下手。本文旨在通过详细解析阿里云服务器的不同实例规格及其适用场景,为用户提供一份实用的选型指南,以供参考。
|
JavaScript
Vue2.0、Vue3.0分别使用v-model封装组件[Vue必会]
本文介绍了在Vue 2和Vue 3中如何使用`v-model`来实现组件间的双向数据绑定,包括在Vue 2中使用`value`和`input`事件,以及在Vue 3中使用`modelValue`和`update:modelValue`事件的方法。
991 22
|
JavaScript 前端开发 UED
Vue 组件设计:构建生动多彩的树形结构组件
本文介绍了如何使用 Vue 构建一个功能强大的树形结构组件。该组件支持递归渲染节点及其子节点,提供了自定义节点颜色、文本和布局的功能。通过独特的样式处理不同层级的节点,展示出丰富的视觉效果。组件还支持动态布局和缩放,确保灵活的界面展示和用户体验。文章提供了详细的代码实现,包括 HTML、JavaScript 和 SCSS,帮助开发者快速集成和定制自己的树形结构组件。
1083 0
Vue 组件设计:构建生动多彩的树形结构组件
|
安全 网络协议 关系型数据库