有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

我正在做一项作业,正在使用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>

我的控制器:

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’”。由于某种原因,图片中的thymeleaf src没有抓住我的评论。如果我硬编码在正确的路径的图像,我可以让它呈现,但它不会改变,当我去一个不同的页面


共 (2) 个答案

  1. # 1 楼答案

    我发现了问题

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

  2. # 2 楼答案

    不要连接。您可以使用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')}"