我使用带有 thymeleaf 的 spring mvc 将图像上传到服务器并在单独的页面“结果”中提交表单后显示它。
问题是在我刷新 Spring Tool Suite 中的资源文件夹“resources/static/images”然后刷新页面之前,图像不会显示。 我修改了 eclipse 工作区选项以自动刷新并且 ide 部分已解决但仍然需要刷新结果页面以显示图像
这是 Controller 代码
@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";
}
这是 View 代码
<img th:src="@{'images/'+${contentEntity.imgUrl}}" class="image-responsive thumbnail" />
最佳答案
您正在“src/resources/...”目录中实用地创建资源(图像文件)。但是,您的服务器似乎是来自不同目录(例如 target/** 或 bin/**)的 serverig 图像。因此,当您刷新资源目录时,STS 会检测到最近创建的新文件,然后将文件复制到目标目录下。
eclipse 中有一个选项“本地 Hook 或轮询”,它不断监视资源的变化(即使这些变化是在 STS/eclipse 外部进行的)。一旦检测到更改,资源将自动刷新。
您可能希望通过以下方式设置此选项:首选项 > 常规 > 工作区 >“使用 native Hook 或轮询刷新”
希望这对您有所帮助。
关于java - 在我刷新页面 Spring Mvc 之前,上传的图像不会显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31892652/