salouhim 发表于 2013-1-15 08:00:48

dwr把JSON对象从界面带到后台处理

1:在界面把数据封装成JSON字符窜: 格式如下:
 
     {"a":"1","b":"2","c":[{"d":"3","e":"4"},{"d":"3","e":"4"}]}
 
     界面js如下:
 
    
   //一个字符串数据function getJsons(){var zlf = checkCongfu("zhonglei");if(!zlf){return "";}var sfxm = document.getElementById("sfxm").value;if(sfxm==2){var fzxamt = document.getElementById("fzxamt").value;var zxel = document.getElementsByName("xnamt");var ed = document.getElementById("namt").value;var fpsum = 0;for(var i=0;i<zxel.length;i++){if(zxel.value!=""){fpsum = fpsum + parseFloat(getNum(zxel.value));}}fpsum = fpsum+parseFloat(getNum(fzxamt));if(parseFloat(getNum(ed))<fpsum){alert("调整额度小于分配额度");return "";}}var mx = document.getElementsByName("dbxz");var namt = document.getElementById("namt");var allused = document.getElementById("usedamt");var danbao = document.getElementsByName("danbao");var zhonglei = document.getElementsByName("zhonglei");var odi = document.getElementById("oid");var appid = document.getElementById("appid");if(namt.value!=""){var r = new StringBuffer('{ \"namt\":\"');var tzsm = document.getElementById("othersVal"); r.append(parseFloat(getNum((namt.value)))*10000);r.append('\",\"oid\":\"');r.append(odi.value);r.append('\",\"tzsm\":\"');r.append((tzsm.value));r.append('\",\"cor\":\"');r.append('<%=cror%>');r.append('\",\"appid\":\"');r.append(appid.value);r.append('\",\"allused\":\"');r.append(0);r.append("\"");if(sfxm==2){r.append(',\"mx\":');var tempr =new StringBuffer("[");var zhonglei = document.getElementsByName("zhonglei");//var jyed = parseFloat(getNum(document.getElementsByName("xnamt")))*10000;var jyed = document.getElementsByName("xnamt")var danbao = document.getElementsByName("danbao");var danbaosm = document.getElementsByName("xnwartcmt");var f = false;tempr.append('{\"zhonglei\":\"');tempr.append(100);tempr.append('\", \"xnamt\":\"');tempr.append(parseFloat(getNum(document.getElementById("fzxamt").value))*10000);tempr.append('\", \"usedamt\":\"');tempr.append(parseFloat(getNum(document.getElementById("fzxamt").a))*10000);tempr.append('\",\"danbao\":\"');tempr.append(danbao.value);tempr.append('\",\"danbaosm\":\"');tempr.append(document.getElementById("fzxnwartcmt").value);tempr.append('\"},');if(danbao.value==""){alert("请填写担保方式!");danbao.focus;return "";}for(var i=0 ; i<mx.length;i++){//if(mx.checked==true || jyed.value!=""){//alert(jyed.value);if(jyed.value!=""){if(jyed.value==""){alert("请填写额度!");jyed.focus;return "";}if(danbao.value==""){alert("请填写担保方式!");danbao.focus;return "";}if(zhonglei.value==""){alert("请填写授信种类!");zhonglei.focus;return "";}tempr.append('{\"zhonglei\":\"');tempr.append(zhonglei.value);tempr.append('\", \"xnamt\":\"');tempr.append(parseFloat(getNum(jyed.value))*10000);tempr.append('\", \"usedamt\":\"');//if(jyed.a!=null && jyed.a!=""){//tempr.append(parseFloat(getNum(jyed.a))*10000);//}else{tempr.append(0);//}tempr.append('\",\"danbao\":\"');tempr.append(danbao.value);tempr.append('\",\"danbaosm\":\"');tempr.append(danbaosm.value);tempr.append('\"},');f = true;}else{//alert("请填写额度!");//jyed.focus;//return "";}}tempr =tempr.substringBetween(0,tempr.getLength()-1);tempr=tempr+"]";r.append(tempr);      }      r.append("}");return r;}else{return "";}} 2:界面把JSON传到后台
     
