springboot伪静态

在日常网站访问中,会把动态地址改造成伪静态地址。

例如: 访问新闻栏目 /col/1/,这是原有地址,如果这样访问,不利于搜索引擎检索收录,同时安全性也不是很好。

改造之后:

/col/1.html。

改造方法:

1.添加urlrewritefilter

<dependency>
    <groupId>org.tuckey</groupId>
    <artifactId>urlrewritefilter</artifactId>
    <version>4.0.4</version>
</dependency>

2.配置bean

复制代码
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);
    }
  }
}
复制代码

参考网址:http://blog.jdriven.com/2016/02/urlrewritefilter-load-configuration-with-spring-resourceloader/

3.配置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>^/col/(\w+)\.html$</from>
        <to>/col/$1/</to>
    </rule>

    <!-- 栏目列表页,注意html后面没有加$,因为后面还有若干参数 -->
    <rule>
        <from>^/col/list/(\w+)/(\w+)\.html</from>
        <to>/col/list/$1/$2/</to>
    </rule>

    <!-- 文章详情页 -->
    <rule>
        <from>^/art/(\w+)\.html$</from>
        <to>/art/$1/</to>
    </rule>
    
    <!-- 静态网页 -->
    <rule>
        <from>^/static/(\w+)\.html$</from>
        <to>/static/$1/</to>
    </rule>

</urlrewrite>
复制代码

image

配置说明请参考:http://blog.163.com/zhangmihuo_2007/blog/static/27011075201351433716225/

至此配置完毕,启动测试,注意看红框处,说明加载了urlwrite。

image

访问页面如下,成功了!

image

 

 
 

转载于:https://www.cnblogs.com/tinyj/p/9799044.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值