ActionContext.getContext() 在 Struts 2 中使用 Session 返回 null

问题描述 投票:0回答:1

我正在尝试在 Struts 2 中使用

Session

private Map<String, Object> session = ActionContext.getContext().getSession();

但我收到此错误:

Exception in thread "main" java.lang.NullPointerException: Cannot invoke 
"com.opensymphony.xwork2.ActionContext.getSession()" because the return value of 
"com.opensymphony.xwork2.ActionContext.getContext()" is null

我尝试使用 Struts 的

SessionMap
但仍然收到相同的错误消息。我尝试过更改
struts-core
库,也尝试了 Google 的一些解决方案,但仍然没有解决这个问题。

session struts2 struts httpsession actioncontext
1个回答
0
投票

ActionContext
是动作调用的容器占位符,更详细的解释是这里

动作上下文可能是

null
的原因有很多。您正在启动另一个线程,或者修改现有线程,或者它只是尚未创建。

要获得

SessionMap
,还有另一种方法。让您的操作类实现
SessionAware
并确保您已为该操作配置了默认拦截器堆栈。之后,会话映射应该被注入到操作实例中。更多相关信息这里

© www.soinside.com 2019 - 2024. All rights reserved.