【问题标题】:Spring Boot + Thymeleaf CSS file cannot access and have 500 error no template foundSpring Boot + Thymeleaf CSS 文件无法访问并出现 500 错误未找到模板
【发布时间】:2019-02-03 03:31:51
【问题描述】:

当我使用 Spring Boot 1.4.0 + Thymeleaf 时,我发现静态资源无法访问并抛出错误“模板可能不存在或可能无法被任何已配置的模板解析器访问”。

Folder structure about my static resources

Browser show 500 error

从服务器日志中,可以找到错误“找不到模板”。但 URL 应该是我的 CSS 文件位置。

2018-08-28 21:07:29.826 ERROR 5676 --- [nio-8080-exec-6] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.thymeleaf.exceptions.TemplateInputException: Error resolving template "Spring Boot Application is available.", template might not exist or might not be accessible by any of the configured Template Resolvers] with root cause

【问题讨论】:

    标签: spring-boot thymeleaf


    【解决方案1】:

    听起来 Spring Boot 正在尝试将资源路径解析为模板。您的控制器之一的路径可能存在冲突。 Spring Boot 默认提供来自/** 的静态资源。您可以使用spring.mvc.static-path-pattern 更改application.properties 文件中的默认路径。

    ## application.properties
    
    spring.mvc.static-path-pattern=/resources/**
    

    这将导致静态内容的新资源路径

    boostrap.min.css

    http://localhost:8080/resources/css/bootstrap.min.css

    https://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#boot-features-spring-mvc-static-content

    【讨论】:

    • 感谢您的回答,但我找到了根本原因,这不是由于 spring.mvc.static-path-pattern。我刚刚发现我有另一个控制器,@RequestMapping 也直接指向 index.html。所以这个函数会阻塞所有的 CSS/JS 资源请求。
    【解决方案2】:

    检查我的源代码后,我发现这是因为我有另一个控制器请求映射到主页并阻止 CSS/JS URL。注释完函数test2()后,首页现在可以加载静态资源了。

    @RequestMapping(value = "/")
    public String test1(){
        return "index";
    }
    
    @RequestMapping
    public String test2(){
        return "index";
    }
    

    【讨论】:

      猜你喜欢
      • 2018-03-01
      • 2015-06-16
      • 2017-06-21
      • 2017-03-20
      • 1970-01-01
      • 1970-01-01
      • 2020-11-21
      • 2017-05-10
      • 2016-01-26
      相关资源
      最近更新 更多