在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 urlrewritefilter http://tuckey.org/urlrewrite/ --><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> <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 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> </outbound-rule> <!-- installation in 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> examples redirect 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 older browsers 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 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></outbound-rule>注释了,再运行竟然行了。
当然了,这只是我项目中的一个问题,不过确教会了我一点:以前只有控制台<span style="color: #ff0000;">报红色</span>的错误我才去看到底是哪里错了,没报红色错误,启动的时候我就很少看控制台输出地是什么内容。这里我只想说的是,如果实在是找不错问题的时候,又确定自己没做错,这时候认真看看控制台,或许会找到问题的所在。