spring webflow requestScope变量在视图中不可见

时间:2014-12-18 01:38:13

标签: spring-webflow-2

给定的

<view-state id="bstate" model="foo">
<on-entry>
    <evaluate expression="service.createPerson(22,'Adam', 'Hayek')"
            result="viewScope.person"></evaluate>
</on-entry>
...
</view-state>

在jsp视图中我可以通过

成功找到人
${person}

但是当我输入 requestScope 而不是 viewScope 时                 

$ {person}在jsp

中不再可用

2 个答案:

答案 0 :(得分:3)

Spring webflow follows POST-REDIRECT-GET approach for every request. 
i.e., initial request is split into 2 requests - 
POST processing and then REDIRECT-GET (render view)

In <on-entry>, action happens in first request and so request 
attribute will not survive when view is rendered.

In <on-render>, whole action happens in second request and so 
request attribute will survive when view is rendered.

So put it in <on-render> instead of <on-entry> for request scope. 
View scope value survives from entry to exit of view. 

答案 1 :(得分:0)

如您所知,SWF使用POST-REDIRECT-GET呈现视图,我们可以进行简单的测试,以了解操作的工作原理。将配置更改为 -

<view-state id="bstate" model="foo">
    <on-entry>
        <evaluate expression="myBean.OnEntry()"></evaluate>
    </on-entry>
    <on-render>
        <evaluate expression="myBean.OnRender()"></evaluate>
    </on-render>
</view-state>

访问流程并注意日志

19:08:07.805 [tomcat-http--35] DEBUG o.s.webflow.execution.ActionExecutor - Executing [EvaluateAction@31865599 expression = myBean.onEntry(), resultExpression = [null]]
19:08:07.809 [tomcat-http--35] DEBUG o.s.w.mvc.servlet.FlowHandlerAdapter - Sending flow execution redirect to '/webflow-actions-test/mypath?execution=e3s1'
19:08:07.841 [tomcat-http--36] DEBUG o.s.webflow.execution.ActionExecutor - Finished executing [EvaluateAction@7584e5f9 expression = myBean.onRender(), resultExpression = [null]]; result = success

正如您所看到的那样,首先执行onEntry(),然后重定向,然后调用onRender()。由于这些是两个不同的请求,因此无法从一个请求到另一个请求访问具有请求范围的变量。

正如Prasad所说,你可以将你的逻辑移到渲染上

<on-render>
    <evaluate expression="service.createPerson(22,'Adam', 'Hayek')"
            result="requestScope.person"></evaluate>
</on-render>

并在JSP中访问它

或者您可以将范围更改为flashScope [注意:每次呈现视图时都会清除此范围。这意味着它将在初始重定向后继续存在但如果再次刷新页面则无法使用]或者您可以使用viewScope [变量将在您的视图状态中访问]。如果您想要跨州的变量,可以使用viewScope

有关范围的更多信息 - http://docs.spring.io/autorepo/docs/webflow/2.3.x/reference/html/el.html#el-variables

同样有趣的主题 - http://forum.spring.io/forum/spring-projects/web/web-flow/68756-what-is-the-use-of-request-scope-and-flash-scope