web-inf配置
<filter> <filter-name>UrlRewriteFilter</filter-name> <filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class> <!-- 检查urlrewrite.xml加载设置时间为秒,开发时使用 --> <init-param> <param-name>confReloadCheckInterval</param-name> <param-value>6</param-value> </init-param> <init-param> <param-name>logLevel</param-name> <param-value>info</param-value> </init-param> </filter> <filter-mapping> <filter-name>UrlRewriteFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
urlrewrite.xml配置:
<!-- 以下是测试配置 开发完会清除掉 --> <rule> <name>测试有后缀的url</name> <note> 测试有后缀的url美化后的url会加一个后缀如html </note> <from>^/t/normal(.html|.htm|.jsp|.do|.h5)$</from> <to>/test/normal</to> </rule> <rule> <from>^/test/rule$</from> <to>/test/normal</to> </rule> <outbound-rule> <from>/test/normal</from> <to>/test/rule</to> </outbound-rule> <rule> <name>测试多层次url</name> <note> 测试多层次url </note> <from>^/r$</from> <to>/test/rule/abc</to> </rule> <outbound-rule> <from>/test/rule/abc</from> <to>/r</to> </outbound-rule> <rule> <name>测试单个参数url</name> <note> 测试单个参数url </note> <from>^/rule/([0-9]+)$</from> <to>/test/rule_1?id=$1</to> </rule> <outbound-rule> <from>/test/rule_1\?id=([0-9]+)</from> <to>/rule/$1</to> </outbound-rule> <rule> <name>测试多个参数url</name> <note> 测试多个参数url </note> <from>^/rule/([0-9]+)_([a-zA-Z0-9]+)_([0-9]+)_([0-9]+)$</from> <to>/test/rule_2?id=$1&name=$2&pageNo=$3&pageSize=$4</to> </rule> <outbound-rule> <from>/test/rule_2\?id=([0-9]+)&name=([a-zA-Z0-9]+)&pageNo=([0-9]+)&pageSize=([0-9]+)</from> <to>/rule/$1_$2_$3_$4</to> </outbound-rule> <rule> <name>测试多个参数含有特殊字符参数url</name> <note> 测试多个参数url </note> <from>^/r/(.*)/(.*)/([0-9]+)_([0-9]+)$</from> <to>/test/rule_3?special=$1&zh=$2&pageNo=$3&pageSize=$4</to> </rule> <outbound-rule> <from>/test/rule_3\?special=(.*)&zh=(.*)&pageNo=([0-9]+)&pageSize=([0-9]+)</from> <to>/r/$1/$2/$3_$4</to> </outbound-rule> <rule> <from>^/rule/([0-9]+)/([a-zA-Z0-9]+)/([0-9]+)/([0-9]+)$</from> <to>/test/rule_2?id=$1&name=$2&pageNo=$3&pageSize=$4</to> </rule> <outbound-rule> <from>/test/rule_2\?id=([0-9]+)&name=([a-zA-Z0-9]+)&pageNo=([0-9]+)&pageSize=([0-9]+)</from> <to>/rule/$1/$2/$3/$4</to> </outbound-rule>
页面配置
<rapid:override name="content"> <table border="1"> <tr> <th width="5">ID</th> <th width="120" height="40">重写后的URL</th> <th width="400">实际URL(鼠标放到下面URL上显示美化过的URL)</th> <th width="350">作用</th> <th width="500">urlrewrite配置</th> </tr> <tr> <td></td> </tr> <tr> <td>1</td> <td height="30"><a href="${ctx }/test/normal">/test/normal</a></td> <td><a href="<c:url value="/test/normal"/>">/test/normal</a></td> <td>不重写直接过去</td> <td>不需要配置</td> </tr> <tr><td>2</td> <td height="30"><a href="${ctx }/t/normal.htm">/t/normal.htm</a></td> <td><a href="<c:url value="/test/normal"/>">/test/normal</a></td> <td>重写后缀如:.html|.htm|.jsp|.do|.h5</td> <td> <rule> <br> <from>^/t/normal(.html|.htm|.jsp|.do|.h5)?$</from> <br> <to>/test/normal</to> <br> </rule> </td> </tr> <tr><td>3</td> <td height="30"><a href="${ctx }/test/rule">/test/rule</a></td> <td><a href="<c:url value="/test/normal"/>">/test/normal</a></td> <td>Urlwriter优先于springmvc的控制器,因为他是filter,而springmvc的控制器是servlet.只会到/test/normal所以不会转到Controller对应的/test/rule</td> <td> <rule> <br> <from>^/test/rule$</from> <br> <to>/test/normal</to> <br> </rule> <br/> <outbound-rule> <br> <from>/test/normal</from> <br> <to>/test/rule</to> <br> </outbound-rule> </td> </tr> <tr><td>4</td> <td height="30"><a href="${ctx }/r">/r</a></td> <td><a href="<c:url value="/test/rule/abc"/>">/test/rule/abc</a></td> <td>重写了资源文件目录</td> <td> <rule> <br> <from>^/r$</from> <br> <to>/test/rule/abc</to> <br> </rule> <br/> <outbound-rule> <br> <from>/test/rule/abc</from> <br> <to>/r</to> <br> </outbound-rule> </td> </tr> <tr><td>5</td> <td height="30"><a href="${ctx }/rule/123456">/rule/123456</a></td> <td><a href="<c:url value="/test/rule_1?id=123456"/>">/test/rule_1?id=123456</a></td> <td>重写了资源文件目录并且重写 单个 参数</td> <td> <rule> <br> <from>^/rule/([0-9]+)$</from> <br> <to>/test/rule_1?id=$1</to> <br> </rule> <br/> <outbound-rule> <br> <from>/test/rule_1\?id=([0-9]+)</from> <br> <to>/rule/$1</to> <br> </outbound-rule> </td> </tr> <tr><td>6</td> <td height="30"><a href="${ctx }/rule/5_v5zghw_1_20">/rule/5_v5zghw_1_20</a></td> <td><a href="<c:url value="/test/rule_2?id=5&name=v5zghw&pageNo=1&pageSize=20"/>">/test/rule_2?id=5&name=v5zghw&pageNo=1&pageSize=20</a></td> <td>多级目录加多参数重写 注意:这里的参数不要包含特殊字符 只包含了 数字或者26个字母大小写组成的内容</td> <td> <rule> <br> <from>^/rule/([0-9]+)_([a-zA-Z0-9]+)_([0-9]+)_([0-9]+)$</from> <br> <to>/test/rule_2?id=$1&name=$2&pageNo=$3&pageSize=$4</to> <br> </rule> <br/> <outbound-rule> <br> <from>/test/rule_2\?id=([0-9]+)&name=([a-zA-Z0-9]+)&pageNo=([0-9]+)&pageSize=([0-9]+)</from> <br> <to>/rule/$1_$2_$3_$4</to> <br> </outbound-rule> </td> </tr> <tr> <td>7</td> <td height="30"><a href="${ctx }/r/.,_:/汉字/9_2">/r/.,_:/汉字/9_2</a></td> <td><a href="<c:url value="/test/rule_3?special=.,_:&zh=汉字&pageNo=9&pageSize=2"/>">/test/rule_3?special=.,_:&zh=汉字&pageNo=9&pageSize=2</a></td> <td>多级目录加多参数重写 包含了特殊字符 注意:特殊字符或中文的可以用 / ,仅字母和数字的可以用 _ 组合</td> <td> <rule> <br> <from>^/r/(.*)/(.*)/([0-9]+)_([0-9]+)$</from> <br> <to>/test/rule_3?special=$1&zh=$2&pageNo=$3&pageSize=$4</to> <br> </rule> <br/> <outbound-rule> <br> <from>/test/rule_3\?special=(.*)&zh=(.*)&pageNo=([0-9]+)&pageSize=([0-9]+)</from> <br> <to>/r/$1/$2/$3_$4</to> <br> </outbound-rule> </td> </tr> </table> <a href="${ctx }/rule/1/show/2/10">测试-1</a> <c:url value="/test/rule_2?id=5&name=v5zghsdsddsw&pageNo=1&pageSize=20" var="tempUrl"> </c:url> <a href="${tempUrl }">跳转-2</a><br/> 注意事项:1.具有相同返回规则时:不应使用outbound-rule 看1.2.3中实际URL鼠标放上的地址是一样,是不正确的。 2. </rapid:override>
Java代码:
package com.aolai.web.controller; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.RequestMapping; @Controller public class TestReurlController { @RequestMapping("/test/normal") public String testNormal(ModelMap model) { String realUrl = "/test/normal"; model.put("realUrl", realUrl); System.out.println(realUrl); return "test/index"; } @RequestMapping("/test/rule") public String testRule(ModelMap model) { String realUrl = "/test/rule"; model.put("realUrl", realUrl); System.out.println(realUrl); return "test/index"; } @RequestMapping("/test/rule/abc") public String testRuleAbc(ModelMap model) { String realUrl = "/test/rule/abc"; model.put("realUrl", realUrl); System.out.println(realUrl); return "test/index"; } @RequestMapping("/test/rule_1") public String testRule1(String id, ModelMap model) { String realUrl = "/test/rule_1?id=" + id; model.put("realUrl", realUrl); System.out.println(realUrl); return "test/index"; } @RequestMapping("/test/rule_2") public String testRule2(String id, String name, Integer pageNo, Integer pageSize, ModelMap model) { String realUrl = "/test/rule_2?id=" + id + "&name=" + name + "&pageNo=" + pageNo + "&pageSize=" + pageSize; model.put("realUrl", realUrl); System.out.println(realUrl); return "test/index"; } @RequestMapping("/test/rule_3") public String testRule3(String special, String zh, Integer pageNo, Integer pageSize, ModelMap model) { String realUrl = "/test/rule_3?special=" +special + "&zh=" + zh + "&pageNo=" + pageNo + "&pageSize=" + pageSize; model.put("realUrl", realUrl); System.out.println(realUrl); return "test/index"; } @RequestMapping("/test/out_bound_rule") public String testOutBoundRule(ModelMap model) { System.out.println("/test/out_bound_rule"); return "test/index"; } @RequestMapping("/test/out_bound_rule1") public String testOutBoundRule1(String id, ModelMap model) { System.out.println("/test/out_bound_rule1?id=" + id); return "test/index"; } @RequestMapping("/test/out_bound_rule2") public String testOutBoundRule2(String id, String name, Integer pageNo, Integer pageSize, ModelMap model) { System.out.println("/test/out_bound_rule2?id=" + id + "&name=" + name + "&pageNo=" + pageNo + "&pageSize=" + pageSize); return "test/index"; } @RequestMapping("/rule2") public String test2(String id, String name, Integer pageNo, Integer pageSize, ModelMap model) { System.out.println("/test/out_bound_rule2?id=" + id + "&name=" + name + "&pageNo=" + pageNo + "&pageSize=" + pageSize); return "test/index"; } }