1.jar包支持urlrewritefilter-4.0.3.jar http://files.cnblogs.com/simpledev/urlrewritefilter-4.0.3.rar
2.在web.xml文件中加入:
<filter> <filter-name>UrlRewriteFilter</filter-name> <filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class> <init-param> <param-name>logLevel</param-name> <param-value>WARN</param-value> </init-param> </filter> <filter-mapping> <filter-name>UrlRewriteFilter</filter-name> <url-pattern>/*</url-pattern> <dispatcher>REQUEST</dispatcher> <dispatcher>FORWARD</dispatcher> </filter-mapping>
3.在项目的WEB-INF/文件夹下面加入:urlrewrite.xml
<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE urlrewrite PUBLIC "-//tuckey.org//DTD UrlRewrite 4.0//EN" "http://www.tuckey.org/res/dtds/urlrewrite4.0.dtd"> <!-- Configuration file for UrlRewriteFilter http://www.tuckey.org/urlrewrite/ --> <urlrewrite> <rule> <note> The rule means that requests to /test/status/ will be redirected to /rewrite-status the url will be rewritten. </note> <from>/test/status/</from> <to type="redirect">%{context-path}/rewrite-status</to> </rule> <outbound-rule> <note> The outbound-rule specifies that when response.encodeURL is called (if you are using JSTL c:url) the url /rewrite-status will be rewritten to /test/status/. The above rule and this outbound-rule means that end users should never see the url /rewrite-status only /test/status/ both in thier location bar and in hyperlinks in your pages. </note> <from>/rewrite-status</from> <to>/test/status/</to> </outbound-rule> <!-- 养车互动 --> <rule> <from>/hudong.html</from> <to>/hudong.jsp</to> </rule> <rule> <from>/hudong/article/(d+).html</from> <to>/careArticleAction?actionName=detail&id=$1</to> </rule> <rule> <from>/hudong/article/(d+)-(d+).html</from> <to>/careArticleAction?actionName=detail&id=$1&pageId=$2</to> </rule> <rule> <from>/hudong/listArticle.html</from> <to>/careArticleAction?actionName=listArticle</to> </rule> <rule> <from>/hudong/listArticle-(d+).html</from> <to>/careArticleAction?actionName=listArticle&pageId=$1</to> </rule> </urlrewrite>
注意:变量用$1,$2,$3等来取代,数字(d+),字母(w+)正则表达式来表达。
&符号一定要写成: &否则xml文件加载失败~!!!
. 匹配除换行符以外的任意字符
/w 匹配字母或数字或下划线或汉字
/s 匹配任意的空白符
/d 匹配数字
/b 匹配单词的开始或结束
^ 匹配字符串的开始
$ 匹 配字符串的结束
项目启动之后:如访问http://localhost:8080/BNCAR2/rewrite-status 如果xml文件没有错误,会显示正常,否则会提示错误位置信息等。
以上链接伪静态代表的含义为:
访问http://localhost:8080/BNCAR2/hudong.html 代替http://localhost:8080/BNCAR2/hudong.jsp
访问http://localhost:8080/BNCAR2/hudong/article/100.html 代替http://localhost:8080/BNCAR2/careArticleAction?actionName=detail&id=100
访问http://localhost:8080/BNCAR2/hudong/article/100-1.html 代替http://localhost:8080/BNCAR2/careArticleAction?actionName=detail&id=100&pageId=1
访问http://localhost:8080/BNCAR2/hudong/listArticle.html 代替http://localhost:8080/BNCAR2/careArticleAction?actionName=listArticle