我在struts2中使用SessionAware,但是当我获取session的值时它返回null。我的 Action 类和方法是-
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;
}
}
最佳答案
有两种方法可以获取操作的 session 映射。
- 实现
SessionAware
。默认情况下, session 映射是在操作调用时填充的。这是一种更好的方式。 - 从操作上下文中获取 session 映射。这样您应该确保请求由 struts2 过滤器处理。
尝试
public String editCustomLocation() throws Exception{
int id=(Integer)sessionMap.get("LocEditId");
//----------some code----------//
retutn SUCCESS;
}
关于java - ActionContext.getContext().getSession() 返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24585795/