---
title: "struts2 urlrewrite 404 错误解决办法"
author: "lichun01"
date: "2012-08-25T12:06:29Z"
tags: []
categories: ["编程语言"]
slug: "struts2-urlrewrite-404"
draft: false
lastmod: "2012-08-22T12:00:46Z"
id: 1653197
url: "https://lichun01.iteye.com/blog/1653197"
---

网站已经开发完毕，struts2+spring+hibernate。

    例如访问某文章的地址为：[http://localhost:8080/showArticle/showArticle/showArticleAction.action?article.id=2](http://localhost:8080/showArticle/showArticle/showArticleAction.action?article.id=2) ，但是各个搜索引擎对动态页面的收录不是很好，所以想到网站静态化。

目前网站静态化大致有两种实现方式，一种就是生成html文件。另外一种就是对外界发布的时html地址，但是服务器内部依然时动态地址，这种实现方式又成为伪静态。由于项目已经开发完毕本人又比较懒所以选择伪静态实现网站的静态化。这里选用的是urlwrite。

下载了urlrewritefilter-2.5.0.zip，下载地址自己到网上检索下。

网站伪静态化步骤：

    1、下载后把urlrewrite-2.5.2.jar包添加到工程中。

    2、在web-info下建立urlrewriter.xml 内容如下：

&lt;?xml version="1.0" encoding="UTF-8"?&gt;  
&lt;urlrewrite&gt;  
 &lt;rule&gt;  
   &lt;from&gt;/showArticle\_(\[a-z0-9]+).html&lt;/from&gt;  
         &lt;to type="forward"&gt;/showArticleAction.action?article.id=$1&lt;/to&gt;  
 &lt;/rule&gt;  
 &lt;rule&gt;  
   &lt;from&gt;/showList.html&lt;/from&gt;  
         &lt;to type="forward"&gt;/showListAction.action&lt;/to&gt;  
 &lt;/rule&gt;  
&lt;/urlrewrite&gt;

       3、在web.xml中添加flter，代码如下：

&lt;filter&gt;

 

 &lt;filter-name&gt;UrlRewriteFilter&lt;/filter-name&gt;  
  &lt;filter-class&gt;org.tuckey.web.filters.urlrewrite.UrlRewriteFilter&lt;/filter-class&gt;  
  &lt;init-param&gt;  
   &lt;param-name&gt;logLevel&lt;/param-name&gt;  
   &lt;param-value&gt;WARN&lt;/param-value&gt;  
  &lt;/init-param&gt;  
 &lt;/filter&gt;  
 &lt;filter-mapping&gt;  
  &lt;filter-name&gt;UrlRewriteFilter&lt;/filter-name&gt;  
  &lt;url-pattern&gt;/\*&lt;/url-pattern&gt;  
 &lt;/filter-mapping&gt;

注意：**该filter要在Struts的filter之上**

 

加入到工程后测试，发现转向到action的时候总是报404的错误，

修改为跳转到静态页面或者jsp可以跳转，但是换成Action路径不成。

分析原因是struts2的拦截器在作怪，尝试把to标签添加type=redirect，运行，可以跳转，但是由于是重定向，地址栏的地址又变成struts2的样子。

但是不加redirect还是报404，

分析，可能是struts2转向机制的问题，

查阅资料，说是要在配struts2拦截器的时候需要加：

&lt;dispatcher&gt;REQUEST &lt;/dispatcher&gt;    
&lt;dispatcher&gt;FORWARD &lt;/dispatcher&gt;    
&lt;dispatcher&gt;INCLUDE &lt;/dispatcher&gt;

这三个节点。

但是个人感觉只要可以分发FORWARD就应该可以。

所以修改代码如下：

&lt;filter-mapping&gt;  
   &lt;filter-name&gt;struts2&lt;/filter-name&gt;  
   &lt;url-pattern&gt;/\*&lt;/url-pattern&gt;  
   &lt;dispatcher&gt;FORWARD&lt;/dispatcher&gt;    
&lt;/filter-mapping&gt;

运行，正常，还有待大量测试。

发布项目，在浏览器地址栏敲：[http://localhost:8080/showArticle/showList.html](http://localhost:8080/showArticle/showList.html) 就会被urlrewriter定位到[http://localhost:8080/showArticle/showListAction.action](http://localhost:8080/showArticle/showListAction.action)  路径下了。

[http://localhost:8080/showArticle/showArticle\_1.html](http://localhost:8080/showArticle/showArticle_1.html)  就会被urlrewriter定位到[http://localhost:8080/showArticle/showArticleAction.action?article.id=1](http://localhost:8080/showArticle/showArticleAction.action?article.id=1) 路径下

总结：

1、配置web.xml的时候，rewrite一定要在strtus2的上面。

2、strtus2的filter-mapping一定要添加：&lt;dispatcher&gt;FORWARD&lt;/dispatcher&gt;