Я работаю над заданием и использую тимилиф и весну для создания веб-страниц.У меня большая часть работает, но я не могу загрузить изображение.
Мой 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;
}
}
Мне трудно понять, как динамически связывать изображения.Когда я запускаю сервер, я получаю сообщение об ошибке «Свойство или поле« изображение »не могут быть найдены с нулевым значением».По какой-то причине в тимелии на изображении src не хватает моего обзора.Если я жестко закодирую правильный путь для изображения, я могу заставить его отрендериться, но он не изменится, когда я перейду на другую страницу.