我正在做作业,并且正在使用百里香和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>
我的控制器:
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上的属性或字段'image'”。由于某种原因,图像src中的百里香无法抓住我的评论。如果我对图像的正确路径进行了硬编码,则可以渲染它,但是当我转到其他页面时,它不会改变。
答案 0 :(得分:0)
不要串联。您可以使用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')}"
答案 1 :(得分:0)
我发现了问题。
在我的控制器中,在用于审阅的请求映射下,我将属性注入为“审阅”而不是“审阅”。疯狂的一个字母如何使整个事情崩溃。