我用的开发框架是Struts2,使用urlrewrite的步骤是先引入相关的包,
然后在web.xml中使用如下配置.
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<!-- 这是UrlRewrite的配置 -->
<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>/*</url-pattern>
</filter-mapping>
<!--结束-->
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.FilterDispatcher
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
<!--重要-->
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
下面是urlrewrite.xml配置。
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE urlrewrite PUBLIC "-//tuckey.org//DTD UrlRewrite 3.2//EN"
"http://tuckey.org/res/dtds/urlrewrite3.2.dtd">
<!--
Configuration file for UrlRewriteFilter
http://tuckey.org/urlrewrite/
-->
<urlrewrite>
<!--以index为例-->
<rule>
<from>/index</from><!-- 地址栏里会显示http://localhost:8080/工程名/index -->
<to>/index.action</to><!-- 实际调用的是index.action -->
</rule>
<rule>
<from>/admin</from>
<to>/blog/admin.jsp</to><!--实际访问的为admin.jsp-->
</rule>
<!--网址中有变量的时候-->
<rule>
<from>/index/([\s\S]*)$</from><!--以$代替变量,变量支持正则表达式-->
<to>/index.action?param=$1</to><!--用$1调用变量-->
</rule>
<!--网址中有多个变量的时候-->
<rule>
<from>/essay/([\s\S]*)/([\s\S]*)$</from><!--多个变量用‘/’分割-->
<to>/main.action?date=$1&id=$2</to><!--变量与变量之间用‘&’连接,重要-->
</rule>
<!--以下不用管-->
<outbound-rule>
<from>/rewrite-status</from>
<to>/test/status/</to>
</outbound-rule>
</urlrewrite>
希望对你有帮助.