如何从事件属性访问 url 并在 spring webflow 中执行外部重定向

How to access url from event attributes and perform external redirect in spring webflow

提问人:shamik 提问时间:10/14/2021 更新时间:10/14/2021 访问量:265

问:

我有一个用例,我需要通过 spring webflow 重定向到外部 URL,每次用户单击链接时,URL 都可能更改。我正在尝试在 flow.xml 中使用 externalRedirect,并在 localAttributeMap 中传入 url 并将其与事件一起返回。

summary.xhtml 代码如下所示。

<h:commandLink action="redirect">
Redirect to new page
</h:commandLink>

flow.xml 如下所示:

<view-state id="summary" view="/flows/forms/summary.xhtml">
  <transition on="redirect" to="retrieveUri" />
</view-state>

<action-state id="retrieveUri">
  <evaluate expression="redirectAction.execute(requestContext, formContext)" />
  <transition on="successRedirect" to="externalView" />
</action-state>

<view-state id="externalView" view="externalRedirect:#{currentEvent.attributes.redirectUrl}">
</view-state>

在我使用的代码库上,我被迫只使用扩展 MultiAction 的 Actions,而我不能使用任何其他具有 String 等返回类型的服务。我的操作类带有返回 Event 的 execute 方法如下所示:

public Event execute(RequestContext context, FormContext formContext) {
  LocalAttributeMap lam = new LocalAttributeMap() ;
  lam.put("redirectUrl", "https://google.com") ;
//Above would of course be replaced with some service layer call but I need to redirect to this url and that too via classes extending MultiAction. 
  return new Event(this, " successRedirect", lam) ;
}

但是,这似乎对我不起作用,我收到以下错误:

Caused by: org.springframework.binding.expression.EvaluationExpression : An ElException occured getting the value for expression 'currentEvent.attributes.redirectUrl' on context [class org.springftamework.webflow.engine.impl.RequestControlContextImpl]
  at org.springframework.binding.expression.spel.SpringElExpression.getValue(SpringElExpression.java:92) 
  at org.springframework.webflow.action.ExternalRedirectAction.doExecute(ExternalRedirectAction.java:42) 
Caused by: org.springframework.expression.spel.SpelEvaluationExpression : EL1007E:(pos 13):  Property or field 'attributes' cannot be found on null
  at org.springframework.expression.spel.ast.PropertyOrFieldReference.readProperty(PropertyOrFieldReference.java:208) 
  at org.springframework.expression.spel.ast.PropertyOrFieldReference.getValueInternal(PropertyOrFieldReference.java:85)
  at org.springframework.expression.spel.ast.PropertyOrFieldReference.getValueInternal(PropertyOrFieldReference.java:78)

作为spring webflow的新手,我不确定这个问题,并希望在理解这个问题以及是否是使用它的正确方法方面得到一些帮助。 项目中spring-webflow的版本为2.3.0.RELEASE

java jsf xhtml spring-webflow

评论

0赞 rptmat57 10/15/2021
尝试将其放入 Flowscope 中
0赞 shamik 10/18/2021
因此,每个请求的 URL 都应该更改,并且由于新 URL 应该打开一个新选项卡,因此用户需要选择返回原始选项卡并再次单击链接,并且应该再次在另一个新选项卡中打开一个新的 URL。我担心,由于流程范围的范围将贯穿整个会话,如果这可能会导致问题(如果不是这种情况,请随时纠正我)。如果 url 是静态 url,则将其添加到 flowScope 将是首选,这意味着它在整个会话中对用户来说都是相同的。
0赞 rptmat57 10/18/2021
嗯,你能尝试使用而不创建新事件等吗?<set name="currentEvent.attributes.redirectUrl" value="action.returnUrlValue()">
1赞 shamik 12/10/2021
因此,在大量挖掘了 spring webflow 的文档之后,我找到了 flowScope 的一个很好的替代品,似乎在这种情况下可以使用 flashScope,因为 flashScope 在每次视图渲染后都会被清除,这就是我希望在应用程序中看到的行为,这就是我现在一直在使用的行为。因此,这意味着,尽管 URL 在每次视图呈现后都会更改,但由于到那时范围将被清除,因此我不会覆盖范围中的现有值

答: 暂无答案