Spring webflow - how to pass the session in evaluate expression?

别等时光非礼了梦想. 提交于 2019-11-29 15:29:35

I think you don't need to pass it as soon as you pass the RequestContext. You can try this:

public class MyAction extends MultiAction{      
    public Event myMethod(RequestContext context){
        HttpSession session = ((HttpServletRequest)context.getExternalContext().getNativeRequest()).getSession();
        ...
    }
}

to insert object (e.g. from flowScope) into session this worked for me:

<evaluate expression="externalContext.sessionMap.put('attributeName', flowScope.myObject)"/>

I had a very similar need to access the HttpSession in a flow. Here's how I did it:

First, take a look at the externalContext special EL variable:

externalContext

It gives you one of these:

org.springframework.webflow.context.ExternalContext

The ExternalContext interface provides a method called getNativeRequest(), which should return to you an HttpRequest object. (in weblflow 2.0.x at least)

Here is the javadoc: http://static.springsource.org/spring-webflow/docs/2.0.x/javadoc-api/org/springframework/webflow/context/ExternalContext.html#getNativeRequest()

So, that means you should able to craft an expression using something like this:

<evaluate expression="externalContext.nativeRequest.session" result="flowScope.information"/>

As a simple test, you can use an expression like this:

expression="externalContext.nativeRequest.session.id"

to pass your session id to a method.

Of course you can use similar EL to pass the session to methods etc.

This worked for me:

<set name="flowRequestContext.externalContext.sessionMap.myId" value="myObject.getId()" />

On the client:

Long id = (Long) request.getSession().getAttribute("myId");

Hope it helps!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!