【问题标题】:Spring webflow URI mapping to flow ID containing slash ("/")Spring webflow URI映射到包含斜杠(“/”)的流ID
【发布时间】:2013-04-25 14:15:33
【问题描述】:

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

/登录
/注册
/subsection/登录
/subsection/注册

我们的 flow-registry 是使用 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/loginhttp://mysite.com/signup 时,一切正常。

但是,当我转到 http://mysite.com/subsection/loginhttp://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 很简单“注册”。

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

谢谢!

【问题讨论】:

    标签: java spring-webflow spring-webflow-2


    【解决方案1】:

    我想通了。 DefaultFlowUrlHandler 的文档指出:

    希望启动流程的 URL 采用这种模式:

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

    所以对于 URI“/subsection/signup”,应用上下文路径为空(对于我们的 webapp),应用 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 注入到我的 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>
    

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

    【讨论】:

      猜你喜欢
      • 2015-03-13
      • 2014-04-16
      • 2012-08-16
      • 1970-01-01
      • 2012-07-18
      • 2014-02-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多