【问题标题】:Image paths are not working in springboot图像路径在 Spring Boot 中不起作用
【发布时间】:2021-11-10 11:18:18
【问题描述】:

我正在使用 spring configure 来存储和显示存储的图像 查看级别。当用户搜索产品时,显示带有图片的产品 和名称,但我的情况它只显示产品名称和产品价格 但不显示产品图片 webconfig.kt

package com.nilmani.cmsshopingcart

import org.springframework.context.annotation.Configuration
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer
@Configuration
class WebConfig : WebMvcConfigurer {
    override fun addResourceHandlers(registry: ResourceHandlerRegistry) {
        registry
            .addResourceHandler("/media/**")
            .addResourceLocations("file:/C:/Users/shyamal/Downloads/cms-shopping-cart/src/main/resources/static/media/")
    }
}

产品索引.html

<!doctype html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head th:replace="/fragments/head"></head>
<body>
<nav th:replace="/fragments/nav :: nav-admin"></nav>
<div class="container">
    <h1 class="display-2">Pages</h1>
    <a href="/admin/products/add" class="btn btn-primary mb-5">Add new</a>
    <!--suppress ThymeleafVariablesResolveInspection -->
    <div th:if="${message}" th:text="${message}" th:class="${'alert ' + alertClass}"></div>
    <div th:if="${!products.isEmpty()}">
        <table class="table sorting" id="pages">
            <tr class="home">
                <th>Name</th>
                <th>Image</th>
                <th>Category</th>
                <th>Price</th>
            </tr>
            <tr th:each="product : ${products}">
                <td th:text="${product.name}"></td>
                <td>
                    <img style="width:120px;" th:src="@{'/media/' + ${product.image}}">
                </td>
                <td th:text="${cats[__${product.categoryId}__]}"></td>
                <td th:text="${product.price}"></td>
                <td><a th:href="@{'/admin/products/edit/' + ${product.id}}">Edit</a></td>
                <td><a th:href="@{'/admin/pages/delete/' + ${product.id}}" class="confirmDeletion">Delete</a></td>
            </tr>
        </table>
    </div>

    <nav class="mt-3" th:if="${count > perPage}">
        <ul class="pagination">
            <li class="page-item" th:if="${page > 0}">
                <a th:href="@{${#httpServletRequest.requestURI} + '?page=__${page-1}__'}" class="page-link">Previous</a>
            </li>
            <li class="page-item" th:each="number :${#numbers.sequence(0,pageCount - 1)}"
                th:classappend="${page==number} ? 'active' : ''">
                <a th:href="@{${#httpServletRequest.requestURI} + '?page=_${number}'}"
                   class="page-ling" th:text="${number+1}"></a>
            </li>
            <li class="page-item" th:if="${page < pageCount -1}">
                <a th:href="@{${#httpServletRequest.requestURI} + '?page=__${page+1}__'}" class="page-link">Next</a>
            </li>
        </ul>
    </nav>

    <div th:unless="${!products.isEmpty()}">
        <h4 class="display-4">There are no pages at the moment</h4>
    </div>
</div> <!-- /container -->
<div th:replace="/fragments/footer"></div>
</script>
</body>
</html>

【问题讨论】:

    标签: spring-boot thymeleaf


    【解决方案1】:

    问题出在

    .addResourceLocations("file:/C:/Users/shyamal/Downloads/cms-shopping-cart/src/main/resources/static/media/")
    

    这里使用相对路径,或者直接使用,

    <img src="../static/images/pirate.jpg" width="1000" th:src="@{images/pirate.jpg}"/>
    

    它是重复的:Can't load image with spring boot thymeleaf

    【讨论】:

      猜你喜欢
      • 2019-10-30
      • 2021-06-07
      • 2017-03-03
      • 2018-05-26
      • 1970-01-01
      • 2014-09-04
      • 2012-11-05
      • 2021-07-26
      • 2017-11-20
      相关资源
      最近更新 更多