通过springBoot加载/使用静态内容(css/jpg)

通过springBoot加载/使用静态内容(css/jpg),css,spring-boot,import,Css,Spring Boot,Import,我被一些“愚蠢”的东西迷住了。 我正在构建我的第一个spring应用程序,我正在尝试访问css和jpg资源文件 我访问了许多页面/论坛,每个人的回答都是“spring自动使用/static文件夹中的内容” 但是我失败了,并且一次又一次地失败,在我的页面上包含css和图片。我尝试了很多事情,现在我觉得完全迷失了方向(这应该是一件容易的事情) 我使用WebSecurity配置适配器: @配置 @EnableGlobalMethodSecurity(Prespenabled=true) 公共类Secu

我被一些“愚蠢”的东西迷住了。 我正在构建我的第一个spring应用程序,我正在尝试访问css和jpg资源文件

我访问了许多页面/论坛,每个人的回答都是“spring自动使用/static文件夹中的内容” 但是我失败了,并且一次又一次地失败,在我的页面上包含css和图片。我尝试了很多事情,现在我觉得完全迷失了方向(这应该是一件容易的事情)

我使用WebSecurity配置适配器:

@配置
@EnableGlobalMethodSecurity(Prespenabled=true)
公共类SecurityConfig扩展了WebSecurity配置适配器{
@自动连线
私人用户详细信息服务用户服务;
@凌驾
受保护的无效配置(HttpSecurity http)引发异常{
//@formatter:off
http.authorizeRequests()
.antMatchers(“/”、“/index”、“/page1”、“/open/**”、“/static/**”、“/css/**”、“/img/**”)permitAll()
.anyRequest().authenticated()
.及()
.formLogin()
.login页面(“/login”)
.defaultSuccessUrl(“/secure/page1”)
.failureUrl(“/login?error=true”)
.permitAll()
.及()
.logout()
.logoutUrl(“/logout”)
.logoutSuccessUrl(“/index”)
.permitAll();
//@formatter:on
}
@凌驾
public void configure(WebSecurity web)引发异常{
忽略()antMatchers(“/static/**”);
}
@凌驾
受保护的无效配置(AuthenticationManagerBuilder auth)引发异常{
auth.userDetailsService(userService);
}
@豆子
公共密码编码器PasswordEncoder(){
返回新的BCryptPasswordEncoder();
}
}
在我的application.property中,我有:

spring.resources.static locations=classpath:static

这是我的植树计划:

这是我的index.html文件:

<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>My Quotes</title>

<link th:href="#{/css/main.css}" rel="stylesheet" >
    
<link rel="stylesheet"
    href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css"
    integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh"
    crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.4.1.slim.min.js"
    integrity="sha384-J6qa4849blE2+poT4WnyKhv5vZF5SrPo0iEjwBvKU7imGFAV0wwj1yYfoRSJoZ+n"
    crossorigin="anonymous"></script>
<script
    src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js"
    integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo"
    crossorigin="anonymous"></script>
<script
    src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js"
    integrity="sha384-wfSDF2E50Y2D1uUdj0O3uMBJnjuUD4Ih7YwaYd1iqfktj0Uod8GCExl3Og8ifwB6"
    crossorigin="anonymous"></script>

</head>


<body>
    <div class="container">
        <div th:if="${param.logout}" class="alert alert-info">Deconnected</div>

        <div class="form-group">
            <h1>Bienvenue dans MyQuotes</h1>

            <div class="form-group">
                <div class="button-group" sec:authorize="!isFullyAuthenticated()">
                    <a th:href="@{/login}" class="btn btn-outline-success topright">login</a>
                </div>
                <div class="button-group" sec:authorize="isFullyAuthenticated()">
                    <a th:href="@{/logout}" class="btn btn-outline-warning topright">logout</a>
                </div>

                </br> <span>Ici vous pouvez enregistrer et retrouver toutes vos
                    meilleurs citations et celles de vos amis </span> </br> </br> <a
                    th:href="@{/open/search}" class="btn btn-outline-warning">chercher
                    une quote</a>
            </div>
        </div>

    </div>
<img th:src="@{img/img.jpg}"/>

</body>
</html>


我的名言
断开连接
我的报价单

Ici vous pouvez Enregister和retrouver Touts vos meilleurs引文和非友人小组


有人能发现我做错了什么或我遗漏了什么吗?

更新您的代码以引用CSS,如下所示,它应该可以工作

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

你可以找到更多的细节