我正在嘗試使用Spring mvc創建簡單的CRUD控制器和視圖。
我能:
獲取文檔列表 上傳文件 刪除的文檔Spring3 MVC - 如何在同一個控制器上正確實現CRUD?
如果我想使用的形式發送請求,
如何實現文件下載?
我應該使用每個文件嗎? 另一件事 - 我正確使用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;
}
我認爲你的控制器看起來不錯。這基本上是品嚐和宗教,但這對我很有用。 –
從視圖(JSP)下載文檔的最佳實踐是什麼? – user648026