3

My project tree looks like this:

enter image description here

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
  • 2
    Have you found any solution to this one? Commented Aug 12, 2018 at 9:49

4 Answers 4

5

I solved this problem with the following code:

spring.resources.static-locations=classpath:templates/
Sign up to request clarification or add additional context in comments.

1 Comment

Holly sh.. I was looking for this so much, I was getting a blank page and I tough I was doing somethign wrong, but the problem was the static content, thank you so much!
4

I'm using Spring Boot 2.2 and not getting any of my static content. I discovered two solutions that worked for me:


Option #1 - Stop using @EnableWebMvc annotation

This annotation disables some automatic configuration, including the part that automatically serves static content from commonly-used locations like /src/main/resources/static. If you don't really need @EnableWebMvc, then just remove it from your @Configuration class.


Option #2 - Implement WebMvcConfigurer in your @EnableWebMvc annotated class and implementaddResourceHandlers()

Do something like this:

@EnableWebMvc
@Configuration
public class SpringMVCConfiguration implements WebMvcConfigurer {

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

}

Just remember that your code is now in charge of managing all static resource paths.


Comments

3

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.

5 Comments

This was my first approach, but it did not work. I am using a very default project structure Spring Boot should really have an issue with this...
what is the error you are getting if adding the above property?
I added spring.resources.static-locations=classpath:/resources/static/css/,classpath:/resources/static/images/,classpath:/resources/static/js/`` to application.properties and got 51669 [http-nio-8081-exec-8] WARN o.s.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/central/css/app.css] in DispatcherServlet with name 'dispatcherServlet'
if you permissioned "/resources/stati‌​c/css/" then why do you expect /central/css/.. to work? There is an unpermissioned central directory along the way
I expected th:href="@{/css/app.css}" to be accessed after classpath:/resources/static/. I have tried the following just now spring.resources.static-locations=classpath:/resources/,classpath:/resources/static/,classpath:/resources/static/css/ and I still get the same no mapping found error.
0

try to add mapping of your static resources in link

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

1 Comment

adding the static mapping href="../../../css/app.css" kind of defeats the whole point of letting Spring Boot handling this, right?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.