如何使用Thymeleaf将文件上传到@ModelAttribute? 我正在做一些事情:
upload.html
<form method="POST" action="#" th:action="@{/sending}" th:object="${collage}" enctype="multipart/form-data" >
<input type="file" th:field="*{picture}" />
<input type="file" th:field="*{picture}" />
<input type="submit" value="upload" />
</form>
我的控制器:
@Controller
public class MainController {
@GetMapping(value = { "/" })
public String index(){
return "upload";
}
@GetMapping("/collage")
public String paintPicture(Model model){
return "collage";
}
@PostMapping("/sending")
public String redirect(@ModelAttribute(value="collage") Collage collage, RedirectAttributes redirectAttr) {
Collections.shuffle(Arrays.asList(collage.getCollage()));
redirectAttr.addFlashAttribute("pictures",collage.getCollage());
return "redirect:/collage";
}
}
Collage.class:
public class Collage {
private MultipartFile[] pictures;
public Collage(){}
public MultipartFile[] getCollage() {
return pictures;
}
public void setCollage(MultipartFile[] pictures) {
this.pictures = pictures;
}
}
我收到了控制台中的java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'collage' available as request attribute
和“/”页面上的文字:
答案 0 :(得分:1)
您可以应用此更改
1)将@ModelAttibute更改为@RequestParam
2)使用MultipartFile []作为参数,仅使用单个输入文件html
//name of input html should be collage
@PostMapping("/sending")
public String redirect(@RequestParam("collage") MultipartFile[] files, RedirectAttributes redirectAttr) {
Collections.shuffle(Arrays.asList(files));
redirectAttr.addFlashAttribute("pictures",files);
return "redirect:/collage";
}
和你的html页面
<form method="POST" th:action="@{/sending}" enctype="multipart/form-data" >
<input type="file" name="collage" multiple="multiple"/>
<input type="submit" value="upload" />
</form>
答案 1 :(得分:0)
一张照片胜过1000个字:
现在的代码示例在Entity内部上传多部分文件数组:
<form action="#" th:action="@{/distribution/save}" class="form-horizontal"
role="form" method="post" th:object="${news}" enctype="multipart/form-data">
<input type="hidden" name="id" value="id" th:field="*{id}"> <div class="form-group has-label"> <label for="inputTitle" th:text="#{news.title}">Título</label>
<input type="text" class="form-control" id="inputTitle" th:placeholder="#{news.title}" th:field="*{title}"></div>
<input type="file" name = "multipartFilesDocument" value="multipartFilesDocument" th:field="*{multipartFilesDocument}" multiple="multiple"/>
<button type="submit" class="btn btn-default"><span th:text="#{common.save}"></span></button>
</div>
</form>
控制器代码:
@PostMapping("/save")
public String saveMultiparthFile(Model model,@ModelAttribute NewsDTO eventDTO){
eventDTO.getId();
return getrDetail(model);
}
实体代码:
public class NewsDTO {
private List<MultipartFile> multipartFilesDocument;
public List<MultipartFile> getMultipartFilesDocument() {
return multipartFilesDocument;
}
public void setMultipartFilesDocument(List<MultipartFile> multipartFilesDocument) {
this.multipartFilesDocument = multipartFilesDocument;
}
}
在这段代码中,enctype="multipart/form-data"
和name = "multipartFilesDocument" value="multipartFilesDocument"
的形式确实很重要