首先导入 urlrewrite-2.6.0.jar
在WEB-INF下创建urlrewrite.xml文件
<?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"> <urlrewrite> <rule> <from>/user/index.html</from><!-- 受访的路径 --> <to>/user/userInfo_index.action</to><!-- 实际访问的路径 --> </rule> <!-- 在from标签中支持使用正则 $1表示from中第一个匹配的值 --> <rule> <from>/user/index-page-([0-9]+).html</from> <to>/user/userInfo_index.action?pager.curpage=$1</to> </rule> </urlrewrite>
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>
如果在项目中运用了struts2 还需要在struts2的配置中添加
web.xml
<filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> <dispatcher>REQUEST </dispatcher> <dispatcher>FORWARD </dispatcher> <dispatcher>INCLUDE </dispatcher> </filter-mapping>