gpt4 book ai didi

java - 使用 Thymeleaf 和 Spring 动态链接到图像

转载 作者:太空宇宙 更新时间:2023-11-04 10:09:12 24 4
gpt4 key购买 nike

我正在做一项作业,正在使用 thymeleaf 和 spring 创建网页。我已经完成了大部分工作,但我似乎无法加载图像。

我的html:

<!DOCTYPE html>
<html xmlns:th="https//www.thymeleaf.org">
<head>
<title>A Course</title>
<link rel="stylesheet" type="text/css"
href="/css/ReviewPageStyleSheet.css" />
</head>
<body>
<h1 id="heading">A Single Course</h1>

<img th:src="@{${'/images/' + review.image}}">

<div id="list" th:each="review: ${reviews}">
<p th:text="${review.title}"></p>
<p th:text="${review.category}"></p>
<a href="http://localhost:8080/show-all-reviews">Back to home</a>

</div>
</body>
</html>

我的 Controller :

package org.wecancodeit.reviewsite;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
public class ReviewSiteController {

@Resource
ReviewSiteRepository reviewsRepo;

@RequestMapping("/show-all-reviews")
public String findAllReviews(Model model) {
model.addAttribute("reviews", reviewsRepo.findAll());
return "reviews";
}

@RequestMapping("review")
public String findOneReview(@RequestParam(value = "id") Long id, Model model) {
model.addAttribute("reviews", reviewsRepo.getOneCourse(id));
return "review";
}

}

我的存储库:

package org.wecancodeit.reviewsite;

import java.util.Collection;
import java.util.HashMap;

import org.springframework.stereotype.Repository;

@Repository
public class ReviewSiteRepository {

private HashMap<Long, Review> reviewList = new HashMap<Long, Review>();

public ReviewSiteRepository() {
Review springMVC = new Review(1L, "Spring Boot & MVC",
"This page reviews information on how to use Spring Boot & MVC and its functionality. ", "java.png");
Review thymeleaf = new Review(2L, "Thymeleaf",
"This page reviews information on what Thymeleaf is, and how to use it.", "thyme.jpg");
Review htmlcss = new Review(3L, "HTML & CSS", "This page reviews what HTML and CSS are, and how to use them. ",
"html.png");

reviewList.put(springMVC.getId(), springMVC);
reviewList.put(thymeleaf.getId(), thymeleaf);
reviewList.put(htmlcss.getId(), htmlcss);

}

public ReviewSiteRepository(Review... reviews) {
for (Review review : reviews) {
reviewList.put(review.getId(), review);
}

}

public Review getOneReview(long firstTestId) {
return reviewList.get(firstTestId);
}

public Collection<Review> findAll() {
return reviewList.values();
}

public Review getOneCourse(Long id) {
return reviewList.get(id);
}

}

最后是java类:

package org.wecancodeit.reviewsite;

public class Review {

private Long Id;
private String title;
private String category;
private String image;

Review(Long id, String title, String category, String image) {
this.Id = id;
this.title = title;
this.category = category;
this.image = image;
}

public Long getId() {
return Id;
}

public String getTitle() {
return title;
}

public String getCategory() {
return category;
}

public String getImage() {
return image;
}

}

我很难弄清楚如何动态链接图像。当我运行服务器时,出现错误“无法在 null 上找到属性或字段‘图像’”。由于某种原因,图像 src 中的 thymeleaf 没有捕获我的评论。如果我在图像的正确路径中进行硬编码,我可以让它渲染,但当我转到不同的页面时它不会改变。

最佳答案

不要连接。您可以使用 Thymeleaf 的表达式来构建 URL,如下所示:

<img th:src="@{/images/{image}(image=${review.image})}">

在本例中,{image} 是一个变量,该变量的值在括号之间指定:(image=thevalue)。由于该值本身就是一个表达式,因此您可以使用正则表达式语法:(image=${review.image})

它比连接更冗长一些,但在 URL 更复杂的情况下它更清晰:

th:src="@{/a/very/long/url/with/{howMany}/parameters/?id={id}&date={date}(howMany=1,id=999,date='2018-09-26')}"

关于java - 使用 Thymeleaf 和 Spring 动态链接到图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52540790/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com