您的位置:首页 > Web前端 > JavaScript

JSP页面伪静态化

2017-08-14 16:08 169 查看
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&age=$2</to>
  </rule>
</urlrewrite>


rule是url重写规则:

from是显示出来的地址,to是映射的实际地址,1是重写参数,它的值与from中的正则表达式是一一对应,可以为多个;()里是匹配的正则表达式,在正则表达式指定字符的串开始,为指定结束

对于中文参数要使用(.*)作为参数转义。

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&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&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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: