在Spring ResourceHandlerRegistry中无法匹配/ * / xxx?

时间:2015-08-13 21:15:04

标签: spring spring-mvc

我尝试将以下网址解析为我服务器上的相同资源:

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("/")
  }

这是怎么回事?

NB。

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

1 个答案:

答案 0 :(得分:1)

我遇到了同样的问题,发现这似乎是由于Spring中蚂蚁风格模式匹配的执行不好,前面的'/'不起作用。

试试这个:

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

在我的完整答案中找到答案:

Spring boot static resources mapping ant matchers with ResourceHandlerRegistry