Java ThymileAF-使用动态url生成图像url
我正在尝试将此实体描述的博客文章的一些信息可视化:Java ThymileAF-使用动态url生成图像url,java,html,spring,thymeleaf,Java,Html,Spring,Thymeleaf,我正在尝试将此实体描述的博客文章的一些信息可视化: @Entity @Table(name = "blog_posts") public class BlogPost { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name = "id") private Long id; @Column(name = "title") private String title
@Entity
@Table(name = "blog_posts")
public class BlogPost {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Long id;
@Column(name = "title")
private String title;
@Column(name = "description")
private String description;
@Column(name = "thumbnail_path")
private String thumbnailPath;
@Column(name = "body")
private String body;
@ManyToOne(cascade = {CascadeType.PERSIST, CascadeType.MERGE,
CascadeType.DETACH, CascadeType.REFRESH})
@JoinColumn(name = "author_id")
private User author;
@Column(name = "date_created")
private Date date = new Date();
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getBody() {
return body;
}
public void setBody(String body) {
this.body = body;
}
public User getAuthor() {
return author;
}
public void setAuthor(User author) {
this.author = author;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getThumbnailPath() {
return thumbnailPath;
}
public void setThumbnailPath(String thumbnailPath) {
this.thumbnailPath = thumbnailPath;
}
}
使用Thymeleaf生成的html,如下所示:
<div class="text-center mt-3 rounded bg-white p-2" th:each="blog_post: ${blog_posts}">
<div class="blog_video position-relative">
<div class="img_blog">
<img th:src="@{~/images/blog/{blog_post.id}/{blog_post.thumbnailPath}}" alt="" class="img-fluid rounded mx-auto d-block">
</div>
<!-- <a href="http://vimeo.com/99025203" class="blog_play"><i class="mdi mdi-play"></i></a> -->
</div>
当我试图为img标记生成一个动态url时,问题就出现了。如何在th:src属性中动态注入变量,如{blog\u post.id}?我找不到解决办法,所以我希望能得到帮助
~EDIT~:事实证明,下面的代码可以做到这一点,但看起来仍然相当混乱。任何人都知道比这更好的方法:
<img th:src="@{/images/blog/{id}/{thumbnail}(id=${blog_post.id}, thumbnail=${blog_post.thumbnailPath})}" alt="No image preview" class="img-fluid rounded mx-auto d-block">
另一种方式:
<img th:src="@{'/images/blog/' +${blog_post.id} +'/' +${blog_post.thumbnailPath}}"
但我认为你的方法看起来更干净:)