JSP页面伪静态化

8 篇文章 0 订阅

1.导包
urlrewritefilter-4.0.3.jar
jstl.jar
standard.jar
2.在WEB-INF/web.xml 增加urlrewritefilter过滤器

    <!-- 加到任何servlet映射的顶部,不然可能有些路径不能被过滤到
        参考:http://urlrewritefilter.googlecode.com/svn/trunk/src/doc/manual/3.2/index.html
    -->
   <filter>
       <filter-name>UrlRewriteFilter</filter-name>
       <filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class>
       <!-- 
           设备文件重加载间隔 (0默示随时加载, -1默示不重加载, 默认-1) 
       -->
       <init-param>
           <param-name>confReloadCheckInterval</param-name>
           <param-value>60</param-value>
       </init-param>

       <!-- 自定义配置文件的路径,是相对context的路径,(默认位置 /WEB-INF/urlrewrite.xml) -->
       <init-param>
           <param-name>confPath</param-name>
           <param-value>/WEB-INF/urlrewrite.xml</param-value>
       </init-param>

       <!-- 
           设置日志级别(将被记录到日志中)
              可以为: TRACE, DEBUG, INFO (default), WARN, ERROR, FATAL, log4j, commons, slf4j,
              比如 sysout:DEBUG(设置到控制台调试输出级别) 
           (默认级别 WARN) -->
       <init-param>
           <param-name>logLevel</param-name>
           <param-value>DEBUG</param-value>
       </init-param>
   </filter>
   <filter-mapping>
       <filter-name>UrlWriterFilter</filter-name>
       <url-pattern>/*</url-pattern>
   </filter-mapping>

3.配置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">

<urlrewrite>
    <rule>
        <from>/page/(.*).html</from>  
        <to>/index.jsp?page=$1</to> 
    </rule>
    <rule>
        <from>^/user/([a-z]+)/([0-9]+)$</from>
        <to>/index.jsp?nickname=$1&amp;age=$2</to>
  </rule>
</urlrewrite>

rule是url重写规则:
from是显示出来的地址,to是映射的实际地址, 1from() 为指定结束
对于中文参数要使用(.*)作为参数转义。

4.案例

示例1:
Rule规则

<rule>
        <from>/page/(.*).html</from>  
        <to>/index.jsp?currentPage=$1</to> 
</rule>

执行效果如下:
请求网址
http://localhost:8080/manager/page/5.html
对应网址:
http://localhost:8080/manager/index.jsp?currentPage=5

示例2:
Rule规则

<rule>
        <name>World Rule</name>
        <from>^/user/([a-z]+)/([0-9]+)$</from>
        <to>/index.jsp?nickname=$1&amp;age=$2</to>
</rule>

执行效果如下:
http://localhost:8080/manager/user/jack/23
对应:
http://localhost:8080/manager/index.jsp?nickname=jack&age=23

示例3

同理rule规则如下时

<rule>
        <from>^/page/(.*)$</from>
        <to type="redirect">/page/$1.action</to>
</rule>

执行效果如下:
http://localhost:8080/manager/page/test
对应:
http://localhost:8080/manager/page/test.action

示例4
Rule规则

<rule>
    <from>^/([a-z]+)/([a-z]+)/([a-z]+)$</from>
    <to>/$1.do?method=$2&amp;uuid=$3</to>
</rule>

在index.jsp中添加如下链接:

<a href="process/show/index">跳转</a>

执行效果如下:
http://localhost:8080/manager/process/show/index,
对应:
http://localhost:8080/manager/process.do?method=show&uuid=index

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值