首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > JavaScript >

分析ulrwriter重写action跟jsp页面地址的用法

2012-09-14 
分析ulrwriter重写action和jsp页面地址的用法1.下载jar包http://www.tuckey.org/urlrewrite/2.配置web.xml

分析ulrwriter重写action和jsp页面地址的用法
1.下载jar包http://www.tuckey.org/urlrewrite/

2.配置web.xml的filter

<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要放在struts的filter之前

3.重点来了,配置urlrewrite.xml,写上重写规则
<?xml version="1.0" encoding="utf-8"?><!DOCTYPE urlrewrite PUBLIC "-//tuckey.org//DTD UrlRewrite 3.2//EN"        "http://tuckey.org/res/dtds/urlrewrite3.2.dtd"><!--    Configuration file for UrlRewriteFilter    http://tuckey.org/urlrewrite/--><urlrewrite>    <!--<rule>-->        <!--<note>-->            <!--action-->        <!--</note>-->        <!--<from>^/([_a-zA-Z]+[_0-9a-zA-Z-/]*[_0-9a-zA-Z]+)$</from>-->        <!--<to type="redirect">/$1.action</to>-->    <!--</rule>-->    <rule>        <note>            jsp        </note>        <from>^/([_a-zA-Z]+[_0-9a-zA-Z-/]*[_0-9a-zA-Z]+).html$</from>        <to type="forward">/$1.jsp</to>    </rule>    <outbound-rule>        <note>            jsp        </note>        <from>^/([_a-zA-Z]+[_0-9a-zA-Z-/]*[_0-9a-zA-Z]+).jsp$</from>        <to type="forward">/$1.html</to>    </outbound-rule>    <outbound-rule>        <note>            action        </note>        <from>^/([_a-zA-Z]+[_0-9a-zA-Z-/]*[_0-9a-zA-Z]+).action$</from>        <to>/$1</to>    </outbound-rule></urlrewrite>


重写规则分析:
1.第一步,<outbound-rule>,此标签内rule 用于替换页面和浏览器显示的url,<from>表示页面原来填写的url,<to>表示重写后显示给用户看的地址,但是,这个替换规则只对
<a href="<%=request.getContextPath() + response.encodeURL("/jsp/home.jsp")%>">URLRewriter 修正页面URL</a><a href="<c:url value="/jsp/home.html" />">URLRewriter 修正页面URL</a>

这两种在href内加了标签的写法有效,例如此处的/jsp/home.jsp在浏览器中的地址显示的是/jsp/home.html

2.第二步,<rule>,此标签内的rule用来解析页面和浏览器显示的url,在第一部中,点击那个a标签,会出现404错误,因为真实目录中并不存在home.html这用一个文件.但是写了rule规则的解析以后,会将请求url从home.html解析成home.jsp..这样便可以在浏览器上显示home.html但实际请求home.jsp资源,重写就成功了

有意思的地方是,struts2对于action请求,在浏览器上的请求地址可以不加上.action后缀,这就正好提供了一个方便的地方.我们可以只在<outbound-rule>指定匹配规则将.action后缀去掉,而不再<rule>里指定去掉.action后缀后的解析规则,因为即使请求写着/home就相当于/home.action

热点排行