本文介绍了为什么使用 ServletRequestAware 而不是 ServletActionContext?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要在 Struts 2 中获取 servlet 请求,我们可以使用 ServletRequestAwareServletActionContext.但是,在特定的 Internet 资源,它被告知应该使用ServletRequestAware 而不是ServletActionContext.

To get a servlet request in Struts 2 we can use either ServletRequestAware or ServletActionContext. However, in a particular internet resource, it is told that ServletRequestAware should be used instead of ServletActionContext.

这是否与 ServletActionContext 将成为多线程环境中的共享资源有关,或者这背后是否有任何其他原因?

Does that have something to do with the fact that ServletActionContext will be a shared resource in a multi-threaded environment or is there any other reason behind this?

推荐答案

ServletActionContext 是一个仅包含静态方法的辅助类,其中一个用于从动作上下文中检索 servlet 请求.但是操作上下文是 ThreadLocal,所以它不能在多线程环境中共享.

The ServletActionContext is a helper class that contains only static methods, one of them used to retrieve servlet request from action context.But action context is ThreadLocal, so it can't be shared in multi-threaded environment.

Struts2 中也没有每个请求的多线程环境,除了 executeAndWait 拦截器使用的后台线程.

There's also no multi-threded environment per request in Struts2, except a background thread used by the executeAndWait interceptor.

使用ServletRequestAware 的原因是因为如果servletConfig 拦截器包含在堆栈中,它是获得servlet 请求对象的保证方法.

The reason to use ServletRequestAware is because it's guaranteed method to obtain a servlet request object if servletConfig interceptor is included in the stack.

您可以在任何地方使用 ServletActionContext,但它不保证将返回请求对象而不是 null.

You can use ServletActionContext from anywhere, but it doesn't guarantee that a request object instead of null will be returned.

这篇关于为什么使用 ServletRequestAware 而不是 ServletActionContext?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-13 23:57