我正在使用弹簧表单上传文档。html表单...
<form method="post" action="/SafeSiteLive/formTask3.do" enctype="multipart/form-data">
<table id="documentDetailsTable">
<tr>
<td>Document Type: </td>
<td><select id="documentType" name="type"> </select></td>
</tr>
<tr>
<td>
Document Name:
</td>
<td>
<input type="text" id="documentName" name="name"/>
</td>
</tr>
<tr id="newFile">
<td>
Choose a file:
</td>
<td>
<input type="file" name="file" />
</td>
</table>
<input type="text" style="display: none;" name="taskInstanceId" id="taskInstanceId">
<input id="uploadButton" value="Upload" type="submit"/>
<input class="closeButton" id="closeNew" value="Close" type="button"/>
</form>
这连接到我的控制器...
@RequestMapping(value = "/formTask3.do", method = RequestMethod.POST)
public ModelAndView handleFormTaskUpload3(@RequestParam("name") String name,
@RequestParam("type") String type,
@RequestParam("file") MultipartFile file,
@RequestParam("taskInstanceId") int taskInstanceId) {
System.out.println("handleFormUploadTask.1 ");
if (!file.isEmpty()) {
try {
byte[] bytes = file.getBytes();
Document document = new Document();
document.setBytes(bytes);
String extension = "";
int i = file.getOriginalFilename().lastIndexOf('.');
if (i > 0) {
extension = file.getOriginalFilename().substring(i + 1);
}
document.setExtension(extension);
document.setName(name);
document.setType(DocType.documentType.valueOf(type));
Site site = SiteService.getSite(1);
document.setSite(site);
DocumentService.addDocument(document);
DocumentTaskLink docTaskLink = new DocumentTaskLink();
DocumentTaskKey docTaskKey = new DocumentTaskKey();
TaskInstance taskInstance = TaskInstanceService.getTaskInstance(taskInstanceId);
docTaskKey.setDocument(document);
docTaskKey.setTaskInstance(taskInstance);
docTaskLink.setKey(docTaskKey);
DocumentService.saveNewDocumentTaskLink(docTaskLink);
if (bytes != null) {
System.out.println("handleFormUpload. File uploaded with bytes size = " + bytes.length);
}
} catch (Exception e) {
e.printStackTrace();
}
return new ModelAndView("I dont want a new Model And View");
}
return new ModelAndView("redirect:uploadFailure");
}
问题是这个表单返回一个重定向页面的modelAndView。
我希望这最终返回一个表示“成功”或“失败”的字符串,我可以通过对话框通知用户此成功来响应(无需刷新页面)。我什至不介意让这个控制器无效,大多数情况下我只是不想在上传文件时重新加载页面。
这可能吗?
当我使我的控制器无效时,页面仍然重定向到“/SafeSiteLive/formTask3.do”怎么办?
我可以使用这个表单上传控制器返回其他东西吗?