注册 X
提交 注:点击提交后系统会发送邮件到邮箱验证!(仅支持中国大陆邮箱)
我已阅读并同意 服务条款
首页 > IT技术笔记 > 查看笔记

springboot使用urlrewrite重写url地址栏

1、增加依赖

<!-- urlrewrite依赖 -->

<dependency>  

<groupId>org.tuckey</groupId>

<artifactId>urlrewritefilter</artifactId>

<version>4.0.3</version>

</dependency>


2、在springboot启动类同级目录增加UrlRewriteFilterConfig类

package com.rshare;


import java.io.File;

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

  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(),

          "@@traceability@@");

      checkConf(conf);

    } catch (IOException ex) {

      throw new ServletException("Unable to load URL rewrite configuration file from " + URL_REWRITE, ex);

    }

  }

  

}


3、在resource目录增加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">
<urlrewrite> 
	<rule>
  		<from >/recordshare/index.html</from>
    	<to type="forward">/indexController/toList.action?pages=1</to>
	</rule>
	<rule>
  		<from >^/index.html</from>
    	<to type="forward">/indexController/toList.action?pages=1</to>
	</rule>	
	<rule>
  		<from >^/index/(\w+)/([^_\/\.]+)/(\d+)\.html$</from>
    	<to type="forward">/indexController/toList.action?notetype=$1&keyword=$2&pages=$3</to>
	</rule>
	
</urlrewrite>			
	      

 打赏        分享



评论

邮箱: 昵称:
\n