jquery.form.js使用实例

1、jsp页面

<%@ page language="java" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
	<%@include file="/pages/include/base-head.jsp"%>
    <script type="text/javascript" src="${commomSkin}/lib/jquery/jquery-form.js"></script>
  </head>
<body>
<form id="dataform" method="post" enctype="multipart/form-data">
<table style="align:center">
  <tbody>
     <tr>
        <td>
                        批量充值Excel模板:
        
            <img src="${skin}/image/flow/exceltemplate.png"/>
        </td>
     </tr>
     <tr><td>&nbsp;</td></tr>
     <tr>
        <td>
         <input type="file" name="batrechexcel"/>
        <input type="submit" value="确认批量充值" id="submit"/>
        </td>
     </tr>
     <tr><td>&nbsp;</td></tr>
     <tr id="shangjieguo" style="display:none">
        <td>充值结果:<span id="totalresult" style="color:red"></span>
        </td>
     </tr>
     <tr id="xiajieguo" style="display:none">
        <td>错误描述:<br/>
            <span id="errordes" style="color:red"></span>
            <span id="incompleteDataString" style="color:red"></span>
        </td>
     </tr>
  </tbody>
</table>
</form>
<script>
$(function(){   
    var options = {
        url:"${contextPath}/flow/public/batchRecharge",
        beforeSubmit: showRequest,  //提交前处理   
        success:  showResponse,  //处理完成   
        resetForm: true,    
        dataType:  'json'    
    };    
    
    $('#dataform').submit(function() {    
        $(this).ajaxSubmit(options); 
        return false;   //阻止表单默认提交 
    });    
});   
   
function showRequest(formData, jqForm, options) {    
	$("#submit").attr('disabled',true); 
	var batrechexcel = $("input[name='batrechexcel']").val();
	 if(batrechexcel == ""){
		 $("#shangjieguo").show();
		 $("#totalresult").text("请选择要导入的Excel文件");
		 $("#xiajieguo").css("display","none");
		 $("#submit").attr('disabled',false);
		 return false;
	 }else{
		 $("#shangjieguo").show();
		 $("#totalresult").text("充值进行中,请耐心等待...");
		 $("#xiajieguo").css("display","none");
		 return true;
	 }
}    
    
function showResponse(responseText, statusText)  {
	var str = responseText;
	if(str.result == "allsuccess"){
		$("#shangjieguo").show();
		$("#totalresult").text("全部受理成功:"+"总共充值数:"+str.totalNumber+" ,充值成功数:"+str.successNumber+" ,充值失败数:"+str.failNumber+" ,格式不对数:"+str.incompleteDataNumber);
	    window.parent.refreshLimit();
	}else if(str.result == "allfail"){
		$("#shangjieguo").show();
		$("#xiajieguo").show();
		$("#totalresult").text("全部受理失败:"+"总共充值数:"+str.totalNumber+" ,充值成功数:"+str.successNumber+" ,充值失败数:"+str.failNumber+" ,格式不对数:"+str.incompleteDataNumber);
		$("#errordes").html(str.failreason);
		$("#incompleteDataString").html(str.incompleteDataString);
	}else if(str.result == "null"){
		$("#shangjieguo").show();
		$("#totalresult").text("数据为空或数据大于1万");
	}else if(str.result == "partfail"){
		$("#shangjieguo").show();
		$("#xiajieguo").show();
		$("#totalresult").text("部分受理失败:"+"总共充值数:"+str.totalNumber+" ,充值成功数:"+str.successNumber+" ,充值失败数:"+str.failNumber+" ,格式不对数:"+str.incompleteDataNumber);
		$("#errordes").html(str.failreason);
		$("#incompleteDataString").html(str.incompleteDataString);
		window.parent.refreshLimit();
	}else if(str.result == "committwo"){
		$("#shangjieguo").show();
		$("#totalresult").text("请不要重复提交");
	}else if(str.result == "nomoney"){
		$("#shangjieguo").show();
		$("#totalresult").text(str.failreason);
	}else if(str.result == "geshibudui"){
		$("#shangjieguo").show();
		$("#totalresult").text("文件格式不对");
	}
	$("#submit").attr('disabled',false);
}   
</script>  
</body>
</html>

2、后台代码

@Controller
@RequestMapping(value="/flow")
public class BatchRechargeMainAction extends ModelAction<BatchRechargeMain> {

	@Autowired BatchRechargeMainService batReChMainService;
	@Autowired UserService userService;
	
	/**
	 * 跳到批量充值页面
	 * @return
	 * @author 
	 * @throws UnsupportedEncodingException 
	 */
	@RequestMapping(value="/public/batchRecharge",method=RequestMethod.GET)
	public String tobatchRecharge(HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException {
		return "flow/batchrecharge/batchrecharge";
	}
	
	/**
	 * 批量充值
	 * @param request
	 * @param response
	 * @return
	 * @author 
	 * @throws Exception 
	 */
	@RequestMapping(value="/public/batchRecharge",method=RequestMethod.POST)
	@ResponseBody
	public String batchRecharge(HttpServletRequest request, HttpServletResponse response) throws Exception{
		MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;  
	    MultipartFile multipartFile = multipartRequest.getFile("batrechexcel");  
	    String sourceName = multipartFile.getOriginalFilename(); // 原始文件名  
	    String geshi = sourceName.substring(sourceName.indexOf(".")+1);
	    if(!geshi.equals("xlsx")&&!geshi.equals("xls")){
	    	 Map<String, Object> totalResult = new HashMap<String, Object>();
			 totalResult.put("result", "geshibudui");
			 return JsonUtil.toJson(totalResult);
	    }
	    String base = request.getSession().getServletContext().getRealPath("/") + "batchrecharge" + File.separator + "uploadedExcel";  
	    File file = new File(base);  
	    if(!file.exists()){  
	       file.mkdirs();  
	    }   
	    String path=base + File.separator + sourceName;  
	    multipartFile.transferTo(new File(path));
	    Set<PhoneSize> phoneSizes = readBatchRechargeExcel(path);
	    String account = getLoginUser().getAccount();
	    String resultJson = this.batReChMainService.doBatchRecharge(phoneSizes,account);
	    return resultJson;
	}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值