How to upload files to the @ModelAttribute using Thymeleaf? I'am doing something that:
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>
My controller:
@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;
}
}
I'm getting: java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'collage' available as request attribute
in the console and a text on "/" page: