gpt4 book ai didi

html - 如何使用 thymeleaf 在 Spring Boot 中加载图像?

转载 作者:行者123 更新时间:2023-12-04 08:14:05 29 4
gpt4 key购买 nike

我想简单地在 Spring Boot 应用程序的 html 文件中显示图像。
但出于某种原因,我无法让它在一个特定的应用程序中工作。
我说“一个特定的应用程序”是因为我可以让它在另一个应用程序中以完全相同的方式工作。
这是它的样子:
将 tempLogo.jpg 放入文件夹 /src/main/resources/static/images .
在 src/main/resources/templates/home.html 中:

<img src="images/tempLogo.jpg"></br>
<img src="/images/tempLogo.jpg"></br>
<img th:src="@{/images/tempLogo.jpg}"></br>
<img th:src="@{images/tempLogo.jpg}"></br>
这些都不适用于 app1。
所有这些都在 app2 中使用相同的代码和路径运行。
有趣的是,在 app1 中,我可以像这样成功访问/static 文件夹中的 .css 和 .js 文件:
<link rel="stylesheet" type="text/css" href="/css/styles.css"/>
<script src="/scripts/scripts.js"></script>
这些在: /src/main/resources/static/css /src/main/resources/static/scripts不知道发生了什么,就像我说的那样,在 app2 中,它具有相同的确切路径和代码,它确实有效。

最佳答案

经过一番挖掘,发现问题是没有为图像文件夹启用 Spring 安全性。所以加了web.ignoring().antMatchers("/images/**");在扩展 WebSecurityConfigurerAdapter 的类的配置方法中,它起作用了。

@Override
public void configure(WebSecurity web) throws Exception {
web.ignoring().antMatchers("/css/**");
web.ignoring().antMatchers("/scripts/**");
web.ignoring().antMatchers("/images/**");
}

关于html - 如何使用 thymeleaf 在 Spring Boot 中加载图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65797794/

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