Java URLrewrite Filter无法找到匹配项

时间:2011-11-29 21:41:51

标签: java urlrewriter

我正在使用tuckey(http://www.tuckey.org/urlrewrite/

中的URLRewrite过滤器

现在我创建了一些规​​则,但有一个案例令我头疼。 网址: / group / 1 / update 应改写为: / group?id = 1& action = update

我为这种情况创建了一个正则表达式模式: ^ / group /([0-9] +)/(\ w *)$ ,但过滤器无法重写它。没有比赛。

在我的TestCase中,我通过我的所有模式运行此url,只是为了检查。我按照预期找到了比赛。

assertFalse( "/group/1/update".matches("^/group/create$") );
assertFalse( "/group/1/update".matches("^/group/save$") );
assertFalse( "/group/1/update".matches("^/group/([0-9]+)/?$") );
assertTrue( "/group/1/update".matches("^/group/([0-9]+)/(\\w*)$") );
assertFalse( "/group/1/update".matches("^/group/([0-9]+)/(\\w*)\\?(.*)+$") );

那么为什么过滤器无法找到规则?

只是要包含所有内容,这是我的urlrewrite.xml或其中的一部分:

<rule>
    <from>^/group/create$</from>
    <to>/group?action=create</to>
</rule>
<rule>
    <from>^/group/save$</from>
    <to>/group?action=save</to>
</rule>
<rule>
    <from>^/group/([0-9]+)/?$</from>
    <to>/group?id=$1&amp;action=show</to>
</rule>
<rule>
    <from>^/group/([0-9]+)/(\\w*)$</from>
    <to>/group?id=$1&amp;action=$2</to>
</rule>
<rule>
   <from>^/group/([0-9]+)/(\\w*)\\?(.*)+$</from>
    <to>/group?id=$1&amp;action=$2&amp;$3</to>
</rule>

再见

阿德里安

1 个答案:

答案 0 :(得分:1)

您应该使用单反斜杠\而不是双反斜杠\\

<rule>
    <from>^/group/([0-9]+)/(\w*)$</from>
    <to>/group?id=$1&amp;action=$2</to>
</rule>
<rule>
    <from>^/group/([0-9]+)/(\w*)\?(.*)+$</from>
    <to>/group?id=$1&amp;action=$2&amp;$3</to>
</rule>

在Java代码中,double backalsh用于表示单个反斜杠。由于从外部文件读取字符串,因此无需转义反斜杠。