jquery form插件是用于在页面使用ajax上传表单、文件,不刷新

来源:互联网 发布:wnba数据 编辑:程序博客网 时间:2024/06/06 21:45

jquery form插件是用于在页面使用ajax上传表单、文件,不刷新的功能,直接开始例子:

 

有两种方式: ajaxForm 和ajaxSubmit ,前者直接在文档加载完成之后绑定到form上即可,在页面提交的时候会自动触发里面的处理,后者表示手动调用插件的ajax提交时机,可以在form提交的时候触发ajaxSubmit,也可以在一个button的click事件里面触发ajaxSubmit

 

先使用ajaxSubmit:

 

页面:

 

Html代码  收藏代码
  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>  
  2. <%  
  3. String path = request.getContextPath();  
  4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
  5. %>  
  6.   
  7. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  8. <html>  
  9.   <head>  
  10.     <base href="<%=basePath%>">  
  11.       
  12.     <title>My JSP 'upload.jsp' starting page</title>  
  13.       
  14.     <meta http-equiv="pragma" content="no-cache">  
  15.     <meta http-equiv="cache-control" content="no-cache">  
  16.     <meta http-equiv="expires" content="0">      
  17.     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">  
  18.     <meta http-equiv="description" content="This is my page">  
  19.     <script type="text/javascript" src="<%=basePath %>common/js/jquery-1.8.2.min.js"></script>  
  20.     <script type="text/javascript" src="<%=basePath %>common/js/jquery.form.js"></script>  
  21.     <script type="text/javascript">  
  22.         $(document).ready(function() {   
  23.             */  
  24.             //在form提交的时候触发,这个触发的时机是自己控制的,例如也可以在一个button的click事件里面触发ajaxSubmit  
  25.             $("#myForm").submit(function(){  
  26.                 $('#myForm').ajaxSubmit({   
  27.                     beforeSubmit:handleBeforeSubmit,  
  28.                     success:handleSuccess  
  29.                 });   
  30.                 //记得返回false,阻止页面的默认提交行为  
  31.                 return false;  
  32.             });  
  33.         });  
  34.         /**  
  35.          * 提交请求发出之前的处理  
  36.          */  
  37.         function handleBeforeSubmit(){  
  38.             console.info('提交请求发出之前的处理')  
  39.         }  
  40.         /**  
  41.          * 上传成功后的处理  
  42.          */  
  43.         function handleSuccess(){  
  44.             console.info('上传成功后的处理')  
  45.             alert('上传成功 !');  
  46.         }  
  47.     </script>  
  48.   </head>  
  49.   <body>  
  50.     <form action="upload/uploadFile.htm" method="post" enctype="multipart/form-data" id="myForm">  
  51.       <label for="myFile">选择文件:</label>  
  52.       <input type="file" name="myFile" />  
  53.       <input type="hidden" name="myfield" value="myvalue">  
  54.       <input type="submit" value="上传"/>  
  55.    </form>  
  56.   </body>  
  57. </html>  

 

或者:

 

Html代码  收藏代码
  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>  
  2. <%  
  3. String path = request.getContextPath();  
  4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
  5. %>  
  6.   
  7. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  8. <html>  
  9.   <head>  
  10.     <base href="<%=basePath%>">  
  11.       
  12.     <title>My JSP 'upload.jsp' starting page</title>  
  13.       
  14.     <meta http-equiv="pragma" content="no-cache">  
  15.     <meta http-equiv="cache-control" content="no-cache">  
  16.     <meta http-equiv="expires" content="0">      
  17.     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">  
  18.     <meta http-equiv="description" content="This is my page">  
  19.     <script type="text/javascript" src="<%=basePath %>common/js/jquery-1.8.2.min.js"></script>  
  20.     <script type="text/javascript" src="<%=basePath %>common/js/jquery.form.js"></script>  
  21.     <script type="text/javascript">  
  22.         $(document).ready(function() {   
  23.             $('#myForm').ajaxForm({   
  24.                 beforeSubmit:handleBeforeSubmit,  
  25.                 success:handleSuccess  
  26.             });   
  27.         });  
  28.         /**  
  29.          * 提交请求发出之前的处理  
  30.          */  
  31.         function handleBeforeSubmit(){  
  32.             console.info('提交请求发出之前的处理')  
  33.         }  
  34.         /**  
  35.          * 上传成功后的处理  
  36.          */  
  37.         function handleSuccess(){  
  38.             console.info('上传成功后的处理')  
  39.             alert('上传成功 !');  
  40.         }  
  41.     </script>  
  42.   </head>  
  43.   <body>  
  44.     <form action="upload/uploadFile.htm" method="post" enctype="multipart/form-data" id="myForm">  
  45.       <label for="myFile">选择文件:</label>  
  46.       <input type="file" name="myFile" />  
  47.       <input type="hidden" name="myfield" value="myvalue">  
  48.       <input type="submit" value="上传"/>  
  49.    </form>  
  50.   </body>  
  51. </html>  

 

 

都是一样的效果,后台action(这里使用struts2)

 

struts.xml配置:

 

Xml代码  收藏代码
  1.               <!-- 处理文件上传 -->  
  2. <action name="uploadFile" class="uploadFileAction" method="uploadFile">  
  3. </action>  

 

后台Action:

 

Java代码  收藏代码
  1. package com.tch.test.template.web.action;  
  2.   
  3. import java.io.File;  
  4. import org.apache.commons.io.FileUtils;  
  5. import org.springframework.context.annotation.Scope;  
  6. import org.springframework.stereotype.Component;  
  7.   
  8. import java.io.IOException;  
  9.   
  10. import com.opensymphony.xwork2.ActionSupport;  
  11.   
  12. @Component("uploadFileAction")  
  13. @Scope("prototype")  
  14. public class UploadFile extends ActionSupport {  
  15.     private static final long serialVersionUID = 1L;  
  16.     private File myFile;  
  17.     private String myFileContentType;  
  18.     private String myFileFileName;  
  19.     private String destPath;  
  20.     private String myfield;  
  21.   
  22.     public void uploadFile() {  
  23.         /* Copy file to a safe location */  
  24.         destPath = "e:/";  
  25.   
  26.         try {  
  27.             System.out.println("Src File name: " + myFile);  
  28.             System.out.println("Dst File name: " + myFileFileName);  
  29.             System.out.println("myfield : "+myfield);  
  30.   
  31.             File destFile = new File(destPath+myFileFileName);  
  32.             FileUtils.copyFile(myFile, destFile);  
  33.   
  34.         } catch (IOException e) {  
  35.             e.printStackTrace();  
  36.         }  
  37.     }  
  38.   
  39.     public File getMyFile() {  
  40.         return myFile;  
  41.     }  
  42.   
  43.     public void setMyFile(File myFile) {  
  44.         this.myFile = myFile;  
  45.     }  
  46.   
  47.     public String getMyFileContentType() {  
  48.         return myFileContentType;  
  49.     }  
  50.   
  51.     public void setMyFileContentType(String myFileContentType) {  
  52.         this.myFileContentType = myFileContentType;  
  53.     }  
  54.   
  55.     public String getMyFileFileName() {  
  56.         return myFileFileName;  
  57.     }  
  58.   
  59.     public void setMyFileFileName(String myFileFileName) {  
  60.         this.myFileFileName = myFileFileName;  
  61.     }  
  62.   
  63.     public String getMyfield() {  
  64.         return myfield;  
  65.     }  
  66.   
  67.     public void setMyfield(String myfield) {  
  68.         this.myfield = myfield;  
  69.     }  
  70.       
  71. }  

 

OK。

0 0