我正在尝试根据百里香中pojo的ID获取特定的图像

时间:2019-05-21 10:32:00

标签: java spring-boot model-view-controller thymeleaf

问题在这里。我正在尝试从对象的ID获取文件夹。 例如,我想要来自images / 1 / 1.jpg的照片 prop.id具有属性的ID,它将在images / {id}文件夹中包含它的照片。

<img class="card-img-top" src="images/${prop.id}/1.jpg" alt="Card image cap">
Property property = new Property(1,null,"description single", null);

    @RequestMapping(value = "/", method = RequestMethod.GET)
    public ModelAndView messages(Model model) {
        PropertiesDao prop = new PropertiesDao();
        List<Property> x= prop.getAll();
        ModelAndView mav = new ModelAndView("newhome");
        model.addAttribute("multipleProperty",x );
        model.addAttribute("property", property);
        return mav;
    }

这是我正在尝试的代码。 目前,Property类具有我感兴趣的ID和描述。我想根据ID来获取在resources / static / images / 1

中名为1的特定文件夹
<div class="col-sm-3" th:each="prop: ${multipleProperty}">
            <div class="card">
                <img class="card-img-top" src="images/${prop.id}/1.jpg" alt="Card image cap">
                <div class="card-body">
                    <h4 class="card-title" th:text="${prop.description}">Sample Card Title</h4>
                    <p class="card-text" th:text="${prop.id}">He seems sinking under the evidence could
                        not only grieve and a visit. The father is to bless and placed in
                        his length hid...</p>
                    <a href="#" class="btn btn-primary">Tell me more &rarr;</a>
                </div>
            </div>
        </div>
package com.generic.components;

import java.util.List;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

public class Property {

    private int id;
    private List<String> images;
    private String description;
    private List<Rooms> rooms;
    public Property(int id, List<String> images, String description, List<Rooms> rooms) {
        super();
        this.id = id;
        this.images = images;
        this.description = description;
        this.rooms = rooms;
    }
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public List<String> getImages() {
        return images;
    }
    public void setImages(List<String> images) {
        this.images = images;
    }
    public String getDescription() {
        return description;
    }
    public void setDescription(String description) {
        this.description = description;
    }
    public List<Rooms> getRooms() {
        return rooms;
    }
    public void setRooms(List<Rooms> rooms) {
        this.rooms = rooms;
    }
    @Override
    public String toString() {
        return String.format("Property [id=%s, images=%s, description=%s, rooms=%s]", id, images, description, rooms);
    }



}

我已经成功地从对象中提取了变量id,但是我无法让百里香叶将images / $ {prop.id} /1.jpg识别为images / 1 / 1.jpg

1 个答案:

答案 0 :(得分:0)

您应该为img src添加th:

<img class="card-img-top" th:src="${'images/' + prop.id + '/1.jpg'}" alt="Card image cap">