Urlrewrite的配置与使用方法:
1. 导入urlrewrite包:urlrewrite-2.6.0.jar(见附件)
2. 配置web.xml:
<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>
<url-pattern>*.jsp</url-pattern>
<url-pattern>/struts2/*</url-pattern>
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
</filter-mapping>
<!-- 动态url转化为静态url -->
<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>*.htm</url-pattern>
</filter-mapping>
3. 配置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>
<note>导航</note>
<from>
/(index|show|magazinelc|enroll).htm$
</from>
<to>/$1Action!list.action</to>
</rule>
<rule>
<note>直接调用方法的链接</note>
<from>/([a-zA-z]+)-([a-zA-z]+).htm$</from>
<to>/$1Action!$2.action</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>
4. 使用:struts.xml中
<package name="MyShopping" extends="struts-default">
<action name="bookAction" class="com.action.BookAction" >
<result name="ok">admin/book/bookDetail.jsp</result>
</action>
</package>
5. 页面中:引用了直接方法调用的url形式:
action="/MyShopping/book-Detail.htm"