我正在尝试使用Spring mvc创建简单的CRUD控制器和视图
我能够:
获取文档列表
上传文件
删除文件
如果我想使用FORM发送请求,
如何实施下载文件?
我应该用于每个文件吗?
另一件事 - 我正确使用MVC框架吗?
<html>
<body>
<!-- the list: -->
<c:forEach items="${documentList}" var="documentRow">
<Generate table here>
<!-- upload part -->
<form:form modelAttribute="uploadDocument" method="post" enctype="multipart/form-data">
<form:input path="fileData" type="file"/>
<input type="hidden" id="actUploadocument" name="action" value="uploadDocument" />
</form:form>
<!-- delete part -->
<form:form method="post" enctype="multipart/form-data">
<input type="hidden" id="documentId" value="" />
<input type="hidden" id="actUploadocument" name="action" value="deleteDocument" />
</form:form>
</body>
</html>
CRUD控制器?
@Controller
@RequestMapping("/documents")
public class DocumentsController
{
@Autowired
private MainService mainService;
@RequestMapping(method = RequestMethod.GET)
public String listDocuments(Model model) {
List<Document> docs = mainService.getAllDocuments();
model.addAttribute("documentList",docs);
model.addAttribute(new UploadDocument());
return "admin/documents";
}
@RequestMapping(method = RequestMethod.POST , params="action=uploadDocument")
public String uploadDocument(UploadDocument uploadDocument){
savedocument(uploadDocument);
return "redirect:/admin/documents.do";
}
@RequestMapping(method = RequestMethod.POST , params="action=removeDocument")
public String removeDocument(@RequestParam(value="documentId", required=true) String documentId){
savedocument(documentId);
return "redirect:/admin/documents.do";
}
@RequestMapping(method = RequestMethod.POST , params="action=downloadDocument")
public String downloadDocument(@RequestParam(value="documentId", required=true) String documentId,
HttpServletRequest request,HttpServletResponse response ) {
writeDocToResponse(documentId,response);
return null;
}
答案 0 :(得分:0)
基本上,您只需打开文件下载对话框即可将HTTP响应标识为附件。 例如:
response.reset();
response.setContentType(getYourFileContentType());
response.setHeader("Content-Disposition","attachment; filename=\""+getYourFileName()+"\"");
然后你可以调用你的服务方法来传输文件。