2012-01-18 70 views
3

我正在使用spring webflow,但我需要訪問我的HttpSession中的一個方法,該方法使用transition ==> evaluate expression來訪問。 (所以在包含我的流程的xml文件中) 到目前爲止,我還沒有找到一種方法將它傳遞給我的方法。我看了一下flowrequestcontext,但到目前爲止我還沒有找到方法。Spring webflow - 如何在評估表達式中傳遞會話?

回答

7

我想你不需要通過它,只要你通過RequestContext。你可以試試這個:

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

我有一個非常類似的需要訪問HttpSession在流動。以下是我做的:

首先來看看externalContext特殊EL變量:

externalContext

它給你的其中之一:

org.springframework.webflow.context.ExternalContext

ExternalContext接口提供了一種稱爲getNativeRequest()的方法,該方法應返回HttpRequest對象。 (在weblflow 2.0.x版本至少)

這裏的Javadoc: http://static.springsource.org/spring-webflow/docs/2.0.x/javadoc-api/org/springframework/webflow/context/ExternalContext.html#getNativeRequest()

所以,這意味着你應該能夠使用類似這門手藝的表達式:

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

一個簡單的測試,你可以使用這樣的表達式:

expression="externalContext.nativeRequest.session.id"

將您的會話ID傳遞給方法。

當然你也可以使用類似的EL通過列席會議,方法等

4

插入的對象(例如,從flowScope)到會話這個工作對我來說:

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

這爲我工作:

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

在客戶端:

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

希望它有幫助!