我在Spring Boot的电子商务应用中有一个表单。它运作良好。 我的控制器部分如下所示:
@RequestMapping(value = "/admin/add",method = RequestMethod.POST)
public String adminAddProductSubmit(@ModelAttribute(value = "product") Product product){
productServiceJpa.addProduct(product);
return "/admin/added";
}
现在我想将上传输入添加到上传图片中。这儿存在一个问题。 我试过这个:
@RequestMapping(value = "/admin/add",method = RequestMethod.POST)
public String adminAddProductSubmit(final @ModelAttribute(value = "product") Product product, final @RequestAttribute(value = "image") MultipartFile uploadingFile){
File file = new File(uploadingdir + uploadingFile.getOriginalFilename());
try {
uploadingFile.transferTo(file);
} catch (IOException e) {
e.printStackTrace();
}
productServiceJpa.addProduct(product);
}
不幸的是,它不起作用。
我的表格:
<form th:action="@{/admin/add}" th:object="${product}" class="form-horizontal" method="post" enctype="multipart/form-data">
<h2>Nazwa przedmiotu</h2>
<div class="form-group">
<label for="title" class="col-sm-3 control-label">Tytuł</label>
<div class="col-sm-9">
<input type="text" th:field="*{title}" class="form-control" />
</div>
</div>
<div class="form-group">
<label for="category" class="col-sm-3 control-label">Kategoria</label>
<div class="col-sm-9">
<input type="text" th:field="*{category}" class="form-control" />
</div>
</div>
<div class="form-group">
<label for="amount" class="col-sm-3 control-label">Ilość</label>
<div class="col-sm-9">
<input type="text" th:field="*{amount}" class="form-control" />
</div>
</div>
<div class="form-group">
<label for="shortDesc" class="col-sm-3 control-label">Krótki opis</label>
<div class="col-sm-9">
<input type="text" th:field="*{shortDesc}" class="form-control" />
</div>
</div>
<div class="form-group">
<label for="description" class="col-sm-3 control-label">Opis</label>
<div class="col-sm-9">
<input type="text" th:field="*{description}" class="form-control" />
</div>
</div>
<div class="form-group">
<label for="price" class="col-sm-3 control-label">Cena</label>
<div class="col-sm-9">
<input type="text" th:field="*{price}" class="form-control" />
</div>
</div>
<div class="form-group">
<label for="image" class="col-sm-3 control-label">
<div class="col-sm-9">
<input type="file" th:field="*{image}" class="custom-file-input"/>
<span class="custom-file-control"></span>
</div>
</label>
</div>
<input type="submit" class="btn btn-info" value="Dodaj"/>
</form>
你能告诉我如何发送我的对象并在@ModelAttribute中获取它并从文件输入中获取文件?
Spring Boot文档中有很多教程,但是只有上传表单。我希望有一个包含许多文本输入和文件输入的表单。
答案 0 :(得分:0)
您必须为@Valid
参数添加Product
注释,下一个参数必须是BindingResult
。所以你的方法是:
@RequestMapping(value = "/admin/add",method = RequestMethod.POST)
public String adminAddProductSubmit(final @ModelAttribute(value = "product") @Valid Product product, BindingResult bindingResult, final @RequestParam(value = "image") MultipartFile uploadingFile){
File file = new File(uploadingdir + uploadingFile.getOriginalFilename());
try {
uploadingFile.transferTo(file);
} catch (IOException e) {
e.printStackTrace();
}
productServiceJpa.addProduct(product);
}
Here解释了为什么BindingResult
必须在@Valid
注释之后出现。