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

时间:2016-08-31 03:22:41

标签: javascript css spring-mvc resources thymeleaf

我的项目具有以下结构:

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。

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

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

但是如果我尝试直接调用这个css,我会被重定向到登录页面而无法访问该文件。我已经尝试按照某人的建议将html foldel重命名为templates和assets文件夹为static(以及页面上的所有必要更改)。结果相同。

令我感到奇怪的是,这个项目几乎是我工作的另一个项目的 ipsis literis 副本,一切都得到了很好的解决。

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

1 个答案:

答案 0 :(得分:0)

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

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

    ........


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