Spring webflow - how to pass the session in evalua

2019-01-28 10:27发布

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条回答
太酷不给撩
2楼-- · 2019-01-28 11:09

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.

查看更多
劳资没心,怎么记你
3楼-- · 2019-01-28 11:12

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();
        ...
    }
}
查看更多
虎瘦雄心在
4楼-- · 2019-01-28 11:12

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

<evaluate expression="externalContext.sessionMap.put('attributeName', flowScope.myObject)"/>
查看更多
狗以群分
5楼-- · 2019-01-28 11:14

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!

查看更多
登录 后发表回答