【发布时间】:2018-06-11 19:09:35
【问题描述】:
我正在尝试为模型上传包含其他一些字段的文件,但我无法将文件与这些字段绑定。 处理请求。
@RequestMapping(value="/products", method=RequestMethod.POST)
public String saveProduct(@Valid @ModelAttribute("product") Product product, BindingResult result){
if (result.hasErrors()) {
return "manageProduct";
}
}
在带有一些其他字段的模型类中
@Entity
public class Product {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
private String code;
@NotBlank(message = "Product name can't be blank")
private String name;
@NotBlank(message = "Brand name can't be blank")
private String brand;
@NotBlank(message = "Description can't be blank.")
@Size(min = 10, max = 500, message = "Description shoud be contain 10 and 500 characters")
@JsonIgnore
private String description;
@Min(value = 1)
@Column(name = "unit_price")
private float unitPrice;
private int quantity;
@JsonIgnore
@Column(name = "is_active")
@OrderBy("id ASC")
private boolean active;
@Column(name = "category_id")
@JsonIgnore
private int categoryId;
@Column(name = "supplier_id")
@JsonIgnore
private int supplierId;
private int purchases = 0;
private int views = 0;
@Transient
private MultipartFile file;
//getters and setters
}
form 也是 enctype="multipart/form-data" 并且方法是 "post"。 当尝试使用其他字段上传文件时,会发生错误。 我正在使用弹簧靴。
【问题讨论】:
-
您能发布堆栈跟踪、示例文件属性和完整模型吗?另外,具体的问题是什么?
-
更新了我的模型类,主要是我不能绑定文件和模型类的字段。
-
您不想实际存储文件并与您的实体关联吗?短暂的有什么好处?