jeecg基礎框架中對上傳附件已經封裝的至關不錯了,只須要參照演示demo中的多附件管理的例子稍做修改就能夠了(在」經常使用示例-多附件管理「下,另外一個例子」上傳下傳「太簡單了,通常項目中不可能這麼簡單的)。javascript
先看一下jeecg基礎框架自帶的存儲附件的表及實體中主要屬性:css
private TSUser TSUser;// 建立人 private String businessKey;// 業務類主鍵 private String subclassname;// 子類名稱全路徑 private String attachmenttitle;// 附件名稱 private byte[] attachmentcontent;// 附件內容 private String realpath;// 附件物理路徑 private Timestamp createdate; private String note; private String swfpath;// swf格式路徑 private String extend;// 擴展名
其中businessKey就能夠和咱們的業務表來關聯了,用來存儲業務表的主鍵java
下面主要記錄一下參照基礎框架中自帶例子須要修改的地方:web
controllerajax
public ModelAndView addorupdate(ExprDelComEntity exprDelCom, HttpServletRequest req) { if (StringUtil.isNotEmpty(exprDelCom.getId())) { exprDelCom = exprDelComService.getEntity(ExprDelComEntity.class, exprDelCom.getId()); req.setAttribute("exprDelComPage", exprDelCom); List<TSAttachment> exprDelComFiles = exprDelComService.findByProperty(TSAttachment.class, "businessKey", exprDelCom.getId()); req.setAttribute("exprDelComFiles", exprDelComFiles); } return new ModelAndView("com/jason/logistics/express/exprDelCom"); }
這邊除了須要根據id獲取對應的實體實例外,還須要根據業務主鍵的id獲取對應的附件列表,jeecg自帶的例子是直接新建一箇中間表t_finance_files(對應的實體爲TFinanceFilesEntity)來保存附件表t_s_attachment與業務的關聯,好處是能夠在業務表創建一個與附件表的一對多的關係,在加載時只須要直接根據業務id來加載業務對象就能夠了,在頁面能夠直接經過xxx.files這樣來獲取附件列表,而我這邊直接利用附件表中的businessKey,好處是不須要多建一個對象和一張中間表,但須要在controller中額外的根據業務表的id獲取對應的附件列表。sql
@RequestMapping(params = "delFile") @ResponseBody public AjaxJson delFile( HttpServletRequest request) { AjaxJson j = new AjaxJson(); String id = request.getParameter("id"); TSAttachment file = systemService.getEntity(TSAttachment.class, id); message = "" + file.getAttachmenttitle() + "被刪除成功"; exprDelComService.deleteFile(file); systemService.addLog(message, Globals.Log_Type_DEL, Globals.Log_Leavel_INFO); j.setSuccess(true); j.setMsg(message); return j; }
增長一個delFile方法專門來用於處理刪除文件的邏揖express
@RequestMapping(params = "save") @ResponseBody public AjaxJson save(ExprDelComEntity exprDelCom, HttpServletRequest request) { AjaxJson j = new AjaxJson(); if (StringUtil.isNotEmpty(exprDelCom.getId())) { message = "快遞公司信息更新成功"; ExprDelComEntity t = exprDelComService.get(ExprDelComEntity.class, exprDelCom.getId()); try { MyBeanUtils.copyBeanNotNull2Bean(exprDelCom, t); exprDelComService.saveOrUpdate(t); systemService.addLog(message, Globals.Log_Type_UPDATE, Globals.Log_Leavel_INFO); } catch (Exception e) { e.printStackTrace(); message = "快遞公司信息更新失敗"; } } else { message = "快遞公司信息添加成功"; exprDelComService.save(exprDelCom); systemService.addLog(message, Globals.Log_Type_INSERT, Globals.Log_Leavel_INFO); } // j.setMsg(message); j.setObj(exprDelCom); return j; }
修改save方法,生成的代碼是直接返回String類型的信息的,這邊由於頁面中須要獲取業務實體的id及其它可能的屬性,因此須要返回整個業務對象的實體(這邊讓我在開發過程當中卡了一個殼)api
@RequestMapping(params = "del") @ResponseBody public AjaxJson del(ExprDelComEntity exprDelCom, HttpServletRequest request) { AjaxJson j = new AjaxJson(); exprDelCom = systemService.getEntity(ExprDelComEntity.class, exprDelCom.getId()); message = "快遞公司信息刪除成功"; exprDelComService.deleteExprDelCom(exprDelCom); systemService.addLog(message, Globals.Log_Type_DEL, Globals.Log_Leavel_INFO); j.setMsg(message); return j; }
這邊須要修改del方法,在刪除業務實體時調用專門的方法deleteExprDelCom,而不是默認的基類的delete方法,是由於這邊在刪除對應的業務表的記錄外還須要刪除對應的附件表的實例及對應在磁盤中的文件。app
@RequestMapping(params = "saveFiles", method = RequestMethod.POST) @ResponseBody public AjaxJson saveFiles(HttpServletRequest request, HttpServletResponse response, TSAttachment tSAttachment) { AjaxJson j = new AjaxJson(); Map<String, Object> attributes = new HashMap<String, Object>(); String fileKey = oConvertUtils.getString(request.getParameter("fileKey"));// 文件ID String exprDelComId = oConvertUtils.getString(request.getParameter("exprDelComId"));//快遞公司ID if (StringUtil.isNotEmpty(fileKey)) { tSAttachment = systemService.getEntity(TSAttachment.class, fileKey); } // ExprDelComEntity exprDelCom = exprDelComService.getEntity(ExprDelComEntity.class, exprDelComId); tSAttachment.setBusinessKey(exprDelComId); UploadFile uploadFile = new UploadFile(request, tSAttachment); uploadFile.setCusPath("files"); uploadFile.setSwfpath("swfpath"); uploadFile.setByteField(null);//不存二進制內容 tSAttachment = systemService.uploadFile(uploadFile); attributes.put("fileKey", tSAttachment.getId()); attributes.put("viewhref", "commonController.do?objfileList&fileKey=" + tSAttachment.getId()); attributes.put("delurl", "commonController.do?delObjFile&fileKey=" + tSAttachment.getId()); j.setMsg("文件添加成功"); j.setAttributes(attributes); return j; }
增長saveFiles方法專門處理上傳附件的邏輯框架
2. service
public interface ExprDelComServiceI extends CommonService{ void deleteFile(TSAttachment file); void deleteExprDelCom(ExprDelComEntity exprDelCom); }
接口中增長兩個方法,一個是刪除附件的方法,一個是刪除業務實體的方法
@Service("exprDelComService") @Transactional public class ExprDelComServiceImpl extends CommonServiceImpl implements ExprDelComServiceI { /** * 附件刪除 */ public void deleteFile(TSAttachment file) { //[1].刪除附件 String sql = "select * from t_s_attachment where id = ?"; Map<String, Object> attachmentMap = commonDao.findOneForJdbc(sql, file.getId()); //附件相對路徑 String realpath = (String) attachmentMap.get("realpath"); String fileName = FileUtils.getFilePrefix2(realpath); //獲取部署項目絕對地址 String realPath = ContextHolderUtils.getSession().getServletContext().getRealPath("/"); FileUtils.delete(realPath+realpath); FileUtils.delete(realPath+fileName+".pdf"); FileUtils.delete(realPath+fileName+".swf"); //[2].刪除數據 commonDao.delete(file); } /** * 快遞公司刪除 */ public void deleteExprDelCom(ExprDelComEntity exprDelCom) { //[1].上傳附件刪除 String attach_sql = "select * from t_s_attachment where id in (select id from t_finance_files where financeId = ?)"; List<Map<String, Object>> attachmentList = commonDao.findForJdbc(attach_sql, exprDelCom.getId()); for(Map<String, Object> map:attachmentList){ //附件相對路徑 String realpath = (String) map.get("realpath"); String fileName = FileUtils.getFilePrefix2(realpath); //獲取部署項目絕對地址 String realPath = ContextHolderUtils.getSession().getServletContext().getRealPath("/"); FileUtils.delete(realPath+realpath); FileUtils.delete(realPath+fileName+".pdf"); FileUtils.delete(realPath+fileName+".swf"); } //[2].刪除資金管理 commonDao.delete(exprDelCom); } }
實現類中增長丙個方法的實現,只要根據jeecg例子的代碼稍作修改便可。
3. 界面(jsp)
<script type="text/javascript"> $(function(){ //查看模式狀況下,刪除和上傳附件功能禁止使用 if(location.href.indexOf("load=detail")!=-1){ $(".jeecgDetail").hide(); } }); function uploadFile(data){ $("#exprDelComId").val(data.obj.id); alert($(".uploadify-queue-item")); if($(".uploadify-queue-item").length>0){ upload(); }else{ frameElement.api.opener.reloadTable(); frameElement.api.close(); } } function close(){ frameElement.api.close(); } </script> <!-- 彈出頁面窗口大小控制 --> <style type="text/css"> #formobj { height: 65%; min-height: 300px; overflow-y: auto; overflow-x: auto; min-width: 600px; } </style>
在新增修改界面<head>部分中增長專門處理與附件有關的代碼,其中uploadFile方法是最主要的,當提交表單保存業務表的記錄成功後會調用這個方法來繼續上傳附件,也就是說jeecg中處理上傳附件是分兩步來處理的
<t:formvalid formid="formobj" dialog="true" usePlugin="password" layout="table" callback="@Override uploadFile" action="exprDelComController.do?save">
這邊使用了jeecg中的t:formvalid標籤,注意默認生成的代碼中callback這個屬性是沒有的,這邊增長這個屬性就是爲了在表單提交成功後繼續上傳附件(@Override uploadFile),這邊uploadFile就是對應前面增長的javascrip方法名了,而前面的@Override關鍵字我猜應該是默認已經有uploadFile這個方法了,這邊是專門進行重寫。
<tr> <td align="right"><label class="Validform_label"> 三證信息: </label></td> <td class="value"><input type="hidden" value="${exprDelComPage.id}" id="exprDelComId" name="exprDelComId" /> <table> <c:forEach items="${exprDelComFiles}" var="exprDelComFile"> <tr style="height: 34px;"> <td>${exprDelComFile.attachmenttitle}</td> <td><a href="commonController.do?viewFile&fileid=${exprDelComFile.id}&subclassname=org.jeecgframework.web.system.pojo.base.TSAttachment" title="下載">下載</a></td> <td><a href="javascript:void(0);" onclick="openwindow('預覽','commonController.do?openViewFile&fileid=${exprDelComFile.id}&subclassname=org.jeecgframework.web.system.pojo.base.TSAttachment','fList','800','700')">預覽</a></td> <td><a href="javascript:void(0)" class="jeecgDetail" onclick="del('exprDelComController.do?delFile&id=${exprDelComFile.id}',this)">刪除</a></td> </tr> </c:forEach> </table></td> </tr> <tr> <td></td> <td class="value"><script type="text/javascript"> $.dialog.setting.zIndex =1990; function del(url,obj){ $.dialog.confirm("確認刪除該條記錄?", function(){ $.ajax({ async : false, cache : false, type : 'POST', url : url,// 請求的action路徑 error : function() {// 請求失敗處理函數 }, success : function(data) { var d = $.parseJSON(data); if (d.success) { var msg = d.msg; tip(msg); $(obj).closest("tr").hide("slow"); } } }); }, function(){ }); } </script> <div class="form" id="filediv"></div> <div class="form jeecgDetail"> <t:upload name="fiels" id="file_upload" extend="pic" buttonText="添加文件" formData="exprDelComId" uploader="exprDelComController.do?saveFiles"> </t:upload> </div> </td> </tr>
這邊增長了附件顯示和上傳附件的控件部分,其中t:upload這個標籤最主要的了,其中formData這個屬性是設置在上傳附件時須要一塊兒提交的表單中的其它屬性,通常只要提交業務實體的主鍵就能夠了,而uploader這個屬性對應的是上傳附件的連接