困扰了一天的urlwriter问题:
需求:将输入类似/test/abc-333-1.html的url转化为/test/333/1/abc-333-1.html的形式,
例如/test/abc-333-1.html 转为 /test/333/1/abc-333-1.html
/test/abc-11-2.html 转为 /test/11/2/abc-11-2.html
/test/abc-11-3.html 转为 /test/11/3/abc-11-3.html
,我写的urlwriter的rule如下:
<rule>
<note>
</note>
<from>/test/(.*?)\\-([0-9]{1,3})\\-[0-9]{1,}\\.html$</from>
<to>/test/$2/$3/$1-$2-$3.html</to>
</rule>
输入url:http://localhost:8080/test/AD-Sound-Recorder-3-0.html
提示错误如下:
HTTP Status 404 - /test/AD-Sound-Recorder-3-0.html
--------------------------------------------
type Status report
message /test/AD-Sound-Recorder-3-0.html
description The requested resource (/test/AD-Sound-Recorder-3-0.html) is not available.
----------------------------------------------
从以上错误信息可以看出,该url地址没有转换(否则应该报找不到test/3/0/AD-Sound-Recorder-3-0.html)
翻开urlrewriter的文档,里面说urlrewriter的执行方式如下:
When executing a rule the filter will (very simplified) do something like this psuedo code:
Pattern.compile(<from> element);
pattern.matcher(each request url);
matcher.replaceAll(<to> element);
if ( <condition> elements match && pattern matched ) {
execute <run> elements (if any)
perform <to> element (if any)
}
也就是说把/test/(.*?)\\-([0-9]{1,3})\\-[0-9]{1,}\\.html$作为正则表达式来解析,于是另外写了一个
Test.java如下
public static void main(String[] args)
{
String url = "http://localhost:8080/test/Easy-Karaoke-Recorder--3-0.html";
Pattern p = Pattern.compile("/test/(.*?)\\-([0-9]{1,3})\\-[0-9]{1,}\\.html$");
Matcher mat = p.matcher(url);
while(mat.find())
{
String str = mat.group();
System.out.println(str);
}
}
打印的结果为test/Easy-Karaoke-Recorder--3-0.html,也就是说输入的Url可以用正则表达式解析出来,
可是在urlrewriter中不管用,请问是为什么?
[解决办法]
关注,urlwriter,帮顶