Thymeleaf 找到 CSS 和 JS 文件,但不解释它们。为什么?

Thymeleaf finds CSS and JS file, but doesn't interpret them. Why?

我的项目结构如下:

webapps/
    assets/
        css/
        img/
        js/
    WEB-INF/
        html/
            fragments/
                common.html
                default.html
                header.html
                sidebar.html
        home/
            homeNotSignedIn.html
            homeSignedIn.html   
        login.html

我的 Application.java 我有这个:

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "b.c.g.appName")
public class AppnameConfiguration extends WebMvcConfigurerAdapter {

// ... several beans

    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry
            .addResourceHandler("/css/**","/js/**","/img/**")
            .addResourceLocations("/css/","/js/","/img/")
            .setCachePeriod(3600)
            .resourceChain(true)
            .addResolver(new GzipResourceResolver())
            .addResolver(new PathResourceResolver());
    }
}

当我 运行 应用程序时,会显示我的登录页面 homeNotSignedIn.html,但 Thymeleaf 未加载 CSS 或 JS。

当我右键单击并 select 查看页面源代码时,所有链接都在那里,格式正确如下:

<link rel="stylesheet" href="/applicationName/assets/css/animate.min.css" />

但是如果我尝试直接调用此 css,我将被重定向到登录页面并且无法访问该文件。我已尝试 按照 某人的建议将 html 文件夹重命名为模板,并将资产文件夹重命名为静态(以及页面上所有必要的更改)。相同的结果。

真正让我感到奇怪的是,这个项目几乎是我从事的另一个项目的 ipsis literis 副本,并且一切都得到了完美的解决。

我快要发疯了...请帮帮我!我错过了什么?

您的 Dispatcher 是否配置为处理请求? 您可能需要在 onStartup 中使用与此类似的内容:

@Override
public void onStartup(ServletContext servletContext) throws ServletException {

    ........


    AnnotationConfigWebApplicationContext dispatcherContext = new   AnnotationConfigWebApplicationContext();
        dispatcherContext.register(AppnameConfiguration .class);
}