我想在Struts2中的自定义ActionMapper中获取/设置会话参数。当我打电话
Map<String, Object> session = ActionContext.getContext().getSession();
在我的自定义MyActionMapper
课程中,session
为null
。
我做错了什么?我是否必须配置一些实际的会话?
谢谢,Gregor
答案 0 :(得分:1)
当您使用ognl #session.USER_KEY
时,如果您使用getSession
或setSession
,则无法找到。
最好从语法
下面获取SessionMap
SessionMap sessionmap = ActionContext.getContext().get("session") ;
然后它会正常工作。
答案 1 :(得分:0)
这实际上可以作为我的自定义ActionMapper
中的方法正常工作:
private SessionMap<String, Object> getSession(HttpServletRequest request) {
SessionMap<String, Object> session =
(SessionMap<String, Object>) ActionContext.getContext().getSession();
if ( session == null ) {
/* create a sessionMap if we don't already have one. */
session = new SessionMap<String, Object>(request);
}
return session;
}