一、頁面
<td><input name="button" type="button" id="importButton" style="height: 25px; line-height: 25px; margin-top: 15px;" onClick = "checkForm()" value="點擊上傳"/> </td>
二、js
//導入 function checkForm(){var url = APP_PATH+'/import.do?method=import'; $("#dispatchForm").ajaxSubmit({ url:url, dataType:'html', success:function(data){ data=data.replace(/\r\n/g, ""); var retJson = eval("(" + data + ")"); if(typeof(retJson.success)!='undefined'){ //判斷導入校驗結果是成功還是失敗 importResult=retJson.success; if(retJson.text !=''){ $('#importDivInfo').css("display","block"); $('#importTable').html(""); $('#importTable').html(retJson.text); if("模板導入不正確,請重新導入"==retJson.text){ $('#exportInfo').css("display","none"); }else{ $('#exportInfo').css("display","block"); } }else{ alert("導入數據解析失敗"); } } }, error:function(ret){ if (top.jq) { top.Mask.unload(); } $('#importButton').attr("disabled",false) $("#importButton").attr("class","button_add"); ymPrompt.alert("上傳失敗!"); } });}
三、action
/** * @method 導入模板 * @param mapping * @param form * @param request * @param response * @throws Exception */ public void import(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) { BatchResultInfo info=null; JsonResponse resp = new JsonResponse(); try { FileForm fileForm = (FileForm) form; // 獲取選擇的模版文件 FormFile file = fileForm.getFile(); if(null!=file){ // 調用importData(File)執行導入操作 info = jkApproveChangeService.importJKApprove(file, applyType,rtUser); //最終是否校驗成功 boolean batchImport = true; if(null==info){ batchImport = false; resp.setSuccess(batchImport); }else{ StringBuffer htmlText=createHtmlTable(); resp.setText(htmlText.toString()); } } printHTML(response, resp.getJsonString()); } } catch (Exception e) { e.printStackTrace(); logger.error("importJKApprove(ActionMapping, ActionForm, HttpServletRequest, HttpServletResponse)", e); }catch (Exception e) { logger.error("importJKApprove(ActionMapping, ActionForm, HttpServletRequest, HttpServletResponse)", e); } }
/** * 返回頁面生成table展示出來 * @param successList * @return */ private StringBuffer createHtmlTable(){ StringBuffer htmlText=new StringBuffer(); htmlText.append("<table border='1' class='tableborder'><tr>"); htmlText.append("<td>校驗結果</td></tr>"); htmlText.append("</table>"); return htmlText; }
四、FileForm:
import org.apache.struts.action.ActionErrors; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionMapping; import org.apache.struts.action.ActionMessage; import org.apache.struts.upload.FormFile; import javax.servlet.http.HttpServletRequest; public class FileForm extends ActionForm { private static final long serialVersionUID = 1L; /** * 導入文件 */ private FormFile file ; public FormFile getFile() { return file; } public void setFile(FormFile file) { this.file = file; } public ActionErrors validate(ActionMapping actionMapping, HttpServletRequest request) { ActionErrors errors = new ActionErrors(); ActionMessage message = new ActionMessage(""); if (null == this.file) { errors.add("file", message); } return errors; } public void reset(ActionMapping mapping, HttpServletRequest request) { try { request.setCharacterEncoding("utf-8"); } catch (Exception e) { } } }
struts.xml
<action input="" path="/import" scope="request" name="FileForm" parameter="method" type="com.demo.ImportAction"> </action>