【问题标题】:Unable to load resource using Thymeleaf View Layer无法使用 Thymeleaf 视图层加载资源
【发布时间】:2014-08-17 19:55:27
【问题描述】:

我是第一次使用 Thymeleaf 视图层。我已将模板的默认位置更改为WEB-INF/views,并且我的所有静态资源都位于/resources/css/ 目录下,这些资源使用spring 资源处理程序映射到/css/** 模式。我有一个使用在运行时失败的 CSS 文件的页面。我得到以下异常。如果我删除 <link> 标签,页面加载时没有应用任何 CSS。以下是我在页面中的链接

任何指针将不胜感激。

<link rel="stylesheet" href="/resources/css/bootstrap.min.css" th:href="@{/css/bootstrap.min.css}">

资源处理程序:

@Override
 public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/css/**").addResourceLocations("/resources/css/");
        registry.addResourceHandler("/js/**").addResourceLocations("/resources/js/");
 }

例外:

javax.servlet.ServletException: Filter execution threw an exception
    org.springframework.boot.context.web.ErrorPageFilter.handleException(ErrorPageFilter.java:135)
    org.springframework.boot.context.web.ErrorPageFilter.doFilter(ErrorPageFilter.java:104)
    org.springframework.boot.context.web.ErrorPageFilter.doFilter(ErrorPageFilter.java:85)
    org.springframework.boot.context.web.ErrorPageFilter.handleException(ErrorPageFilter.java:135)
    org.springframework.boot.context.web.ErrorPageFilter.doFilter(ErrorPageFilter.java:104)
    org.springframework.boot.context.web.ErrorPageFilter.doFilter(ErrorPageFilter.java:85)
    org.springframework.boot.context.web.ErrorPageFilter.handleException(ErrorPageFilter.java:135)

【问题讨论】:

  • stacktrace 的摘录并未指出问题所在。你能发布完整的堆栈跟踪吗?

标签: spring spring-mvc spring-security thymeleaf


【解决方案1】:

您使用的是spring boot,不需要覆盖addResourceHandlers(..),只需将资源放在src/main/resources/staticsrc/main/resources/publicsrc/main/resources/resources中即可,这些文件夹在spring boot中默认注册。

更新:
然后把它放在 /WEB-INF/static 或 WEB-INF/resources 或 WEB-INF/public

【讨论】:

  • 但是我不想把WEB相关的资源放到src/main/resources下面。我想把CSS相关的东西放在/webapp/resources/css里面。如何配置 ThymLeaf 以从该位置读取资源?
  • 感谢您的更新。我设法让那部分工作。但是现在我遇到了一个非常奇怪的 StackOverflow 错误,如果我包含一个简单的静态 twitter 引导代码 sn-p,页面将无法工作。我不明白问题的本质。
猜你喜欢
  • 2023-02-15
  • 2018-09-11
  • 2017-07-13
  • 1970-01-01
  • 2017-03-13
  • 2016-08-05
  • 1970-01-01
  • 2015-03-08
  • 2012-07-26
相关资源
最近更新 更多