Thymeleaf + Spring-Boot - why can't I access static resources?

纵饮孤独 提交于 2019-12-08 10:45:17

问题


My project tree looks like this:

I can access the templates now, but can't load static resources such as CSS, images, and JS.

I have a common.html fragment where I declare all my static resources, for instance:

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

A header fragment where I include common.html like so:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:th="http://www.thymeleaf.org">

<head th:include="fragments/common :: commonFragment" lang="en"></head>
// page body
</html>

A default.html file for layout:

<!DOCTYPE html>
<html lang="en"
      xmlns:th="http://www.thymeleaf.org"
      xmlns="http://www.w3.org/1999/xhtml"
      xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout">

<head th:include="fragments/common :: commonFragment" lang="en">
    <meta charset="utf-8"/>
    <meta name="robots" content="noindex, nofollow"/>
    <title>Touch</title>
    <meta http-equiv="X-UA-Compatible" content="IE=edge"/>
    <meta http-equiv="content-dataType" content="text/html; charset=utf-8"/>
    <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
    <meta name="apple-mobile-web-app-capable" content="yes"/>
    <link rel="shortcut icon" th:href="@{/static/images/favicon.ico}"  type="image/x-icon" />
</head>

<body>
<div th:id="defaultFragment" th:fragment="defaultFragment" class="container">
    <div id="header" th:replace="fragments/header :: headerFragment" />
        <div layout:fragment="content" />
</div>
</body>
</html>

Also, on my application.properties file, I have these entries:

#SPRING RESOURCE HANDLING
spring.resources.static-locations=classpath:/resources/

#THYMELEAF
spring.thymeleaf.cache = true
spring.thymeleaf.check-template = true
spring.thymeleaf.check-template-location = true
spring.thymeleaf.content-type=text/html
spring.thymeleaf.enabled=true
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.mode=HTML5
spring.thymeleaf.prefix=classpath:templates/
spring.thymeleaf.suffix=.html
spring.thymeleaf.template-resolver-order=1

But I keep getting the same No mapping found message.

32190 [http-nio-8081-exec-2] WARN  o.s.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/central/css/app.css] in DispatcherServlet with name 'dispatcherServlet'

What am I not seeing here?


回答1:


Use the spring.resources.static-locations in your properties to define the static resources locations (making them directly publicly available):

spring.resources.static-locations=classpath:/your/static/resources/here/like/central/css/

you can provide comma separated values i.e. taken from the documentation:

spring.resources.static-locations=classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/ # Locations of static resources.



回答2:


try to add mapping of your static resources in link

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


来源:https://stackoverflow.com/questions/46324517/thymeleaf-spring-boot-why-cant-i-access-static-resources

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!