看文章之前,先要理解过程是怎么样的。
LZ 起初以为是先访问 Action,然后url 会自己变成 .html 的形式,然而我错了!
其实是先访问 .html,然后触发某个Action
业务逻辑我就不给了,会写 .action?id=1? 这种的,才会想去url 重写!
web.xml
?
<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> <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>
?
?
?
//这里切记 拦截器一定要放在struts 的前面!
urlRewriter.xml
?? //这里的意思是访问 news-1.html 就会跳转访问 ReadNews这个action,并且参数为1 ??? $1对应的就是([0-9]+) ? ?
? ?<rule>
<from>news-([0-9]+).html</from>
<to type="forward">/ReadNews?id=$1</to>
</rule>
?
?
?