淺談struts1 & jquery form 文件異步上傳
1.概述
還在用struts1?是的,在地球的沒寫地方,落后的生產(chǎn)方式還在運轉(zhuǎn)(老項目).
從 繼承org.apache.struts.action.Action, 繼承org.apache.struts.action.ActionForm開始吧
2. 代碼
2.1 html頁面
<html> <head> <title>網(wǎng)頁上傳</title> </head> <body> <center> <h1>本地文件網(wǎng)頁上傳</h1> <hr> </center> <h1>文件信息列表</h1> <hr> <form id="myform" method="post" enctype="multipart/form-data"> <table width="0" border="0" cellspacing="10" cellpadding="0"> <tr> <td>選擇文件:</td> <td><input type="file" name="uploadFile" /></td> </tr> <tr> <td>標(biāo)題:</td> <td><input type="text" name="filetitle" /></td> </tr> <tr> <td colspan="2"> <input type="button" id="mysubmit" value="確認(rèn)上傳"/> </td> </tr> </table> </form> <script src="script/jquery.js"></script> <script src="script/jquery.form.js"></script> <script src="script/_fileUpload.js"></script> </body> </html>
2.2 _fileUpload.js
/** *_fileUpload.js * * */ window.onload = function() { $("#mysubmit").bind("click", function(){ $("#myform").ajaxSubmit({ url: "myUpload.do", type: "post", success: function(data){ console.log(11111111); console.log(data); }, error: function(responseError){ console.log(22222222222); console.log(responseError); } }); }); }
2.3 MyUploadAction.java(繼承自Action)
package com.rocky.console.action; import java.io.File; import java.io.FileOutputStream; import java.io.InputStream; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.struts.action.Action; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; import org.apache.struts.upload.FormFile; import com.rocky.console.form.MyUploadForm; import com.rocky.console.service.ResponseUtil; public class MyUploadAction extends Action { public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception{ MyUploadForm myUploadForm = (MyUploadForm) form; FormFile uploadFile = myUploadForm.getUploadFile(); String filetitle = myUploadForm.getFiletitle(); System.out.println("111111"+filetitle); int fileSize = uploadFile.getFileSize(); InputStream inputStream = uploadFile.getInputStream(); System.out.println("fileSize::::::::"+fileSize); String path = "x:"; String filename = path + File.separator + uploadFile.getFileName(); FileOutputStream fos = new FileOutputStream(filename); byte[] b = new byte[1024]; int len = -1; while((len = inputStream.read(b))!=-1){ fos.write(b, 0, len); } fos.close(); String result = "success"; ResponseUtil.write(response, result, null); return null; } }
2.4 MyUploadForm.java( 繼承自ActionForm)
package com.rocky.console.form; import javax.servlet.http.HttpServletRequest; import org.apache.struts.action.ActionErrors; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionMapping; import org.apache.struts.upload.FormFile; public class MyUploadForm extends ActionForm { /** * */ private static final long serialVersionUID = 6650496540449458586L; private FormFile uploadFile = null; private String filetitle; public String getFiletitle() { return filetitle; } public void setFiletitle(String filetitle) { this.filetitle = filetitle; } public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) { return null; } public void reset(ActionMapping mapping, HttpServletRequest request) { } public FormFile getUploadFile() { return uploadFile; } public void setUploadFile(FormFile uploadFile) { this.uploadFile = uploadFile; } }
2.5 struts-config.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "struts-config_1_2.dtd"> <struts-config> <data-sources /> <form-beans> <form-bean name="myUploadForm" type="com.rocky.console.form.MyUploadForm" /> </form-beans> <global-exceptions /> <global-forwards /> <action-mappings> <!-- rocky myupload --> <action path="/myUpload" attribute="myUploadForm" name="myUploadForm" type="com.rocky.console.action.MyUploadAction" /> </action-mappings> <message-resources parameter="ApplicationResources" /> </struts-config>
2.6 說明
2.6.1 jquery.form.js 網(wǎng)上可以下載
使用var formData = new FormData(), 然后formData.append("myfile", document.getElementById("myfile").files[0]);form.append...
當(dāng)form表單field較多時 寫很多 append很麻煩, 顯然 ajaxSubmit方便很多
2.6.2 前端過來的數(shù)據(jù) 通過 ActionForm 直接封裝到其子類(MyActionForm)對象中 , 用FormFile接收file文件 用String等接收其他類型數(shù)據(jù)
當(dāng)然都是根據(jù)HTML 標(biāo)簽的name屬性一一對應(yīng) 來注入的
2.6.3 ActionForm是怎么和自定義實現(xiàn)的bean(MyUploadForm) 對上的?
在struts-config.xml中form-bean設(shè)置自己的那個bean,通過<action path="/myUpload" attribute="myUploadForm" name="myUploadForm"
來完成這種映射
以上這篇淺談struts1 & jquery form 文件異步上傳就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
jQuery學(xué)習(xí)筆記之jQuery動畫效果
本次學(xué)習(xí)分為兩個文件的測試,第一個是基本動畫,第二個是滑動和透明動畫效果,分別如下2013-09-09jQuery操作Select選擇的Text和Value(獲取/設(shè)置/添加/刪除)
本文將詳細(xì)介紹下jQuery獲取/設(shè)置/添加/刪除Select選擇的Text和Value,感興趣的你可以參考下本文或許對你有所幫助2013-03-03jQuery實現(xiàn)將div中滾動條滾動到指定位置的方法
這篇文章主要介紹了jQuery實現(xiàn)將div中滾動條滾動到指定位置的方法,涉及jQuery結(jié)合animate方法動態(tài)操作頁面元素屬性的相關(guān)技巧,需要的朋友可以參考下2016-08-08Django中使用jquery的ajax進(jìn)行數(shù)據(jù)交互的實例代碼
這篇文章主要介紹了Django中使用jquery的ajax進(jìn)行數(shù)據(jù)交互的相關(guān)知識,非常不錯,具有參考借鑒價值 ,需要的朋友可以參考下2017-10-10