简体   繁体   English

我试图根据百里香的pojo的id得到一个特定的图像

[英]I'm trying to get a specific image based on the id of a pojo in thymeleaf

The problem is here. 问题出在这里。 I am trying to get the folder from the id of the object. 我试图从对象的id获取文件夹。 For example, i'd like the photo from images/1/1.jpg prop.id has the id of the property, which will have photos of it in the images/{id} folder. 例如,我想来自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;
    }

This is the code i'm trying to make work. 这是我正在努力工作的代码。 The Property class has id and description i'm interested in at the moment. Property类具有我目前感兴趣的id和描述。 I'd like that based on the id, to get the specific folder named 1 in resources/static/images/1 我希望基于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);
    }



}

I have succeded in extracting the variable id from the object, but i can't make thymeleaf recognize images/${prop.id}/1.jpg as images/1/1.jpg 我已成功从对象中提取变量id,但我不能让百合花识别images / $ {prop.id} /1.jpg为images / 1 / 1.jpg

你应该添加th: for img src:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM