Spring boot 如何修复为每个项目显示相同图像的问题

Spring boot 如何修复为每个项目显示相同图像的问题,spring-boot,thymeleaf,Spring Boot,Thymeleaf,问题:如何修复为每个项目显示相同图像的问题 thymleaf.html <div th:each="goals : ${goals}"> <div th:text="${goals.getId()}"></div> <img th:src="@{'image/'+ ${goals.getId()}}" width="100px;" height="100px;"/> <div th:text="${goals.title}">&l

问题:如何修复为每个项目显示相同图像的问题

thymleaf.html

<div th:each="goals : ${goals}">
<div th:text="${goals.getId()}"></div>
<img th:src="@{'image/'+ ${goals.getId()}}" width="100px;"   
height="100px;"/>
<div th:text="${goals.title}"></div>
查看页面的控制器

@GetMapping(value = "/goals")
public String read(Model model) {

model.addAttribute("login", new LogIn()); //it for bottom menu
model.addAttribute("currentlyPage","goals"); // it for top menu
model.addAttribute("addNewGoal", new addNewGoal()); //another page

//get all goals
List<Goals> goals = new ArrayList<>();
goalsRepository.findAll().forEach(goals::add);
model.addAttribute("goals",goals);
return "goals";
我希望看到不同的图像,但到处都是相同的图像。

我找到了解决方案

  • 我将实体的图像类型更改为ByteBuffer

  • 我通过下一个代码更改了控制器:

    @GetMapping(value = "/image/{id:.+}")
    
    public @ResponseBody ResponseEntity<byte[]> showProductImage(@PathVariable String id, HttpServletResponse response) throws IOException {
    
         Goals goals = goalsRepository.findById(UUID.fromString(id));
    
         ByteBuffer buffer =goals.getImage();
         byte[] bytes = buffer.array();
    
         return ResponseEntity.ok().contentType(MediaType.IMAGE_JPEG).body(bytes);
    }
    
    @GetMapping(value=“/image/{id:.+}”)
    public@ResponseBody ResponseEntity showProductImage(@PathVariable String id,HttpServletResponse)引发IOException异常{
    Goals=goalsRepository.findById(UUID.fromString(id));
    ByteBuffer buffer=goals.getImage();
    byte[]bytes=buffer.array();
    返回ResponseEntity.ok().contentType(MediaType.IMAGE_JPEG).body(字节);
    }
    
  • 我更改了一些类型的值以进行转换

  • 我找到了解决办法

  • 我将实体的图像类型更改为ByteBuffer

  • 我通过下一个代码更改了控制器:

    @GetMapping(value = "/image/{id:.+}")
    
    public @ResponseBody ResponseEntity<byte[]> showProductImage(@PathVariable String id, HttpServletResponse response) throws IOException {
    
         Goals goals = goalsRepository.findById(UUID.fromString(id));
    
         ByteBuffer buffer =goals.getImage();
         byte[] bytes = buffer.array();
    
         return ResponseEntity.ok().contentType(MediaType.IMAGE_JPEG).body(bytes);
    }
    
    @GetMapping(value=“/image/{id:.+}”)
    public@ResponseBody ResponseEntity showProductImage(@PathVariable String id,HttpServletResponse)引发IOException异常{
    Goals=goalsRepository.findById(UUID.fromString(id));
    ByteBuffer buffer=goals.getImage();
    byte[]bytes=buffer.array();
    返回ResponseEntity.ok().contentType(MediaType.IMAGE_JPEG).body(字节);
    }
    
  • 我更改了一些类型的值以进行转换

  • @GetMapping(value = "/image/{id:.+}")
    
    public @ResponseBody ResponseEntity<byte[]> showProductImage(@PathVariable String id, HttpServletResponse response) throws IOException {
    
         Goals goals = goalsRepository.findById(UUID.fromString(id));
    
         ByteBuffer buffer =goals.getImage();
         byte[] bytes = buffer.array();
    
         return ResponseEntity.ok().contentType(MediaType.IMAGE_JPEG).body(bytes);
    }