- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在做一项作业,正在使用 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/
问题:如何对文本文字中的多个连续下划线进行转义? 我正在为 HTML 使用标准的 Thymeleaf 方言(我不在这里使用 Spring 或 SpEL)。 在 Thymeleaf 中,我可以将下划线创
在 SaaS 应用程序中,我使用了一些模板来生成通知电子邮件或某些 HTML 页面。到目前为止,我没有使用 thymeleaf,而且所有模板都是硬编码的,但我很想改变它,以便应用程序的用户可以自己编辑
我看到JSP页面有.jsp/.jspf/.jspx后缀(来自 JavaServer Pages™ Specification Version2.2),Velocity 模板使用 .vm后缀,FreeM
我有一个像这样的 Thymeleaf 片段 ... 脚本部分我只想包含它一次,即使我会在页面中多次包含 f1 。实现这一目标最简单/最干净的方法是什么? 我什至可以将此片段拆
两个 Thymeleaf 属性有什么区别:th:include 和 th:replace? 最佳答案 根据documentation如果您遇到这种情况: content here 片段将被放置在
我是 Thymeleaf 初学者。我从一个通用布局页面开始: fragments/layout.html Template title Some text
我有两个数组,我想在同一个表(不同的列)中显示其内容。如何使用 index 或 th:each 遍历数组 这是我想要实现的目标 List1Elm1 List
我在 session 中有一个对象,例如一个部门,这个部门有 child 。我得到了它的 child 的列表,现在我想在这个列表中添加这个部门对象。这在服务器端非常简单,但可以做到这个在 thymel
我的 Thymeleaf 页面中有几个下拉列表,如下所示: 当我查看页面时,列表中的第一个值显示为已选中,并且实际上已作为选中值提交,即使它不是手动选中的。我宁愿默认不选择任
我有一个通用的布局,默认情况下,除已包含(更高级的)搜索表单的搜索页面本身之外,每个页面上均应显示(基本)搜索表单。 是否可以将参数从我的搜索页面传递到版式,以便不显示默认搜索表单? 这是我想做的一个
我有一个 User 对象列表,我想将它转换为一个名称列表,加入它并呈现它(不是在表格中)。我该怎么做? class User { String name; String address; }
我在前端使用thymeleaf,我知道variable中的thymeleaf概念 如果我使用th:text,变量中的值将被打印,并且我可以在同一元素中使用该变量。有没有办法在其他元素中使用var呢?
我知道 Thymeleaf 是为渲染 View 而制作的,但是我只是想知道是否有任何方法可以在 Thymeleaf 片段的请求范围内设置变量? 我有一个非常大的条件表达式,我必须在整个应用程序中重复很
假设我有两个 Thymeleaf 模板: index.html : foo bar 片段/main.html : This is the main cont
我想声明一些布局用作所有表单字段的模板。 大致给出这个片段 Edition description 这个片段“调用” 它将产生
在 Thymeleaf 中实现 Markdown 的最佳方式是什么? 模板模式 一种新的方言(什么处理器?) 如果我可以在 HTML 中嵌入 markdown,那将会很有用。 最佳答案 根据我对 Ja
我想使用模板片段创建最多包含三个项目的列表。无论是否有项目,项目都会显示三个空格,因此看起来像这样。 0}" th:insert="code-block :: block(${bloc
如何从 Thymeleaf 重定向页面(我有如下 JSP 代码) out.println("REDIRECT=http://www.example.com/api/response?id="+id)
我想在 Thymeleaf 的字符串中放置双引号,我有以下形式: 我想要的结果是: Value of "apple" is "1.5". 但我得到以下异常: EL1065E: unexpected
我想使用模板片段创建最多包含三个项目的列表。无论是否有项目,项目都会显示三个空格,因此看起来像这样。 0}" th:insert="code-block :: block(${bloc
我是一名优秀的程序员,十分优秀!