赞
踩
后台接收的请求,希望把请求URL做重定向,改变原有的请求,此篇可以帮助你解决这个问题,可以用来改变常规URL重定向,也可以隐藏所访问的静态资源
http://blog.jdriven.com/2016/02/urlrewritefilter-load-configuration-with-spring-resourceloader/
实现这个功能需要以下几个步骤
以下对上述步骤展开具体说明
<!--地址重定向用-->
<dependency>
<groupId>org.tuckey</groupId>
<artifactId>urlrewritefilter</artifactId>
<version>4.0.4</version>
</dependency>
import java.io.IOException; import javax.servlet.FilterConfig; import javax.servlet.ServletException; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Configuration; import org.springframework.core.io.Resource; import org.tuckey.web.filters.urlrewrite.Conf; import org.tuckey.web.filters.urlrewrite.UrlRewriteFilter; @Configuration public class UrlRewriteFilterConfig extends UrlRewriteFilter { private static final String URL_REWRITE = "classpath:/urlrewrite.xml"; // Inject the Resource from the given location @Value(URL_REWRITE) private Resource resource; // Override the loadUrlRewriter method, and write your own implementation @Override protected void loadUrlRewriter(FilterConfig filterConfig) throws ServletException { try { // Create a UrlRewrite Conf object with the injected resource Conf conf = new Conf(filterConfig.getServletContext(), resource.getInputStream(), resource.getFilename(), "@@systemID@@"); //最后的参数是自己系统的标识ID即可 checkConf(conf); } catch (IOException ex) { throw new ServletException("Unable to load URL rewrite configuration file from " + URL_REWRITE, ex); } } }
<?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>/test/</from> <to>/</to> </rule> <!-- 栏目列表页,注意html后面没有加$,因为后面还有若干参数 --> <rule> <from>^/test/list/(\w+)/(\w+)\.html</from> <to>/test/list/$1/$2/</to> </rule> <!-- 文章详情页 --> <rule> <from>^/test11/(\w+)\.html$</from> <to>/realTime/$1/</to> </rule> <!-- 静态网页 --> <rule> <from>^/static/(\w+)\.html$</from> <to>/static/$1/</to> </rule> </urlrewrite>
上述完成后就可以了,so easy,可以自己测试一下了,这里就不截图了,亲试过,好用
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。