Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
594 views
in Technique[技术] by (71.8m points)

html - Thymeleaf, IntelliJ and Spring Boot not loading CSS files correctly

I have been experiencing this issues and each time, I only resolve it by creating a new project which appears to work. This time I intend to find out why this happens or I encounter it this often. CSS files get loaded into the server as HTML files and when I open then with the relative path, they open as HTML

Index file located under templates/index.html

  <!DOCTYPE html>
    <html lang="en"
           xmlns="http://www.w3.org/1999/xhtml"
           xmlns:th="http://www.thymeleaf.org">
     <head>
            <meta charset="UTF-8" />
            <title>Title</title>
            <link type="text/css" th:href="@{/css/styles.css}" href="../static/css/styles.css"
                  rel="stylesheet" />
        </head>
        <body>

    <script type="text/javascript" th:src="@{/js/bootstrap.min.js}"></script>
    <script type="text/javascript" th:src="@{/js/jquery.js}"></script>

    </body>
    </html>

This is my CSS file which is located under resources/static/css/styles.css

body {
    color: blue;
}

I have Spring security on my classpath so I configured security as this

@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    private static final String[] PUBLIC_MATCHERS = {
            "/css/**",
            "/js/**",
            "/webjars/**",
            "/static/**"
    };

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                .authorizeRequests().antMatchers(PUBLIC_MATCHERS).permitAll()
                .anyRequest().authenticated()
                .and().formLogin();
    }
}

I do not understand why the browser loads up as such

Seems like all resources are loaded successfully according to the network status

enter image description here

When I click on the styles.css, this is the output in the debugger. I don't know why

enter image description here

enter image description here

enter image description here

Any Help is much appreciated before I start up a new Project again. Thank you

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I was able to solve the problem after a series of test to produce this problem. The problem arises when I leave my mappings general as in allowing my controller to map directly to the root mapping.

Example is

@GetMapping // Here is the problem... This was the only method in the controller
public String home() {
  return "index";
}

I fixed this by setting a mapping like

@GetMapping(value= "/")
public String home() {
   return "index";
}

or by using the Class Level Mapping

@RequestMapping(value= "/")
public class Controller {

   public String home() {
     return "home";
   }

}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...