1.
下载urlrewrite的jar包,这里使用的是urlrewrite-2.6.0.jar包,将jar包放到/WEB-INF/的
lib目录下,如果测试什么的没成功,首先是检查jar包是否在工程下或部署到服务器上了,好像
其他的应用都应该先检查这个
2.
配置web.xml文件
<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>
<init-param>
<param-name>confPath</param-name>
<param-value>/WEB-INF/urlrewrite.xml</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>UrlRewriteFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
在<filter>中有很多的参数可以配置,具体的可以google
3.
配置urlrewrite.xml文件
<urlrewrite>
<rule>
<from>^/([_a-zA-Z]+[_0-9a-zA-Z-/]*[_0-9a-zA-Z]+)/([_a-zA-Z]+[_0-9a-zA-Z-/]*[_0-9a-zA-Z]+)/?$</from>
<to type="forward">/$1.do?method=$2</to>
</rule>
<rule>
<from>^/([_a-zA-Z]+[_0-9a-zA-Z-/]*[_0-9a-zA-Z]+).html$</from>
<to type="redirect">/MyExample/$1.jsp</to>
</rule>
</urlrewrite>
这里前一个rule是将如http://localhost:8080/MyExample/example/toExample.do?method=example的
可以用http://localhost:8080/MyExample/example/toExample/example直接访问
后一个是简单的将/example/aa.html转向为/MyExample/example/aa.jsp
4.
使用urlrewrite
以上的都已经配置好后就可以使用urlrewrite了,因为用struts比较多,所以*.do的情况就比较多
看着不爽,所以用了个这个。好了现在就可以使用这个了,
比如你有很多的*.do要用
http...:8080/MyExample/example/toExample.do?method=example1
http...:8080/MyExample/example/toExample.do?method=example2
http...:8080/MyExample/example/toExample.do?method=example3
http...:8080/MyExample/aaa/toAa.do?method=aa
http...:8080/MyExample/bbb/toBb.do?method=bb
http...:8080/MyExample/ccc/c1.jsp
http...:8080/MyExample/ddd/d1.jsp
就可以在页面或action中使用
http...:8080/MyExample/example/toExample/example1/
http...:8080/MyExample/example/toExample/example2/
http...:8080/MyExample/example/toExample/example3/
http...:8080/MyExample/aaa/toAa/aa/
http...:8080/MyExample/bbb/toBb/bb/
http...:8080/MyExample/ccc/c1.html
http...:8080/MyExample/ddd/d1.html
这样的连接了
5.
注意,使用urlrewrite只是将链接改成看上去很简单的样子,实际上还是原来的链接在用,如果
想真正的动态页面静态化不是这么简单的,好像要用。。。忘了
还有要说明的是,这个url重写我并没有深入,而在使用的时候有各种问题,现在还没有遇到,所以
要具体问题具体解决。