2

我正在使用来自 tukey ( http://www.tukey.org/urlrewrite/ )的 URLRewrite 过滤器

现在我创建了一些规​​则,但是有一个案例让我很头疼。URL:/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>

再见

阿德里安

4

1 回答 1

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 代码中,双反斜杠用于表示单个反斜杠。由于字符串是从外部文件中读取的,因此不需要转义反斜杠。

于 2011-11-29T21:52:11.717 回答