题
我使用的URLRewrite过滤器,从tuckey( http://www.tuckey.org/urlrewrite/ )
现在我创造了一些规则,但有一种情况下,让我头疼。URL: /组/1/更新 应改写为: /组?id=1&action=更新
我创建了一个regexp的模式为这种情况下: ^/组/([0-9]+)/(\w*)$, 但该过滤器是无法改写。没有匹配。
在我的测试用例,我跑了这个网址通过我所有的模式,只是为了检查。我找到了匹配如我的预期。
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&action=show</to>
</rule>
<rule>
<from>^/group/([0-9]+)/(\\w*)$</from>
<to>/group?id=$1&action=$2</to>
</rule>
<rule>
<from>^/group/([0-9]+)/(\\w*)\\?(.*)+$</from>
<to>/group?id=$1&action=$2&$3</to>
</rule>
再见了
艾德里安
解决方案
你应该使用单一的反斜杠 \
而不是双杠 \\
.
<rule>
<from>^/group/([0-9]+)/(\w*)$</from>
<to>/group?id=$1&action=$2</to>
</rule>
<rule>
<from>^/group/([0-9]+)/(\w*)\?(.*)+$</from>
<to>/group?id=$1&action=$2&$3</to>
</rule>
在Java代码,双backalsh是用来表示一个单一的反斜杠。由于串读从外部文件,没有必要在逃避反斜杠。
不隶属于 StackOverflow