当前位置:文档之家› 伪静态_URLRewrite实例解析

伪静态_URLRewrite实例解析

Url Rewrite Filter实现页面伪静态化,简单实例及步骤1.首先新建一个Web Project本例把它命名为:UrlRewrite,在创建的时候,选择勾选“Add JSTL libraries to WEB-INF/lib folder ”(注:因为项目中会用到jstl 核心标签库),然后再把urlrewrite-2.6.0.jar 导入到项目Bulid Path路径下,最后再加入struts框架。

2.配置web.xml文件把如下代码加入到web.xml配置文件中,更多的配置可参考官方文档:<!-- UrlRewriteFilter 过滤器配置 --><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>3.新增urlrewrite.xml文件<?xml version="1.0" encoding="utf-8"?><!DOCTYPE urlrewrite PUBLIC "-////DTD UrlRewrite2.6//EN" "/res/dtds/urlrewrite2.6.dtd"><urlrewrite><rule><from>^/([0-9]+).html$</from><to type= "forward">/index.jsp?id=$1</to></rule></urlrewrite>在WEB-INF目录下新建一个Url Rewrite Filter的规则配置文件,默认命名为urlrewrite.xml,示例内容如下:注:rule是url重写规则,from是显示出来的地址,to是映射的实际地址,$1是重写参数,它的值与from中的正则表达式是一一对应,可以为多个,()里是匹配的正则表达式,在正则表达式^指定字符的串开始,$为指定结束4.新建一个简单的action处理类在此命名为:ProcessAction.java;示例代码如下:package com.telin.struts.action;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.apache.struts.action.ActionForm;import org.apache.struts.action.ActionForward;import org.apache.struts.action.ActionMapping;import org.apache.struts.actions.DispatchAction;public class ProcessAction extends DispatchAction {/***Method execute**@param mapping*@param form*@param request*@param response*@return ActionForward*/public ActionForward show(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) { return mapping.findForward("ok");}}5.在struts-config.xml中添加如下內容:<action-mappings><action parameter="method" path="/process"type="com.telin.struts.action.ProcessAction"><forward name="ok" path="/show.html"></forward></action></action-mappings>6..新增index.jsp文件,添加如下内容:Hello word ! <%=request.getParameter("id")%><br />7.新增show.html文件,添加如下内容:This is OK page.8.所有文件都操作处理完后,发布并启动项目下面做一下具体讲解讲解1:正常情况下:在浏览器的地址栏中输入:http://localhost:8080/UrlRewrite/页面内容如下:Hello word ! null(因为此时id并没有值,所以显示为null)Urlrewrite之后:在浏览器的地址栏中输入:http://localhost:8080/UrlRewrite/maomao.html页面内容如下:Hello word ! maomao解析:在项目中,maomao.html实际上并不存在,该请求由Url Rewrite Filter负责拦截,由于它符合我在urlrewrite.xml中配置的拦截规则:<rule><from>^/([0-9]+).html$</from><to type= "forward">/index.jsp?id=$1</to></rule>所以该请求被转发由index.jsp负责处理,即实际处理请求的是index.jsp,但是地址栏中显示地址依旧是maomao.html.即伪静态的显示方式,并把参数id赋值”maomao”,所以页面中id此时不为null。

讲解2:正常情况下:在浏览器的地址栏输入:http://localhost:8080/UrlRewrite/process.do?method=show&uuid=index 页面内容如下:This is OK page.我们怎么把这个url实现伪静态化呢?实现方式1:在urlrewrite.xml中定义如下规则:<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">跳转-1</a>当点击该链接,地址栏中显示url是:http://localhost:8080/UrlRewrite/process/show/index注:这种方式在很多论坛中经常使用。

实现方式2:在urlrewrite.xml中定义如下规则:<rule><from>^/([a-z]+)/([a-z]+)/([a-z]+).html$</from><to>/$1.do?method=$2&amp;uuid=$3</to></rule>在index.jsp中添加如下链接:<a href="process/show/index.html">跳转-2</a>当点击该链接,地址栏中显示url是:http://localhost:8080/UrlRewrite/process/show/index.html注:这只是伪静态的两种url展现方式。

讲解3:在urlrewrite.xml配置文件中有两种规则:即<rule>和<outbound-rule>,在上面的例子中,讲解的都是<rule>规则,下面我们来讲解一下<outbound-rule>规则:<outbound-rule>这是非常类似的一个正常的规则,但它是用于重写的URL ,通过response.encodeURL() 或者<c:url value=””/>标签来实现下面我们在index.jsp中加入如下三段代码:<%@ taglib uri="/jstl/core-rt" prefix="c"%><a href="<%=response.encodeURL("process.do?method=show&uuid=index")%>">跳转-3</a><a href="<c:url value='process.do?method=show&uuid=index'/>">跳转-4</a>在urlrewrite.xml中加入如下<outbound-rule>规则:<outbound-rule><from>process.do\?method=([a-z]+)&amp;uuid=([a-z]+)</from><to>./$1.html</to></outbound-rule>正常情况下:在浏览器的地址栏中输入:http://localhost:8080/UrlRewrite/我们把鼠标指向链接”跳转-3”或者”跳转-4”上的时候,状态栏的地址显示为:http://localhost:8080/UrlRewrite/show.html当我们点击链接”跳转-3”或者”跳转-4”上的时候。

相关主题