后台接收的请求,希望把请求URL做重定向,改变原有的请求,此篇可以帮助你解决这个问题,可以用来改变常规URL重定向,也可以隐藏所访问的静态资源
参考地址http://blog.jdriven.com/2016/02/urlrewritefilter-load-configuration-with-spring-resourceloader/
代码实现部分实现这个功能需要以下几个步骤
- pom中引用jar
- 增加一个配制类
- resource文件夹下增加一个配置文件
以下对上述步骤展开具体说明
pom中引用jar
org.tuckey
urlrewritefilter
4.0.4
增加一个配制类
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);
}
}
}
增加需要重定向的配置文件
测试/test/ / ^/test/list/(w+)/(w+).html /test/list/$1/$2/ ^/test11/(w+).html$ /realTime/$1/ ^/static/(w+).html$ /static/$1/
上述完成后就可以了,so easy,可以自己测试一下了,这里就不截图了,亲试过,好用



