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

13.1k Views Asked by At

I'm using spring webflow, but I need to access my HttpSession in a method that's accessed using a transition ==> evaluate expression. (so in the xml file containing my flow) So far I've yet to find a way to actually pass it to my method. I've taken a look at the flowrequestcontext but so far I haven't found a way yet.

4

There are 4 best solutions below

1
On BEST ANSWER

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();
        ...
    }
}
0
On

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

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

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!

0
On

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.