ActionContext.getContext().getSession()返回null [英] ActionContext.getContext().getSession() returns null

查看:86
本文介绍了ActionContext.getContext().getSession()返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在struts2中使用SessionAware,但是当我获取session的值时,它返回null.我的动作类和方法是-

I am using SessionAware in struts2 but when I am getting the value of session it returns null. my action class and methods are-

public class CustomLocation extends ActionSupport implements SessionAware{
private Map<String, Object> sessionMap;

public String prepareEditCustomLocationForm()throws Exception{
    //----some code here------//
    sessionMap.put("LocEditId", id);
    return SUCCESS;
}


public String editCustomLocation() throws Exception{
Map session = (Map) ActionContext.getContext().getSession(); // it returns null
int id=(Integer)session.get("LocEditId");
//----------some code----------//
retutn SUCCESS;
}


@Override
public void setSession(Map<String, Object> map) {
this.sessionMap = map;    
}

}

推荐答案

有两种方法可以获取操作的会话映射.

There are two methods to obtain a session map to the action.

  1. 实施SessionAware.默认情况下,在操作调用中填充会话映射.这是一种更好的方法.
  2. 从操作上下文获取会话映射.这样,您应该确保struts2过滤器可以处理请求.
  1. Implement SessionAware. By default the session map is populated on action call. This is a preferable way.
  2. Get a session map from the action context. This way you should make sure the request is handled by the struts2 filter.

尝试

public String editCustomLocation() throws Exception{
  int id=(Integer)sessionMap.get("LocEditId");
  //----------some code----------//
  retutn SUCCESS;
}

这篇关于ActionContext.getContext().getSession()返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