function dosave(){ var r = getJsons(); if(r!=""){document.getElementById("dos").disabled =true ;document.getElementById("dob").disabled = true ;    cedit.saveFbcmcEditApp(r.toString(),chagexnAmt);}} 3:后台接收JSON并处理:
 
  
public String saveFbcmcEditApp(String app){String s ="";//String s = fbcmCeditAuditDao.saveFbcmcEditApp(app);JSONObject t= JSONObject.fromObject(app);String namt = t.getString("namt");String oid = t.getString("oid");String cor = t.getString("cor");String allused = t.getString("allused");//String mx = t.getString("mx");//this.getDaoFacade().getCommonDao().delete(object)FbcmCeditApp oidFbcmCeditApp = getDaoFacade().getFbcmCeditAuditDao().checkAmt(oid, namt);if(oidFbcmCeditApp==null){return ("调整金额小于占有金额");}if(oidFbcmCeditApp.getSuperclt()!=null){FbcmCeditApp apps =getDaoFacade().getFbcmCeditAuditDao().getFbcmCeditAppByCltno(oidFbcmCeditApp.getSuperclt());if(apps!=null && apps.getAmt().doubleValue() < Double.parseDouble(namt)){return ("调整金额大于上级可用额度");}}Set oset = oidFbcmCeditApp.getFbcmCrtappDetails();//页面要保存的明细if(oidFbcmCeditApp.getIsproject().equals(new Long(String.valueOf(Statics.P_NO)))){String mx = t.getString("mx");JSONArray ja= JSONArray.fromObject(mx);//Set mxs = new HashSet();for(int i =0 ; i<ja.size();i++){JSONObject tempmx = (JSONObject)ja.get(i);String zl =tempmx.getString("zhonglei");String ed =tempmx.getString("xnamt");Iterator iter =oset.iterator();while(iter.hasNext()){FbcmCrtappDetail fcd = (FbcmCrtappDetail)iter.next();if(fcd.getType().equals(zl)){if(fcd.getUsedamt()!=null &&!"".equals(fcd.getUsedamt())){System.out.println(fcd.getUsedamt().doubleValue() +"@@@"+Double.parseDouble(ed));if(fcd.getUsedamt().doubleValue() > Double.parseDouble(ed)){if("100".equals(zl)){return ("非专项调整金额小于占有金额");}else{String name =getDaoFacade().getFbcmCeditAuditDao().getSxName(zl);return (name+"调整金额小于占有金额");}}}}}}}//删除相关的调整的明细List ll = getDaoFacade().getFbcmCeditAuditDao().findSqsms(Long.valueOf(oid));if(ll!=null){for(int i=0; i<ll.size();i++){FbcmAppDetail ttt = (FbcmAppDetail)ll.get(i);this.getDaoFacade().getCommonDao().delete(ttt);}}//删除相关的调整的说明//List lll = getDaoFacade().getFbcmCeditAuditDao().findTzmx(Long.valueOf(oid));List lll = getDaoFacade().getFbcmCeditAuditDao().findTzmx(oidFbcmCeditApp.getOldid());if(lll!=null){for(int i=0; i<lll.size();i++){FbcmCrtappDetail ttt = (FbcmCrtappDetail)lll.get(i);this.getDaoFacade().getCommonDao().delete(ttt);}}////删除相关的调整List l = getDaoFacade().getFbcmCeditAuditDao().getEditAppByOldId(oidFbcmCeditApp.getOldid().toString());//getDaoFacade().getFbcmCeditAuditDao().deleteAuditEditApp(l);if(l!=null){for(int i=0; i<l.size();i++){FbcmCeditApp ttt = (FbcmCeditApp)l.get(i);this.getDaoFacade().getCommonDao().delete(ttt);}}//保存初始申请的调整系数。FbcmCeditApp yssx =(FbcmCeditApp)this.getDaoFacade().getCommonDao().findById(oidFbcmCeditApp.getOldid(), FbcmCeditApp.class);oidFbcmCeditApp.setLoadadjrate(yssx.getLoadadjrate());FbcmCeditApp temp = getDaoFacade().getFbcmCeditAuditDao().saveFbcmCeditApp(oidFbcmCeditApp, namt,allused, oid, cor, t);FbcmCeditApp ysq = getDaoFacade().getFbcmCeditAuditDao().getAppById(oidFbcmCeditApp.getOldid().toString());getDaoFacade().getFbcmCeditAuditDao().saveFbcmAppDetail(ysq, t);s="保存成功;"+temp.getId();return s;} 
页: [1]
查看完整版本: dwr把JSON对象从界面带到后台处理