Spring boot не подключает css к html странице

почему то не отображаются стили на html странице. Стили лежат в папке static.css.
Что я делаю не так?
Заранее спасибо за ответ.

main_page.html:

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <link rel="stylesheet" href="/static/css/main.css" type="text/css" th:href="@{/static/css/main.css}">
</head>
<body>
<div class="hello">
    hello
</div>
</body>
</html>

main.css:

.hello
{
    width: 123px;
    color: blue;
    background: darkorange;
}

MainController:

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

@Controller
public class MainController {

    @GetMapping("/")
    public String mainPage()
    {
        return "main_page";
    }
}

Application:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SroApplication {

    public static void main(String[] args) {
        SpringApplication.run(SroApplication.class, args);
    }

}

*MVCConfig:

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class MvcConfig implements WebMvcConfigurer {
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry
                .addResourceHandler("/styles/css/**")
                .addResourceLocations("classpath:/static/css/");
    }
}

введите сюда описание изображения


Ответы (1 шт):

Автор решения: Михаил Ребров

Смотрим внимательно на данную строку:

<link rel="stylesheet" href="/css/main.css" type="text/css" th:href="@{/css/menu.css}">

С первого(да и со второго) раза я не заметил,
но потом увидел следующее:

th:href="@{/css/menu.css}"

так не стоит делать - вы просто заменяете то, что было написано в атрибуте href...

В вашем случае тег должен выглядеть так:

<link rel="stylesheet" href="/styles/css/main.css" type="text/css"/>

или так:

<link rel="stylesheet" th:href="@{/styles/css/main.css}" type="text/css"/>

Но не то и другое одновременно, и не противоречащие друг другу значения.
А в ссылке нужно указывать тот путь который вы указали в MvcConfig

Если здесь /styles/css/

.addResourceHandler("/styles/css/**")

то и здесь /styles/css/

href="/styles/css/main.css"
→ Ссылка