2013-04-24 50 views
1

我们有一个要求,在我们网站的一个子部分中处理登录和注册的方式略有不同。我们需要为以下每个URI分别分配流量。Spring流web URI映射到包含斜线的流ID(“/”)

/登录
/注册
/分段/登录
/分段/注册

我们流注册表使用位置图案被配置为在一个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 

的调试启用日志记录,我可以在四个流,ID分别创建的日志“登录”,“注册”,“小节/登录”和“小节/注册”看到:

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映射为“登录”(而不是“小节/登录”)或“注册”(而不是“小节/注册”):

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只是“注册”。

我在这里做错了什么?有什么办法强制所需的流量映射?

谢谢!

回答

2

我想通了。对于DefaultFlowUrlHandler文档状态:

期望的URL来启动流量控制到这种模式的:

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

所以对于URI“/小节/注册”,应用程序上下文路径是空的(对于我们的webapp),app servlet路径是“小节”,流程路径是“注册”。因此,这是映射到“注册”flowId而不是“小节/注册”。

我解决了这个问题通过继承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); 
    } 
} 

然后我在Webflow的情况下注入这个bean到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> 

也许还有更好的方法,但这工作。