I have this class "Listing (kind of a product)". I haven;t created an upload form so i manually inserted one record in Listing Table in MySQL but when rendering on Front Thymleaf isn't rendering.
(Class Object is kind of a product with price, description, images etc.)
Listing Class
@Entity
@Table(name="listings")
public class Listing {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int listing_id;
@Column(nullable=false)
private String listing_name;
private LocalDateTime dateUploaded = LocalDateTime.now();
@Column(nullable=false)
private String decription;
@Column(nullable=false)
private int price;
@Column(nullable=false)
private boolean availability;
@Lob
private byte[] image_1;
@Lob
private byte[] image_2;
@Lob
private byte[] image_3;
@Lob
private byte[] image_4;
@ManyToOne(fetch=FetchType.EAGER,cascade= {CascadeType.PERSIST,CascadeType.MERGE,CascadeType.DETACH,CascadeType.REFRESH})
@JoinColumn(name="category_id")
private Category category;
@ManyToOne(fetch=FetchType.EAGER)
@JoinColumn(name="username")
private User user;
public Listing() {
}
}
Controller
@RequestMapping("/")
public String viewHomePage(Model model) {
return viewPage(model,1);
}
@RequestMapping("/page/{thePageNumber}")
public String viewPage(Model model,@PathVariable(name="thePageNumber") int thePageNumber) {
List<Category> categories = aCategoryService.findall();
Page<Listing> listings= aListingService.findAllPagedSortedByTime(thePageNumber);
List<Listing> listingstoShow= listings.getContent();
//System.out.println(Arrays.toString(listingstoShow.toArray()));
model.addAttribute("currentPage",thePageNumber);
model.addAttribute("allCategories", categories);
model.addAttribute("listings",listingstoShow);
model.addAttribute("totalPages",listings.getTotalPages());
model.addAttribute("totalItems",listings.getTotalElements());
return "index";
}
HTML page where i am trying to display each listing
<div th:each="product:${listingstoShow}"
class="col-lg-4 col-md-4 col-sm-6 product" data-aos="fade-up"
data-aos-delay="200">
<div class="product-inner">
<div class="thumb">
<a th:href="@{/}" class="image">
<img class="first-image" th:src="${product.image_1}" alt="Product" />
<img class="second-image" th:src="${product.image_2}" alt="Product" />
</a>
</div>
<div class="content">
<h4 class="sub-title">
<a th:href= "@{/}" th:text="${product.listing_name}">Something</a>
</h4>
<h5 class="title">
<a th:href="@{/}" th:text="${product.listing_name}">Listing Name</a>
</h5>
<p th:text="@{product.decription}">Description</p>
<span th:text="${product.price}" class="price"> <span class="new">$40.50</span></span>
<div class="shop-list-btn">
<a th:href="@{/}"><button
class="btn btn-sm btn-outline-dark btn-hover-primary"
title="Add To Cart">Book</button></a>
</div>
</div>
</div>
</div>
This is the output I can see where totalItem is 1 as in MySQl there is only one listing which I manually inserted with 4 images as required by class object
question from:
https://stackoverflow.com/questions/65893540/spring-thymleaf-class-object-is-retrieving-from-mysql-but-thymleaf-is-not-able-t 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…