【问题标题】:Spring MVC Getting PathVariables containing dots and slashesSpring MVC 获取包含点和斜杠的 PathVariables
【发布时间】:2012-09-21 22:40:59
【问题描述】:

我想匹配 http://host/10.39284/LKJF283/23332/dd 形式的 URL(其中路径总是以 10 开头。其余部分将是点、斜线、字母和数字的混合)并将整个内容存储在10. 进入 PathVariable。

我在想我可以用这样的正则表达式来做到这一点:

@RequestMapping(value="/{key:10\.+}", method=RequestMethod.GET)
    public String summary(@PathVariable String key, Model model) {
}

但这给了我一个错误,说“无效的转义序列”。知道我该怎么做吗?

【问题讨论】:

标签: spring-mvc


【解决方案1】:

这就是我让它工作的方式。据我所知,Spring 无法处理 URL 中不用作路径分隔符的斜杠。因此,我改为使用此处找到的 url 重写过滤器:http://www.tuckey.org/urlrewrite/

我在 web.xml 中启用了它

<filter>
    <filter-name>UrlRewriteFilter</filter-name>
    <filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>UrlRewriteFilter</filter-name>
    <url-pattern>/*</url-pattern>
    <dispatcher>REQUEST</dispatcher>
    <dispatcher>FORWARD</dispatcher>
</filter-mapping>

然后将此添加到我的 WEB-INF/urlrewrite.xml

<urlrewrite>
    <rule>
       <from>^/(10\..*)$</from>
       <to>/keysummary?key=$1</to>
    </rule>
</urlrewrite>

然后像这样写了我的控制器

@RequestMapping(value="/keysummary", method=RequestMethod.GET)
public String DOISummary(@RequestParam("key") String key, Model model) {
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-04-27
    • 2018-02-10
    • 1970-01-01
    • 1970-01-01
    • 2016-12-12
    • 2020-05-11
    • 1970-01-01
    相关资源
    最近更新 更多