使用thymeleaf和javaspring从java对象获取字符串

hsvhsicv  于 2021-07-06  发布在  Java
关注(0)|答案(1)|浏览(335)

我试图用thymeleaf从java对象中获取字符串,并使用java spring作为框架,但我得到一个错误,错误是:
org.springframework.expression.spel.spelevaluationexception:el1004e:在java.lang.string类型上找不到方法调用:method getsrc()
html代码是:

<div th:each="product: ${products}">
    <div class="col-1">
        <div class="gallery">
            <div th:each="imgg :${product.id}+img">
                <p th:text="${imgg.getSrc()}"></p>
            </div>
            <div class="desc">
                <p th:text="${product.name}"></p>
            </div>
        </div>
    </div>
</div>

控制器是:

@GetMapping("/")
public String dashboard(ModelMap model) {
  List < CpuProduct > CpuProducts = cpurepo.findAll();

  model.put("products", CpuProducts);

  CpuProducts.forEach(prod ->{

    model.put(prod.getId() + "img", prod.getPhotos());
  });

  return "index";
}

CPU风管等级为:

package com.deiutz.BuildIT.User.products;

import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.OrderColumn;

@Entity
public class CpuProduct {

    private long id;
    private double price;
    private String description;
    private String model;
    private String name;
    private Set<Image> photos;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    public long getId() {
        // TODO Auto-generated method stub
        return id;
    }

    public void setId(long id) {
        this.id = id;

    }

    public double getPrice() {
        // TODO Auto-generated method stub
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    public String getDescrpiton() {

        return description;
    }

    public void setDescrpiton(String description) {
        this.description = description;

    }

    public String getModel() {

        return model;
    }

    public void setModel(String model) {
        this.model = model;

    }

    public String getName() {

        return name;
    }

    public void setName(String name) {
        this.name = name;

    }

    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy = "product")
    @OrderColumn
    public Set<Image> getPhotos() {
        return this.photos;
    }

    public void setPhotos(Set<Image> photos) {
        this.photos = photos;

    }

}

图像类是:

package com.deiutz.BuildIT.User.products;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;

@Entity

public class Image {

    private long id;
    private byte[] foto;
    private CpuProduct product;
    private String src = "img/" + id;

    public byte[] getFoto() {
        return foto;
    }

    public void setFoto(byte[] foto) {
        this.foto = foto;
    }

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    @ManyToOne()
    public CpuProduct getProduct() {
        return product;
    }

    public void setProduct(CpuProduct product) {
        this.product = product;
    }

    public String getSrc() {
        return src;
    }

    public void setSrc(String src) {
        this.src = src;
    }

}
1u4esq0p

1u4esq0p1#

我不知道是什么引起了你的问题。可能您无法通过在模板中添加两个字符串来访问模型Map中的变量,它只会将其连接到字符串。
另一方面,我可以给你一个解决方案(一个更干净的)。简而言之,让我们在一个模型中放置一个按产品idMap的照片Map图,而不是按包含照片的产品id放置n个模型。

model.put("products", cpuProducts);

Map<Integer, List<Photo>> photosByProduct = cpuProducts.stream()
            .collect(Collectors.toMap(CpuProduct::getId, CpuProduct::getPhotos));

//          Or using forEach
//
//          Map<Integer, List<Photo>> photosByProduct = new HashMap<>();
//          cpuProducts.forEach(cpuProduct -> {
//              photosByProduct.put(cpuProduct.getId(), cpuProduct.getPhotos())
//          });

model.put("photos", photosByProduct);

<div th:each="product: ${products}">
    <div class="col-1">
        <div class="gallery">
            <div th:each="photo: ${photos.get(product.id)}">
                <p th:text="${photo.src}"></p>
            </div>
            <div class="desc">
                <p th:text="${product.name}"></p>
            </div>
        </div>
    </div>
</div>

相关问题