一:到 http://www.tuckey.org/urlrewrite/#download下载urlrewrite Jar包(我用的是urlrewrite-3.2.0.jar)。
二:解压所下载的文件,把urlrewrite-3.2.0.jar复制到项目的WebRoot/WEB-INF/lib/目录下。
三:把urlrewrite.xml复制到项目的WebRoot/WEB-INF/目录下。
四:在web.xml文件中加入以下
<filter> <filter-name>urlRewriteFilter</filter-name> <filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class> </filter> <filter-mapping> <filter-name>urlRewriteFilter</filter-name> <url-pattern>*.htm</url-pattern> <dispatcher>REQUEST</dispatcher> <dispatcher>FORWARD</dispatcher> </filter-mapping>
(UrlRewriteFilter这个可以更改,但是如果要更改的话,就两个UrlRewriteFilter都一起改为相同的字符串,比如说都改为:vic也是可以的。其实这个在urlrewrite.xml里有说明,仔细看看就知道了。
五:接下来就是urlrewrite.xml里面的规则的写法了
1、单页面规则写法:(记得不要把项目名写到里面去了!)
<rule>
<from>^/index.html</from>
<to>/index.jsp</to>
</rule>
2、带一个参的URL规则写法
把:article.jsp?id=参数 伪成:article_参数.html
<rule>
<from>^/article_(.[0-9]*).html</from>
<to>/article.jsp?id=$1</to>
</rule>
()里面是正则表达式,自己去找找这方面的知识,美元符号$1的意思是匹配第一个正则表达式。
3、带两个参和多个参的URL规则写法
把article.jsp?id=参1&type=参2伪成article-参1-参2.html
<rule>
<from>^/article-(.[0-9]*)-(.[0-9]*).html</from>
<to>/article.jsp?id=$1&type=$2</to>
</rule>
注意一点:用“&”来代替”&“,多个参的url规则基本类似,依葫芦画瓢,应该能写出来!哦,还有一点,那就是
jsp页面上的url的写法,href=”article_<%=news.getArticleID()
%>.html”,如果前面加项目名的话,就再多加个”/”。
项目中urlrewrite.xml菜单伪静态如下:
<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE urlrewrite PUBLIC "-//tuckey.org//DTD UrlRewrite 3.0//EN" "http://tuckey.org/res/dtds/urlrewrite3.0.dtd"> <urlrewrite> <rule> <note>文章分类</note> <from>^/content/article_list/(\w+).htm$</from> <to type="forward">/shop/article!list.action?sign=$1</to> </rule> <rule> <note>商品分类</note> <from>^/content/goods_list/(\w+).htm$</from> <to type="forward">/shop/goods!list.action?sign=$1</to> </rule> <rule> <note>商品评论</note> <from>^/content/comment_list/(\w+).htm$</from> <to type="forward">/shop/comment!list.action?id=$1</to> </rule> <rule> <note>在线留言</note> <from>^/content/leave_message.htm$</from> <to type="forward">/shop/leave_message!list.action</to> </rule> </urlrewrite>