Shn*_*pln 5 java image thymeleaf spring-boot
我正在做一项作业,并使用 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>Run Code Online (Sandbox Code Playgroud)
我的控制器:
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";
}
}
Run Code Online (Sandbox Code Playgroud)
我的存储库:
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);
}
}
Run Code Online (Sandbox Code Playgroud)
最后是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;
}
}
Run Code Online (Sandbox Code Playgroud)
我很难弄清楚如何动态链接图像。当我运行服务器时,出现错误“无法在 null 上找到属性或字段‘图像’”。由于某种原因,图像 src 中的百里香叶没有抓住我的评论。如果我在图像的正确路径中进行硬编码,我可以让它渲染,但当我转到不同的页面时它不会改变。
不要串联。您可以使用 Thymeleaf 的表达式来构建 URL,如下所示:
<img th:src="@{/images/{image}(image=${review.image})}">
Run Code Online (Sandbox Code Playgroud)
在本例中,{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')}"
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
11145 次 |
| 最近记录: |