Struts2中应用UrlRewrite无反应的解决方案
???? 首先声明一下,这可能只是对于我这个项目中问题的解决,这里之所以写出来,一是觉得可能别人也会遇到我这个问题;二是强调一种解决问题的方法。
???? 在Struts2中用urlrewrite,结果一点作用都没起,web.xml中的配置和urlrewrite.xml的配置以及位置自认为都是正确的,而且启动的时候并没有报什么错误,先贴一下我的配置:
web.xml中的配置:
<?xml version="1.0" encoding="UTF-8"?><web-app id="WebApp_9" version="2.4"xmlns="http://java.sun.com/xml/ns/j2ee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"><display-name>Struts Blank</display-name><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><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><welcome-file-list><welcome-file>index.jsp</welcome-file></welcome-file-list></web-app>
?这和网上介绍的方法一样,没什么特别的。
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"><!--Configuration file for UrlRewriteFilterhttp://tuckey.org/urlrewrite/--><urlrewrite><rule><note>The rule means that requests to /test/status/ will beredirected to /rewrite-status the url will be rewritten.</note><from>/test/status/</from><to type="redirect">%{context-path}/rewrite-status</to></rule><rule><from>/login.html</from><to type="redirect">%{context-path}/login.action</to></rule><outbound-rule><note>The outbound-rule specifies that when response.encodeURL iscalled (if you are using JSTL c:url) the url /rewrite-statuswill be rewritten to /test/status/.The above rule and this outbound-rule means that end usersshould never see the url /rewrite-status only /test/status/both in thier location bar and in hyperlinks in your pages.</note></outbound-rule><!--INSTALLATIONin your web.xml add...<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>EXAMPLESRedirect one url<rule><from>/some/old/page.html</from><to type="redirect">/very/new/page.html</to></rule>Redirect a directory<rule><from>/some/olddir/(.*)</from><to type="redirect">/very/newdir/$1</to></rule>Clean a url<rule><from>/products/([0-9]+)</from><to>/products/index.jsp?product_id=$1</to></rule>eg, /products/1234 will be passed on to /products/index.jsp?product_id=1234 without the user noticing.Browser detection<rule><condition name="user-agent">Mozilla/[1-4]</condition><from>/some/page.html</from><to>/some/page-for-old-browsers.html</to></rule>eg, will pass the request for /some/page.html on to /some/page-for-old-browsers.html only for olderbrowsers whose user agent srtings match Mozilla/1, Mozilla/2, Mozilla/3 or Mozilla/4.--></urlrewrite>
这不知道是从哪摘取的,好像是一个文档中复制过来的。
?
web.xml和urlrewrite.xml也都放在同一文件夹下了,接着我页面中有一个url:
<a href="login.html">登录</a>
按理说应该是可以了的,但是不起作用。
上网上去查,也有很多人在Struts2中用urlrewrite不成功了,也有很多的解决方法。照着他们的解决方案试了很多,不成功,一点作用都没起。从404报错说找不到login.html文件,这时候就怀疑是否是没读到这个配置文件,但是自己又反驳了,启动项目的时候myeclipse的控制台中没有报错啊(控制台报错的错误是红色的,我这没红色,我就认为没有什么错误了,这是后话了)。接着去群里面问了,有一个哥们问了一下:”控制台报错了吗?“,我当然回答是没报错了。又弄了很久还是没找出原因,最后不知道是哪根痉对了跑去看了一下控制台输出,结果看到了这样的一句话:
org.tuckey.web.filters.urlrewrite.UrlRewriteFilter ERROR: unloading existing conf
?配置文件竟然没有载入,为什么呢?配置文件的位置是正确的啊,为什么会没被载入呢!于是把这句话弄到Google中搜了一下,国外一论坛的哥们也有这样的错误,大致看了一下其中一句话惊醒了我”是不是配置文件中表达式之类的不对了呢?看看控制台上面这句话还报什么错了“。对啊,看看这上面报什么错误,去看发现:
org.tuckey.web.filters.urlrewrite.OutboundRule ERROR: Outbound Rule Outbound Rule 0 had error: to is not valid because it is blank
?于是乎,把上面urlrewrite.xml中的
<outbound-rule><note>The outbound-rule specifies that when response.encodeURL iscalled (if you are using JSTL c:url) the url /rewrite-statuswill be rewritten to /test/status/.The above rule and this outbound-rule means that end usersshould never see the url /rewrite-status only /test/status/both in thier location bar and in hyperlinks in your pages.</note></outbound-rule>
?注释了,再运行竟然行了。
?
?
?? 当然了,这只是我项目中的一个问题,不过确教会了我一点:以前只有控制台报红色的错误我才去看到底是哪里错了,没报红色错误,启动的时候我就很少看控制台输出地是什么内容。这里我只想说的是,如果实在是找不错问题的时候,又确定自己没做错,这时候认真看看控制台,或许会找到问题的所在。
?
?
?
2 楼 sdh5724 2011-02-17 其实, 不用这么麻烦的, 这个filter代码实现很简单的, 看不能工作, 使用debug选项挂起容器,就能debug filter的初始化过程了。