Răsfoiți Sursa

Images: display image on Form Page

Andrew Grant 2 luni în urmă
părinte
comite
bfe6873cd6

+ 3 - 7
src/main/java/scot/carricksoftware/grants/controllers/images/images/ImageFormController.java

@@ -9,21 +9,17 @@ import jakarta.validation.Valid;
 import org.springframework.ui.Model;
 import org.springframework.validation.BindingResult;
 import org.springframework.web.bind.annotation.ModelAttribute;
-import org.springframework.web.bind.annotation.PostMapping;
 import org.springframework.web.bind.annotation.RequestParam;
-import org.springframework.web.bind.annotation.RequestPart;
 import org.springframework.web.multipart.MultipartFile;
 import scot.carricksoftware.grants.commands.images.ImageCommand;
-import scot.carricksoftware.grants.constants.ImageMappingConstants;
+
+import java.io.IOException;
 
 @SuppressWarnings("unused")
 
 public interface ImageFormController {
 
-
-    @SuppressWarnings({"UnnecessaryLocalVariable", "unused"})
-    @PostMapping(ImageMappingConstants.IMAGE)
     String saveOrUpdate(@Valid @ModelAttribute ImageCommand imageCommand,
                         @RequestParam MultipartFile file,
-                        BindingResult bindingResult, Model model);
+                        BindingResult bindingResult, Model model) throws IOException;
 }

+ 17 - 10
src/main/java/scot/carricksoftware/grants/controllers/images/images/ImageFormControllerImpl.java

@@ -25,6 +25,9 @@ import scot.carricksoftware.grants.converters.images.image.ImageConverterImpl;
 import scot.carricksoftware.grants.services.images.image.ImageService;
 import scot.carricksoftware.grants.validators.images.ImageCommandValidator;
 
+import java.io.IOException;
+import java.util.Base64;
+
 @SuppressWarnings("LoggingSimilarMessage")
 @Controller
 public class ImageFormControllerImpl implements ImageFormController {
@@ -66,18 +69,15 @@ public class ImageFormControllerImpl implements ImageFormController {
     }
 
 
-    @SuppressWarnings({"UnnecessaryLocalVariable", "unused"})
     @PostMapping(ImageMappingConstants.IMAGE)
     @Override
     public String saveOrUpdate(@Valid @ModelAttribute ImageCommand imageCommand,
                                @RequestParam MultipartFile file,
-                               BindingResult bindingResult, Model model) {
+                               BindingResult bindingResult, Model model) throws IOException {
         logger.debug("ImageFormControllerImpl::saveOrUpdate");
 
-        var z = imageCommand;
-        var x = file;
-        int debug = -1;
         imageCommand.setFileName(file.getOriginalFilename());
+        imageCommand.setImageData(file.getBytes());
 
         imageCommandValidator.validate(imageCommand, bindingResult);
 
@@ -89,23 +89,30 @@ public class ImageFormControllerImpl implements ImageFormController {
 
         ImageCommand savedCommand = imageService.saveImageCommand(imageCommand);
         model.addAttribute(ImageAttributeConstants.IMAGE_COMMAND, savedCommand);
+        model.addAttribute("image", new ImageUtil().getImgData(savedCommand.getImageData()));
         return MappingConstants.REDIRECT + ImageMappingConstants.IMAGE_SHOW.replace("{id}", "" + savedCommand.getId());
     }
 
 
-
     @SuppressWarnings("SameReturnValue")
     @GetMapping(ImageMappingConstants.IMAGE_SHOW)
     public String showById(@PathVariable String id, Model model) {
         logger.debug("ImageFormControllerImpl::saveOrUpdate");
         ImageCommand savedCommand = imageConverter.convert(imageService.findById(Long.valueOf(id)));
+        if (savedCommand != null) {
+            model.addAttribute("image", new ImageUtil().getImgData(savedCommand.getImageData()));
+        } else {
+            model.addAttribute("image", null);
+        }
         model.addAttribute(ImageAttributeConstants.IMAGE_COMMAND, savedCommand);
         return ViewConstants.IMAGE_FORM;
     }
 
+    public static class ImageUtil {
 
-
-
-
-
+        public String getImgData(byte[] byteData) {
+            return Base64.getMimeEncoder().encodeToString(byteData);
+        }
+    }
 }
+

+ 9 - 1
src/main/resources/templates/images/image/form.html

@@ -76,12 +76,20 @@
             <tr>
                 <td>&nbsp;</td>
             </tr>
-        </table>
+            </table>
+
+            <div th:if="${image != null and !#strings.isEmpty(image)}">
+                        <img class='img-thumbnail' th:src="'data:image/jpeg;base64,' + ${image} "
+                             width="250" height="250" alt =""/>
+            </div>
+
         <button type="submit" class="btn btn-primary">Commit</button>
         <a class="btn btn-secondary" th:href="@{/images}" th:text="${'List all'}">List all</a>
         <a class="btn btn-success" th:href="@{/}" th:text="${'Home'}">Home</a>
         <h6><span style="color: rgb(255,0,0);">*</span><span> Cannot be edited</span></h6>
     </form>
 </div>
+
+
 </body>
 </html>