首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Thymeleaf和Spring动态链接到图像

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

Stack Overflow用户
提问于 2018-09-27 23:50:58
回答 2查看 9K关注 0票数 5

我正在做一项任务,正在使用胸腺叶和spring创建网页。我有它的大部分工作,但我似乎不能得到一个图像加载。

我的html:

代码语言:javascript
复制
<!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>

我的控制器:

代码语言:javascript
复制
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";
}

}

我的存储库:

代码语言:javascript
复制
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类:

代码语言:javascript
复制
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;
}

}

我很难弄清楚如何动态链接图片。当我运行服务器时,我得到一个错误,说“属性或字段'image‘不能在null上找到”。由于某种原因,在图像中的胸腺叶中,src没有抓住我的评论。如果我在图像的正确路径中硬编码,我可以让它呈现,但当我转到不同的页面时它不会改变。

EN

回答 2

Stack Overflow用户

发布于 2018-09-27 23:59:41

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

代码语言:javascript
复制
<img th:src="@{/images/{image}(image=${review.image})}">

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

它比连接有点冗长,但在URL更复杂的情况下更简洁:

代码语言:javascript
复制
th:src="@{/a/very/long/url/with/{howMany}/parameters/?id={id}&date={date}(howMany=1,id=999,date='2018-09-26')}"
票数 2
EN

Stack Overflow用户

发布于 2018-09-28 00:36:40

我找到问题了。

在我的控制器中,在reviews的请求映射下,我将属性注入为" review“,而不是”review“。一封信就能把整件事搞得一团糟。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52540790

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档