【问题标题】:ActionContext.getContext() returns null using Session in Struts 2ActionContext.getContext() 在 Struts 2 中使用 Session 返回 null
【发布时间】:2022-01-09 01:53:25
【问题描述】:

我正在尝试在 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 的一些解决方案,但仍然没有解决这个问题。请为此提供解决方案。

【问题讨论】:

    标签: struts2 httpsession actioncontext


    【解决方案1】:

    ActionContext是action调用的容器占位符,更详细的解释是here

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-10
      • 2011-10-25
      • 1970-01-01
      • 2014-03-05
      • 2010-09-07
      相关资源
      最近更新 更多