Struts2中UrlRewriterFilter(url重写)的初步使用

之前做的网站基本成型,不过,听说要对搜索引擎友好点,还得要将网站弄成静态化,至少要做点“伪静态”的工作。而我对url rewrite早有所闻,于是乎就上[url]其官网[/url] 下了个来玩。入门很简单,下载地址是[url]http://tuckey.org/urlrewrite/dist/urlrewritefilter-2.6.zip[/url] ,下载来解压。将解压出来的urlrewrite-2.6.0.jar复制到项目中的WEB-INF\lib下,还有urlrewrite.xml复制到WEB-INF下。

接下来就是配置了,配置也是超级简单的。

web.xml里的配置如下,在urlrewrite.xml里面也有注释说明怎么添加的。

<!– UrlRewriteFilter –>
<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的配置上面。还要在struts2的配置里加上两行,具体如下:

<!– 配置struts2 –>
<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>



接下来是修改配置urlrewrite.xml里的内容,它的规则如下:

<urlrewrite>

<rule>
<note>
The rule means that requests to /test/status/ will be redirected to /rewrite-status
the url will be rewritten.
</note>
<from>/test/status/</from>
<to type="redirect">%{context-path}/rewrite-status</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>

只要你自己把需要重写的URL规则写上去就好了,我的一个例子就是:

<rule>
<note>shop</note>
<from>/shops.html</from>
<to>/shops/findShops.action</to>
</rule>

这样访问shops.html就是访问/shops/findShops.action。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值