UrlRewrite地址重写

动态的URL变成静态的URL可以UrlRewriteFilter来处理


UrlRewriteFilter的介绍:
    UrlRewriteFilter是一个用于改写URL的Web过滤器,类似于Apache的mod_rewrite。适用于任何Web应用服务器(如 Resin,Orion,Tomcat等)。其典型应用就把动态URL静态化,便于搜索引擎爬虫抓取你的动态网页。
   为什么要使动态的URL变成伪静态的URL:
   1:为了对搜索的友好,因为有些搜索不能抓取动态页面或是对动态抓取的页面没有静态页面高.
   2:屏蔽内部的url结构.
   3:美化url.
   UrlRewriteFilter使用:
     1.下载http://tuckey.org/urlrewrite/#download目前稳定的版本是2.6,最新版3.1,推荐使用2.6版.解压缩后将文件考到相应的web-inf/lib和web-inf下.
     2、配置web.xml

    <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>
        <dispatcher>REQUEST</dispatcher>
        <dispatcher>FORWARD</dispatcher>
    </filter-mapping>



     根据自己的需要,将相应目录下的url转给UrlRewriteFilter来处理。
   3、配置urlwrite规则文件WEB-INF/urlrewrite.xml 

    

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE urlrewrite PUBLIC "-//tuckey.org//DTD UrlRewrite 2.6//EN"    
    "http://tuckey.org/res/dtds/urlrewrite2.6.dtd">
<urlrewrite>
	<rule>
		<from>^/([a-z]+)/([a-z]+)/([0-9]+).htm$</from>
		<to>/$1/$2/index.jsp?currPage=$3</to>
	</rule>
	<rule>
		<from>^/([a-z]+)/([a-z]+)/([a-z]+)/([0-9]+).htm$</from>
		<to>/$1/$2/$3/index.jsp?currPage=$4</to>
	</rule>
	<rule>
		<from>^/register.htm</from>
		<to>/register.jsp</to>
	</rule>
	<rule>
		<from>^/signin.htm</from>
		<to>/signin.jsp</to>
	</rule>
	<rule>
		<from>^/search/searchExpo.html</from>
		<to>/search/searchExpo.action</to>
	</rule>
</urlrewrite>

说明:

http://localhost:8080/test/news/news/2.htm 会直接映射到 http://localhost:8080/test/news/news/index.jsp?currPage=2 这个链接

http://localhost:8080/test/news/news/aa/2.htm 会直接映射到 http://localhost:8080/test/news/news/aa/index.jsp?currPage=2 这个链接

 jsp代码:

<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
	String url = request.getRequestURL().toString();
	request.setAttribute("url", url);
	System.out.println(request.getParameter("currPage")+"=============================>>>>>>>>>>>>>>>>>>>>>>>>"+url);
	
%>

在浏览器中输入:http://localhost:8080/test/news/news/2.htm 发现url: http://localhost:8080/test/news/news/index.jsp 且currPage=2,然后在jsp中根据相应的参数做下一步处理;

一般是在jsp页面自定义标签,然后在自定义标签中根据参数处理:

如,可以在自定义中标签中;

public class TestTag extends TagSupport {

	private static final long serialVersionUID = 1L;
	protected int currPage = 1;// 当前页数,默认为1, currPage 接收到的值就为url重写后的值
	private int pageSize;// 每页数据数量

	@Override
	public int doStartTag() throws JspException {
          .....
        }
 }


一般可以用这种方法实现网页的伪静态;



.

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值