1、下载urlrewrite-2.6.0.jar 放到所在项目 WEB-INF/lib下
2、把文件 urlrewrite.xml 放到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">
<!-- %{context-path}
type : forward | redirect
-->
<urlrewrite>
<rule>
<from>/test_(.*)_(.*)</from>
<to type="forward">/test.action?aatest=$1&aatest1=$2</to>
</rule>
</urlrewrite>
3、配置 web.xml
<!-- url重写-->
<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>
<!-- 配置struts2 start -->
<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>*.action</url-pattern>
<dispatcher>REQUEST </dispatcher>
<dispatcher>FORWARD </dispatcher>
<dispatcher>INCLUDE </dispatcher>
</filter-mapping>
<!-- 配置struts2 end -->
这样就完成的url的重写,原来要访问的action是这样的 http://localhost:8080/goods/test.action?aatest=值1&aatest2=值2
重写后,变成了 http://localhost:8080/goods/test_值1_值2 ,这样看起来好看多了吧。
-------------------------------------------------------------------------------------------------------------------------------------
常用rule
<!--
<rule>
<from>/test_(.*)_(.*)</from>
<to type="forward">/test.action?aatest=$1&aatest1=$2</to>
</rule>
test.action?aatest=值1&aatest1=值2
重写后 test_值1_值2
-->
------------------------------------------------------------------------
<rule>
<from>^/test/(.*)$</from>
<to>/test.action?aatest=$1</to>
<!--
test.action?aatest=参数1
重写后 test/参数1
-->
</rule>
------------------------------------------------------------------------
原来 test.action?aatest=参数 test.action?aatest=0005
重写后 test/参数.html test/0005.html
<rule>
<from>^/test/(.*).html</from>
<to type="forward">/test.action?aatest=$1</to>
</rule>