问题描述
我正在处理一种混合情况,我使用 Servlets &Struts2.
I am working on a mixed situation where i am using Servlets & Struts2.
我正在调用 HTTPServlet 并在其中处理数据.
I am calling a HTTPServlet and where i am processing on data.
然后我需要调用 struts API 来在 DB 中插入数据.
Then I need to make a call to struts API to insert data in DB.
因为我也在使用 HTTPSession.
In that i am using HTTPSession also.
所以我打电话
ServletActionContext.getRequest()
获取 HttpRequest 然后在那个 struts 操作类中会话.
To get HttpRequest and then session in that struts action class.
Struts Action 类正在被 Servlet 调用.
Struts Action class is getting called by Servlet.
但是
ServletActionContext.getRequest()
总是返回NULL.
是不是因为它没有被网络调用.
Is it because that it is not getting called by web.
推荐答案
首先,ServletActionContext
和 ActionContext
都使用一个 ThreadLocal
来存储每个-request 对象(例如请求和响应).这是由 Struts2 处理的.如果您尝试从 Servlet 调用它们,它们将返回 null
,因为请求被路由到您的 servlet,而不是 Struts2(因此 Struts2 没有为请求创建操作上下文).
First, ServletActionContext
and ActionContext
both use a ThreadLocal
to store per-request objects (such as the request and response). This is handled by Struts2. If you attempt to call those from a Servlet, they'll return null
, since the request was routed to your servlet, not to Struts2 (and thus Struts2 did not create an action context for the request).
话虽如此,Struts2 是对 Servlet API 的更高级别的抽象.您从 servlet 中调用 Struts2 操作这一事实听起来真的错误.
With that said, Struts2 is a higher level abstraction over the Servlet API. The fact that you are invoking a Struts2 action from within a servlet sounds really wrong.
如果您需要在 servlet 和操作中执行一些通用过程,则创建一个单独的类来处理该过程(即,在数据库中插入数据),然后从您的 servlet 和操作中调用该类.
If you need to perform some common process in both a servlet and an action, then create a separate class to handle the process (i.e., inserting data in a database) and then call that class from both your servlet and your action.
如果这不是您想要做的,那么请提供更多详细信息,以及您的 servlet 和操作代码的示例.
If that isn't what you're trying to do, then please provide more details, along with an example of your servlet and action code.
这篇关于ServletActionContext.getRequest() 返回 NULL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!