如何让bindingresult忽略所需的对象或者任何解决convert byte[]的方法都需要multipartfile我有pb spring mvc vali?

c2e8gylq  于 2021-06-30  发布在  Java
关注(0)|答案(0)|浏览(262)

[1] 看来我有麻烦了。如果删除.haserrors(),则无法将产品添加到db,我可以这样做,但无法验证,反之亦然
产品控制器.java

public String saveProd(ModelMap model, @ModelAttribute(value = "product") @Validated Product product, BindingResult result, @RequestParam MultipartFile image) throws Exception {
    ProductValidator prodVali = new ProductValidator();
    prodVali.validate(product, result);

    if (image.getName() != null) {
        [1]
        if (result.hasErrors()) {
            model.addAttribute("listProduct", productService.getListProduct());
            return "product/add-prod";
        } else {
            System.out.println("Saving file" + image.getOriginalFilename());
            product.setImage(image.getBytes());

            productService.insertProduct(product);
            model.addAttribute("listProduct", productService.getListProduct());
        }
    }
    return "redirect:/admin/product/";
}

添加产品.jsp

<input id="image-input" type="file" name="image" />

产品.java

@Lob
private byte[] image;

//contructor
//geter/seter

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题