I'm using spring mvc with thymeleaf to upload an image to the server and display it after submitting the form in a separate page "result".
the problem is the image is not displayed until I refresh resources folder "resources/static/images" in Spring Tool Suite and then refresh the page. I modified eclipse workspace options to refresh automatically and the ide part solved but still need to refresh the result page to get the image displayed
This is the controller code
@RequestMapping(value = "/save", method = RequestMethod.POST)
public String contentSubmit(@Valid @ModelAttribute ContentEntity cm, BindingResult result,
@RequestParam("file") MultipartFile file, Model model) {
if (!file.isEmpty()) {
try {
cm.setImgUrl(file.getOriginalFilename());
byte[] bytes = file.getBytes();
BufferedOutputStream stream = new BufferedOutputStream(new FileOutputStream(new File("src/main/resources/static/images/"+cm.getImgUrl())));
stream.write(bytes);
stream.close();
} catch (Exception e) {
//return error page
}
}
if (result.hasErrors()) {
return "cms";
}
contentDao.addContent(cm);
return "view";
}
and this is the view code
<img th:src="@{'images/'+${contentEntity.imgUrl}}" class="image-responsive thumbnail" />