有没有办法在自定义ActionMapper中获取会话映射?

时间:2010-08-04 15:32:09

标签: session struts2

我想在Struts2中的自定义ActionMapper中获取/设置会话参数。当我打电话

Map<String, Object> session = ActionContext.getContext().getSession();

在我的自定义MyActionMapper课程中,sessionnull

我做错了什么?我是否必须配置一些实际的会话?

谢谢,Gregor

2 个答案:

答案 0 :(得分:1)

当您使用ognl #session.USER_KEY时,如果您使用getSessionsetSession,则无法找到。

最好从语法

下面获取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;
}