我正在尝试在我的 Spring MVC/jsp 项目中获取文件上传页面,并且我已经正确上传了文件。它在日志文件中输出文件名,然后我将文件名添加到 ModelAndView,当我尝试在视图(.jsp 文件)中访问它们,它们似乎不存在..但我添加的另一个对象确实..
这是我将文件名添加到 ModelAndView 的控制器:
@RequestMapping(value = "/uploadFiles.html", method = RequestMethod.POST)
public String save(@ModelAttribute Token token, @ModelAttribute("uploadForm") FileUpload fileUpload, ModelAndView mav)
{
List<MultipartFile> files = fileUpload.getFiles();
List<String> fileNames = new ArrayList<String>();
if(files != null && files.size() > 0)
{
for(MultipartFile file : files)
{
if(!file.isEmpty())
{
fileNames.add(file.getOriginalFilename());
logger.info("Got file with name: " + file.getOriginalFilename());
}
}
logger.info("Total filenames: " + fileNames.size());
}
mav.addObject("files",fileNames);
mav.addObject("token",token);
return "etl/EtlUploadSuccess";
}
日志输出显示它正在工作:
09:51:09,072 INFO [Controller] [http-bio-8080-exec-4] Got file with name: ExcelFileOne.xlsx
09:51:09,085 INFO [Controller] [http-bio-8080-exec-4] Got file with name: ExcelFileTwo.xls
09:51:09,096 INFO [Controller] [http-bio-8080-exec-4] Total filenames: 2
这是成功页面的代码片段,它应该显示文件名:
<p>The following files have been uploaded successfully with the token ${token.name}:</p>
<c:forEach items="${files}" var="file">
${file}<br/>
</c:forEach>
页面显示:
The following files have been uploaded successfully with the token testToken:
Annnd就是这样......我很困惑为什么文件名没有显示......