欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

JSP请求地址重写

程序员文章站 2022-04-23 19:14:17
...
请求地址重写好处不在赘述。现在有开源框架支持,非常方便:urlrewrite,http://tuckey.org/urlrewrite/。具体实现也非常简单,首先下载他的jar包,放到lib里,并添加library。首先在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>*.aspx</url-pattern>
    <dispatcher>REQUEST</dispatcher>
    <dispatcher>FORWARD</dispatcher>
</filter-mapping>

*.aspx就是你要拦截的请求地址结尾。
然后再WEN-INF下新建urlrewrite.xml,内容为:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE urlrewrite PUBLIC "-//tuckey.org//DTD UrlRewrite 4.0//EN"
        "http://www.tuckey.org/res/dtds/urlrewrite4.0.dtd">

<!--

    Configuration file for UrlRewriteFilter
    http://www.tuckey.org/urlrewrite/

-->
<urlrewrite>

    <rule>
        <note>
                注释文本,映射规则说明
        </note>
        <from>^/(\w+)/(\w+)/(\w+)\.aspx$</from>
        <to type="forward">$2Action!$3.aspx</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>
        <from>/rewrite-status</from>
        <to>/test/status/</to>
    </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.

    Centralised browser detection
        <rule>
            <condition name="user-agent">Mozilla/[1-4]</condition>
            <set type="request" name="browser">moz</set>
        </rule>
    eg, all requests will be checked against the condition and if matched
    request.setAttribute("browser", "moz") will be called.

    -->

</urlrewrite>

之后改web-inf里struts的拦截器,一定类似底下的:
 <!-- STRUTS2配置 -->
    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>*.aspx</url-pattern>
         <dispatcher>REQUEST</dispatcher>
         <dispatcher>FORWARD</dispatcher>
         <dispatcher>INCLUDE</dispatcher>
    </filter-mapping>

*.aspx可以换成你映射后的真实路径的结尾,struts配置文件中也要有相应的配置:
<constant name="struts.action.extension" value="aspx"></constant>

最关心的应该是映射规则,那里from节点是请求过来的地址,to节点是映射的真实地址,type是表示转发还是重定向,转发,struts拦截后可以获取参数,重定向则获取不到。2个节点可以是真实的地址,也可以用正则表达式匹配,最好是正则表达式,要不然写死人。