gpt4 book ai didi

java - Spring Boot Thymeleaf 静态内容未加载

转载 作者:行者123 更新时间:2023-12-02 03:13:23 25 4
gpt4 key购买 nike

我刚刚启动了一个新的 Spring Boot(带有 Spring Security)和 Thymeleaf 项目。因为我想集成静态资源,所以我尝试了不同的文件结构,但没有一个,Spring 绑定(bind)了文件。由于我没有更改静态资源路径,因此我的控制台输出 Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]启动时。

我尝试过不同的文件结构,但没有成功。

src
|--main
|--java
|--resources
|--static
|--css
|--bootstrap.min.css
|--custom_test.css
|--templates
|--....

还有

src
|--main
|--java
|--resources
|--webjars
|--static
|--css
|--bootstrap.min.css
|--custom_test.css
|--templates
|--....

src
|--main
|--java
|--resources
|--public
|--css
|--bootstrap.min.css
|--custom_test.css
|--templates
|--....

在 HTML/Thymeleaf 中我尝试过

    <link rel="stylesheet" type="text/css" href="webjars/static/css/bootstrap.min.css"/>
<link rel="stylesheet" type="text/css" href="webjars/static/css/custom_test.css"/>

    <link rel="stylesheet" type="text/css" th:href="@{/css/bootstrap.min.css}"/>
<link rel="stylesheet" type="text/css" th:href="@{/css/custom_test.css}"/>

到目前为止,这些都不起作用。我真的希望你能帮助我和其他有同样问题的人。提前致谢!

最佳答案

您的问题可能是Spring Security的配置问题。首先,检查浏览器的开发人员工具,看看对客户端未获取的资源的响应是什么。响应状态代码可能是 302 或 401,具体取决于您当前的配置。

如果是这种情况,您需要向项目添加如下所示的附加配置:

@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter
{
@Override
protected void configure(HttpSecurity http) throws Exception
{
http.authorizeRequests()
.antMatchers("/").permitAll()
.antMatchers("/favicon.ico").permitAll()
.antMatchers("/css/**").permitAll()
.antMatchers("/js/**").permitAll()
.antMatchers("/static/**").permitAll()
.antMatchers("/images/**").permitAll()
.antMatchers("/blog/**").permitAll()
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage("/login")
.permitAll()
.and()
.logout()
.permitAll();
}
// ...
}

关于java - Spring Boot Thymeleaf 静态内容未加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40726626/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com