【问题标题】:Can't match /*/xxx in Spring ResourceHandlerRegistry?无法匹配 Spring ResourceHandlerRegistry 中的 /*/xxx?
【发布时间】:2015-08-13 21:15:04
【问题描述】:

我正在尝试将以下 URL 解析为我服务器上的相同资源:

http://myhost/website/content/css/theme.css
http://myhost/subsite/app/website/content/css/theme.css

一般来说:

http://myhost/{any non-/ string}/app/website/content/css/theme.css

以下配置成功解析:

http://myhost/website/content/css/theme.css

但顽固地拒绝解析任何 */app 网址:

@Configuration
@EnableWebMvc
class WebMVCConfig extends WebMvcConfigurerAdapter {

  override def addResourceHandlers(registry: ResourceHandlerRegistry): Unit = {
    // servlet context URL path pattern --> webapp_relative_path
    registry.addResourceHandler("/css/**").addResourceLocations("/css/")
    registry.addResourceHandler("/js/**").addResourceLocations("/js/")
    registry.addResourceHandler("/website/**").addResourceLocations("/website/")
    registry.addResourceHandler("/index.html").addResourceLocations("/index.html")

    // subsite redirects
    registry.addResourceHandler("/*/app/index.html").addResourceLocations("/index.html")
    registry.addResourceHandler("/*/app/website/**").addResourceLocations("/website/")
    registry.addResourceHandler("/*/app/css/**").addResourceLocations("/css/")
    registry.addResourceHandler("/*/app/js/**").addResourceLocations("/js/")
    registry.addResourceHandler("/*/app/**").addResourceLocations("/")
  }

这是怎么回事?

注意。

  • servlet 上下文是 ROOT (/)。
  • 所有内容都在 /src/main/webapp/ 中(即 /src/main/webapp/website/content/css/theme.css 存在)

【问题讨论】:

    标签: spring spring-mvc


    【解决方案1】:

    我遇到了同样的问题,发现这似乎是由于 Spring 中 ant-style 模式匹配的错误实现,前面的 '/' 不起作用。

    试试这个:

    registry.addResourceHandler("*/app/**").addResourceLocations("/")
    

    在以下位置找到我的完整答案:

    Spring boot static resources mapping ant matchers with ResourceHandlerRegistry

    【讨论】:

      猜你喜欢
      • 2016-07-01
      • 1970-01-01
      • 2020-05-18
      • 1970-01-01
      • 1970-01-01
      • 2014-05-25
      • 1970-01-01
      • 2015-07-19
      • 2017-04-28
      相关资源
      最近更新 更多