urlrewrite可以将动态的URL进行伪静态,比如把
xxxx.jsp?id=1转为http://xxxx/xxx/1
这里发现一个好工具:tuckey urlrewrite,使用方法如下:
1 下载urlrewrite,官方下载地址:http://tuckey.org/urlrewrite/dist/urlrewritefilter-2.6.zip
2 放到项目的LIB包中
3 建立urlrewrite.xml文件,放到WEB-INF
目录下,编写如下:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE urlrewrite PUBLIC "-//tuckey.org//DTD UrlRewrite 2.6//EN"
"http://tuckey.org/res/dtds/urlrewrite2.6.dtd">
<!--
Configuration file for UrlRewriteFilter
http://tuckey.org/urlrewrite/
-->
<urlrewrite>
<rule>
<from>^/([a-z]+)$</from>
<to type= "forward" >/world.jsp?id=$1</to>
</rule>
<rule>
<from>^/world/(.*)$</from>
<to>/world.jsp?tid=$1</to>
</rule>
<rule>
<from>^/(.*).html$</from>
<to>/test1/$1.jsp</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>
</urlrewrite>
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE urlrewrite PUBLIC "-//tuckey.org//DTD UrlRewrite 2.6//EN"
"http://tuckey.org/res/dtds/urlrewrite2.6.dtd">
<!--
Configuration file for UrlRewriteFilter
http://tuckey.org/urlrewrite/
-->
<urlrewrite>
<rule>
<from>^/([a-z]+)$</from>
<to type= "forward" >/world.jsp?id=$1</to>
</rule>
<rule>
<from>^/world/(.*)$</from>
<to>/world.jsp?tid=$1</to>
</rule>
<rule>
<from>^/(.*).html$</from>
<to>/test1/$1.jsp</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>
</urlrewrite>
rule是url重写规则,from是显示出来的地址,to是映射的实际地址,$1是重写参数,可以为多个,()里是匹配的正则表达式.
比如:
<rule>
<from>/(.*)/login.htm</from>
<to type="forward">/yui/login.htm?name=$1</to>
</rule>
http://localhost:8080/app/abc/login.htm其实际请求的url为http://localhost:8080/app/yui/login.htm?name=abc
4 配置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>
</filter-mapping>
<listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/urlrewrite.xml </param-value>
</context-param>
urlrewrite可以将动态的URL进行伪静态
最新推荐文章于 2021-03-20 08:52:34 发布