URL重写

一,下载一个urlrewritefilter-4.0.3.jar ,拷贝到 WEB-INF/lib下面

 

二,修改web.xml,加入以下过滤器

<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>/*</url-pattern>

    <dispatcher>REQUEST</dispatcher>

    <dispatcher>FORWARD</dispatcher>

</filter-mapping>

 

三,在WEB-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>
			<![CDATA[
			通用格式(无参数)
			/namespace/method.html
			转为
			/namespace/method.action
			]]>
		</note>
		<from>^/([a-zA-Z0-9\.]+)/(\w+).html$</from>
		<to>/$1/$2.action</to>
	</rule>
	
	<rule>
		<note>
		    <![CDATA[
			通用格式(有一个参数)
			/namespace/methodName/paramName-paramValue.html
			转为
			/namespace/methodName.action?paramName=paramValue
		   ]]>
		</note>
		<from>^/([a-zA-Z0-9]+)/(\w+)/([a-zA-Z0-9\.]+)-([\w\,\.\|]+).html$</from>
		<to>/$1/$2.action?$3=$4</to>
	</rule>
	
	<rule>
		<note>
			<![CDATA[
			通用格式(有两个个参数)
			/namespace/methodName/paramName-paramValue/paramName2-paramValue2.html
			转为
			/namespace/methodName.action?paramName=paramValue¶mName2=paramValue2
			]]>
		</note>
		<from>^/([a-zA-Z0-9]+)/(\w+)/([a-zA-Z0-9\.]+)-([\w\,\.\|]+)/([a-zA-Z0-9\.]+)-([\w\,\.\|]+).html$</from>
		<to>/$1/$2.action?$3=$4&$5=$6</to>
	</rule>
	
	<rule>
		<note>
			<![CDATA[
			通用格式(有三个个参数)
			/namespace/methodName/paramName-paramValue/paramName2-paramValue2.html
			转为
			/namespace/methodName.action?paramName=paramValue¶mName2=paramValue2¶mName3=paramValue3
			]]>
		</note>
		<from>^/([a-zA-Z0-9]+)/(\w+)/([a-zA-Z0-9\.]+)-([\w\,\.\|]+)/([a-zA-Z0-9\.]+)-([\w\,\.\|]+)/([a-zA-Z0-9\.]+)-([\w\,\.\|]+).html$</from>
		<to>/$1/$2.action?$3=$4&$5=$6&$7=$8</to>
	</rule>
	
	<rule>
		<note>
			<![CDATA[
			通用格式(有四个个参数)
			]]>
		</note>
		<from>^/([a-zA-Z0-9]+)/(\w+)/([a-zA-Z0-9\.]+)-([\w\,\.\|]+)/([a-zA-Z0-9\.]+)-([\w\,\.\|]+)/([a-zA-Z0-9\.]+)-([\w\,\.\|]+)/([a-zA-Z0-9\.]+)-([\w\,\.\|]+).html$</from>
		<to>/$1/$2.action?$3=$4&$5=$6&$7=$8&$8=$9</to>
	</rule>
	
	<outbound-rule>
		<from>(\w+).action$</from>
		<to>$1.html</to>
	</outbound-rule>
	
	<outbound-rule>
		<from>(\w+).action\?([a-zA-Z0-9]+)\=([\w\,\.\|]+)$</from>
		<to>$1/$2-$3.html</to>
	</outbound-rule>
	
	<outbound-rule>
		<from>(\w+).action\?([a-zA-Z0-9]+)\=([\w\,\.\|]+)&([a-zA-Z0-9]+)\=([\w\,\.\|]+)$</from>
		<to>$1/$2-$3/$4-$5.html</to>
	</outbound-rule>
	
	<outbound-rule>
		<from>(\w+).action\?([a-zA-Z0-9]+)\=([\w\,\.\|]+)&([a-zA-Z0-9]+)\=([\w\,\.\|]+)&([a-zA-Z0-9]+)\=([\w\,\.\|]+)$</from>
		<to>$1/$2-$3/$4-$5/$6-$7.html</to>
	</outbound-rule>
	
	<outbound-rule>
		<from>(\w+).action\?([a-zA-Z0-9]+)\=([\w\,\.\|]+)&([a-zA-Z0-9]+)\=([\w\,\.\|]+)&([a-zA-Z0-9]+)\=([\w\,\.\|]+)&([a-zA-Z0-9]+)\=([\w\,\.\|]+)$</from>
		<to>$1/$2-$3/$4-$5/$6-$7/$8-$9.html</to>
	</outbound-rule> 
</urlrewrite>


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值