gpt4 book ai didi

java - Spring webflow URI 映射到包含斜杠 ("/"的流 ID

转载 作者:行者123 更新时间:2023-11-30 04:24:52 24 4
gpt4 key购买 nike

我们要求在我们网站的某个部分中以稍微不同的方式处理登录和注册。我们需要为以下每个 URI 提供单独的流。

/登录
/注册
/小节/登录
/小节/注册

我们的流注册表是使用 webflow 文档中的位置模式进行配置的:

   <flow:flow-registry id="flowRegistry" base-path="/WEB-INF/flow" flow-builder-services="flowBuilderServices">
<flow:flow-location-pattern value="/**/*-flow.xml" />
</flow:flow-registry>

流程 XML 文件的结构如下:

./root/src/main/webapp/WEB-INF/flow
|-- subsection
| |-- login
| | `-- subsection-login-flow.xml
| `-- signup
| `-- subsection-signup-flow.xml
|-- login
| `-- login-flow.xml
`-- signup
`-- signup-flow.xml

启用 DEBUG 日志记录后,我可以在日志中看到使用 ID“login”、“signup”、“subsection/login”和“subsection/signup”创建了四个流:

24-Apr 09:21:50,887 DEBUG RMI TCP Connection(2)-127.0.0.1 definition.registry.FlowDefinitionRegistryImpl -  Registering flow definition 'ServletContext resource [/WEB-INF/flow/subsection/login/subsection-login-flow.xml]' under id 'subsection/login'   
24-Apr 09:21:50,887 DEBUG RMI TCP Connection(2)-127.0.0.1 definition.registry.FlowDefinitionRegistryImpl - Registering flow definition 'ServletContext resource [/WEB-INF/flow/subsection/signup/subsection-signup-flow.xml]' under id 'subsection/signup'
24-Apr 09:21:50,888 DEBUG RMI TCP Connection(2)-127.0.0.1 definition.registry.FlowDefinitionRegistryImpl - Registering flow definition 'ServletContext resource [/WEB-INF/flow/login/login-flow.xml]' under id 'login'
24-Apr 09:21:50,889 DEBUG RMI TCP Connection(2)-127.0.0.1 definition.registry.FlowDefinitionRegistryImpl - Registering flow definition 'ServletContext resource [/WEB-INF/flow/signup/signup-flow.xml]' under id 'signup'

当我访问http://mysite.com/login时或http://mysite.com/signup ,一切都按预期进行。

但是,当我转到http://mysite.com/subsection/login时或http://mysite.com/subsection/signup ,URI 被映射到“login”(而不是“subsection/login”)或“signup”(而不是“subsection/signup”):

24-Apr 09:32:16,917 DEBUG ajp-bio-8009-exec-5 mvc.servlet.FlowHandlerMapping -  Mapping request with URI '/subsection/signup' to flow with id 'signup'

我在调试器中对此进行了跟踪,DefaultFlowUrlHandler.getFlowId 方法仅根据最后一个“/”后面的内容返回流 ID,因此对于 URI“/subsection/signup”,返回的流 ID 很简单“注册”。

我在这里做错了什么?有什么方法可以强制实现所需的流映射吗?

谢谢!

最佳答案

我明白了这一点。 DefaultFlowUrlHandler 的文档指出:

Expects URLs to launch flow to be of this pattern:

http://<host>/[app context path]/[app servlet path]/<flow path>

因此,对于 URI“/subsection/signup”,应用程序上下文路径为空(对于我们的 web 应用程序),应用程序 servlet 路径为“subsection”,流路径为“signup”。因此,这是映射到“signup”flowId,而不是“subsection/signup”。

我通过子类化 DefaultFlowUrlHandler 并重写 getFlowId 方法解决了这个问题:

public class UriFlowUrlHandler extends DefaultFlowUrlHandler
{
@Override
public String getFlowId(HttpServletRequest request)
{
// Strip off leading "/" and any file extension (e.g., ".jsp")
String uriNoExtension = StringUtils.substringBeforeLast(request.getRequestURI().substring(1), ".");

if (StringUtils.isNotEmpty(uriNoExtension))
return uriNoExtension;
else
return super.getFlowId(request);
}
}

然后我将此 bean 注入(inject)到我的 webflow 上下文中的 FlowHandlerMapping 中:

   <bean id="uriFlowUrlHandler" class="com.mycompany.web.spring.UriFlowUrlHandler" />

<bean class="org.springframework.webflow.mvc.servlet.FlowHandlerMapping">
<property name="flowRegistry" ref="flowRegistry" />
<property name="alwaysUseFullPath" value="true" />
<property name="flowUrlHandler" ref="uriFlowUrlHandler" />
</bean>

也许有更好的方法,但这可行。

关于java - Spring webflow URI 映射到包含斜杠 ("/"的流 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16199365/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com