Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linking to an image dynamically with Thymeleaf and Spring

Tags:

I am working an assignment and am using thymeleaf and spring to create web pages. I have the majority of it working, but I can not seem to get an image to load.

My 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>

My 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";
}

}

My repository:

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);
}

}

Lastly the java class:

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;
}

}

I am having a hard time figuring out how to link images dynamically. When I run the server I get an error that says "Property or field 'image' cannot be found on null". For some reason in the thymeleaf in the image src is not grabbing my review. If I hard code in the correct path for the image I can get it to render, but it wont change when I go to a different page.

like image 666
Shnpln Avatar asked Sep 27 '18 15:09

Shnpln


1 Answers

Don't concatenate. You can use Thymeleaf's expression to build the URL, like this:

<img th:src="@{/images/{image}(image=${review.image})}">

In this case, {image} is a variable, and the value of that variable is specified between the parenthesis: (image=thevalue). Since the value is itself an expression, you use the regular expression syntax: (image=${review.image}).

It's a little more verbose than concatenating, but it's cleaner in cases where the URL is more complex:

th:src="@{/a/very/long/url/with/{howMany}/parameters/?id={id}&date={date}(howMany=1,id=999,date='2018-09-26')}"
like image 180
Martín Straus Avatar answered Nov 15 '22 01:11

Martín Straus