ActionContext.getContext()。getSession()返回null

时间:2014-07-05 11:12:24

标签: java session java-ee struts2

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

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;    
}

}

1 个答案:

答案 0 :(得分:1)

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

  1. 实施SessionAware。默认情况下,会话映射在操作调用时填充。这是一种更好的方式。
  2. 从动作上下文中获取会话映射。这样你就应该确保struts2过滤器处理请求。
  3. 尝试

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