我正在尝试在Struts2:中使用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
库,也尝试过谷歌的一些解决方案,但仍然没有解决这个问题。
ActionContext
是操作调用的容器占位符,这里有更详细的解释。
操作上下文可能是null
有很多原因。您正在启动另一个线程,或者修改现有线程,或者它还没有创建。
要获得SessionMap
,还有另一种方法。让您的操作类实现SessionAware
,并确保为该操作配置了默认的拦截器堆栈。之后,会话映射应该被注入到操作实例中。点击此处了解更多信息。