无法在Thymeleaf/Spring Boot应用中显示图像。

huangapple go评论75阅读模式
英文:

Can't display an image in the Thymeleaf/Spring Boot App

问题

I have got a problem, which I have been trying to resolve for hours without any success.

I'm building a Spring Boot app with Thymeleaf. In general, I need to put a logo on the index.html page, but it doesn't display at all. I have tried different URI configurations (as in the code snippet), both with and without Thymeleaf tags, none of them worked. Other Thymeleaf (e.g., conditionals etc.) tags work fine - I have tested it, so dependency is not a cause.

Once, I built an app using JSP views and I didn't have any issues with images.

What do you think, am I missing something or what?

project structure

src
 |_main
      |_ java
      |_ resources
               |_ static
                      |_ images
                             |_ logo.png                    
               |_ templates
                      |_ index.html

index.html

<!doctype html>
<html xmlns:th="http://www.thymeleaf.org">

<head th:replace="fragments/head"></head>

<body>
    <!-- Navigation -->
    <nav class="navbar navbar-light">
        <div class="container">
            <a class="navbar-brand">
                <img th:src="@{~/images/logo.png}">
                MyApplication
            </a>
        </div>
    </nav>
    <img th:src="@{~/static/images/logo.png}">
    <img th:src="@{~/images/logo.png}">

    <img th:src="@{//static/images/logo.png}">
    <img th:src="@{//images/logo.png}">

    <img th:src="@{/static/images/logo.png}">
    <img th:src="@{static/images/logo.png}">
    <img th:src="@{/resources/static/images/logo.png}">
    <img th:src="@{resources/static/images/logo.png}">

    <img th:src="@{//static/images/logo.png}">
    <img th:src="@{//static/images/logo.png}">
    <img th:src="@{//resources/static/images/logo.png}">
    <img th:src="@{/resources/static/images/logo.png}">

    <img th:src="@{src/main/resources/static/images/logo.png}">
    <img th:src="@{/src/main/resources/static/images/logo.png}">

    <img src="/static/images/logo.png">
    <img src="static/images/logo.png">

    <img src="/resources/static/images/logo.png">
    <img src="resources/static/images/logo.png">

    <img src="/main/resources/static/images/logo.png">
    <img src="main/resources/static/images/logo.png">

    <img src="src/main/resources/static/images/logo.png">
    <img src="/src/main/resources/static/images/logo.png">

    <img src="D:/Users/Maciek/Documents/git/hotelreservation/src/main/resources/static/images/logo.png">
    <img src="D:\Users\Maciek\Documents\git\hotelreservation\src\main\resources\static\images\logo.png">

<footer th:replace="fragments/footer"></footer>

</body>
</html>
英文:

I have got a problem, which I have been trying to resolve for hours without any success.

I'm building a Spring Boot app with Thymeleaf. In general, I need to put a logo on the index.html page, but it doesn't display at all. I have tried different uri configurations (as in the code snippet), both with and without thymeleaf tags, none of them worked.
Other thymeleaf (eg. conditionals etc.) tags work fine - I have tested it, so dependency is not a cause.

Once, I built an app using jsp views and I didn't have any issues with images.

What do you think, am I missing something or what?

project structure

src
 |_main
      |_ java
      |_ resources
               |_ static
                      |_ images
                             |_ logo.png                    
               |_ templates
                      |_ index.html

index.html

&lt;!doctype html&gt;
&lt;html xmlns:th=&quot;http://www.thymeleaf.org&quot;&gt;
&lt;head th:replace=&quot;fragments/head&quot;&gt;&lt;/head&gt;
&lt;body&gt;
&lt;!-- Navigation --&gt;
&lt;nav class=&quot;navbar navbar-light&quot;&gt; &lt;!--style=&quot;background-color: blueviolet&quot;--&gt;
&lt;div class=&quot;container&quot;&gt;
&lt;a class=&quot;navbar-brand&quot;&gt;
&lt;img th:src=&quot;@{~/images/logo.png}&quot;&gt;
MyApplication
&lt;/a&gt;
&lt;/div&gt;
&lt;/nav&gt;
&lt;img th:src=&quot;@{~/static/images/logo.png}&quot;&gt;
&lt;img th:src=&quot;@{~/images/logo.png}&quot;&gt;
&lt;img th:src=&quot;@{//static/images/logo.png}&quot;&gt;
&lt;img th:src=&quot;@{//images/logo.png}&quot;&gt;
&lt;img th:src=&quot;@{/static/images/logo.png}&quot;&gt;
&lt;img th:src=&quot;@{static/images/logo.png}&quot;&gt;
&lt;img th:src=&quot;@{/resources/static/images/logo.png}&quot;&gt;
&lt;img th:src=&quot;@{resources/static/images/logo.png}&quot;&gt;
&lt;img th:src=&quot;@{//static/images/logo.png}&quot;&gt;
&lt;img th:src=&quot;@{//static/images/logo.png}&quot;&gt;
&lt;img th:src=&quot;@{//resources/static/images/logo.png}&quot;&gt;
&lt;img th:src=&quot;@{/resources/static/images/logo.png}&quot;&gt;
&lt;img th:src=&quot;@{src/main/resources/static/images/logo.png}&quot;&gt;
&lt;img th:src=&quot;@{/src/main/resources/static/images/logo.png}&quot;&gt;
&lt;img src=&quot;/static/images/logo.png&quot;&gt;
&lt;img src=&quot;static/images/logo.png&quot;&gt;
&lt;img src=&quot;/resources/static/images/logo.png&quot;&gt;
&lt;img src=&quot;resources/static/images/logo.png&quot;&gt;
&lt;img src=&quot;/main/resources/static/images/logo.png&quot;&gt;
&lt;img src=&quot;main/resources/static/images/logo.png&quot;&gt;
&lt;img src=&quot;src/main/resources/static/images/logo.png&quot;&gt;
&lt;img src=&quot;/src/main/resources/static/images/logo.png&quot;&gt;
&lt;img src=&quot;hotelreservation/src/main/resources/static/images/logo.png&quot;&gt;
&lt;img src=&quot;/hotelreservation/src/main/resources/static/images/logo.png&quot;&gt;
&lt;img src=&quot;D:/Users/Maciek/Documents/git/hotelreservation/src/main/resources/static/images/logo.png&quot;&gt;
&lt;img src=&quot;D:\Users\Maciek\Documents\git\hotelreservation\src\main\resources\static\images\logo.png&quot;&gt;
&lt;footer th:replace=&quot;fragments/footer&quot;&gt;&lt;/footer&gt;
&lt;/body&gt;
&lt;/html&gt;

答案1

得分: 3

删除 ~,像这样:

<img th:src="@{/images/logo.png}">

如果你使用了Spring Security,请确保允许对资源的请求,方法是在扩展了WebSecurityConfigurerAdapter的Security配置类中覆盖以下方法:

@Override
public void configure(WebSecurity web) throws Exception {
    web.ignoring().antMatchers("static/**");
}
英文:

remove ~, like so:

&lt;img th:src=&quot;@{/images/logo.png}&quot;&gt;

Also if you have Spring Security make sure to allow requests to your resources by overriding this method in your Security Configuration class (the one extending WebSecurityConfigurerAdapter)

@Override
public void configure(WebSecurity web) throws Exception {
web.ignoring().antMatchers(&quot;static/**&quot;);
}

huangapple
  • 本文由 发表于 2020年8月8日 20:06:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/63315185.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定