我正在尝试根据百里香中 pojo 的 id 获取特定图像
Posted
技术标签:
【中文标题】我正在尝试根据百里香中 pojo 的 id 获取特定图像【英文标题】:I'm trying to get a specific image based on the id of a pojo in thymeleaf 【发布时间】:2019-10-07 17:39:01 【问题描述】:问题就在这里。我正在尝试从对象的 id 获取文件夹。 例如,我想要 images/1/1.jpg 中的照片 prop.id 具有属性的 id,该属性的照片将在 images/id 文件夹中。
<img class="card-img-top" src="images/$prop.id/1.jpg" >
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" >
<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 →</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,但我无法让 thymeleaf 将 images/$prop.id/1.jpg 识别为 images/1/1.jpg
【问题讨论】:
【参考方案1】:你应该为 img src 添加th:
:
<img class="card-img-top" th:src="$'images/' + prop.id + '/1.jpg'" >
【讨论】:
以上是关于我正在尝试根据百里香中 pojo 的 id 获取特定图像的主要内容,如果未能解决你的问题,请参考以下文章