ajax setup过滤机制,获取后台的运行时异常(RuntimeException)做提示信息
//运行是异常
ajaxSetup是ajax的一个拦截器,可以拦截表单的提交,也可以拦截后台的运行时异常,做一些提示弹框
ajaxSetup只会补做运行时异常
利用ajax的setup控制将系统的错误信息弹出到页面
timeout.js
$(function(){ //解决ajax超时问题bywanghao2013-10-31begin varisChecked=false; varisCheckedValue=null; varisTimeout=false; //这个可以单独的放到另外一个js varshowExceptionInfo=function(msg){ showWindow("exceptionWindow","错误提示"); $("#exceptionWindow.ball_layer_pointout06").text("系统服务异常,异常信息:"+msg); }; $.ajaxSetup({ "cache":false, "complete":function(XMLHttpRequest,textStatus){ //在每个ajax请求后进行删除权限验证 if(XMLHttpRequest.responseText=="IS_CAN_DELETE_TRUE"||XMLHttpRequest.responseText=="IS_CAN_DELETE_FALSE"){ //如果本身已经获取到删除权限,不再继续验证,从而避免死循环 return; } }, "error":function(textStatus,errorThrown){ if(textStatus.status==200){ return; } unDisplayProgress(); //全局处理异常,所有的业务代码都不再需要手动处理error!!!bywanghao2013-12-3 vartext=textStatus.responseText; varstart=text.indexOf("<h1>"); varend=text.indexOf("</h1>"); varerror=text.substring(start+4,end); if(error=="TIME_OUT"&&!isTimeout){ isTimeout=true; alert("操作超时,为保证系统安全,请重新登录,谢谢!"); window.location=basePath; return; }elseif(!error){ //console.log("后台异常,但是无异常信息,不影响系统功能!"+text); }else{ showExceptionInfo(error); } } }); varrenderDeleteButton=function(isCanDelete){ //console.log(newDate()+"-->"+isCanDelete); if(isCanDelete=="IS_CAN_DELETE_TRUE"){ $("link[title='extra']").attr("disabled","disabled"); $(".del_report_btn").css("display","inline-block"); $(".table_delete_btn").css("display","inline-block"); $(".del_btn").css("display","inline-block"); $(".table_delete_dis_btn").css("display","inline-block"); }elseif(isCanDelete=="IS_CAN_DELETE_FALSE"){ $("link[title='extra']").removeAttr("disabled"); $(".del_report_btn").remove(); $(".table_delete_btn").remove(); $(".del_btn").remove(); $(".table_delete_dis_btn").remove(); } }; //立即执行删除权限验证 //checkIsCanDelete(); });
jsp:
这个错误消息框可以在simesh中配置,装饰到每一个页面
<divid="exceptionWindow"class="ball_boxball_box06"style="display:none;z-index:99999999;width:290px;"> <divclass="bg_boder"> <h2> <ahref="#"class="del"></a>错误提示 </h2> <divclass="ball_layer_pointout06"></div> <divclass="ball_btn_box"> <center> <ahref="javascript:void(0)"class="ball_btn_true">确定</a> </center> </div> </div> </div> //后台处理时抛出运行是异常即可 @Service("overviewService") publicclassOverviewServiceImplimplementsIOverviewService{ privateLoggerlogger=Logger.getLogger(OverviewServiceImpl.class); @Override publicSummeryOfResourcesVOgetSummeryOfResource(){ logger.info("查询一组资源使用情况(云主机、虚拟内核、虚拟内存、安全组)信息"); finalSummeryOfResourcesVOvo=newSummeryOfResourcesVO(); finalJSONObject[]provider=newJSONObject[1]; finalString[]reslt=newString[1]; /**取资源使用情况*/ HttpClientRequestrequest=newHttpClientGetRequest(ConfigFileLoad.getConfContent("API_IP")+"/rest/resource_used"); try{ request.process(newHttpClientResponseHandler(){ @Override publicvoidhandle(Stringresponse,StringretCode,Stringmsg)throwsHttpClientException,ManoException{ logger.debug("查询一组资源使用情况信息,返回状态:"+retCode+",返回信息:"+msg); JSONObjectobj=JSONObject.parseObject(response); if(obj.get("result")!=null){ System.out.println(obj.get("result").toString()); JSONObjectjsonObj=JSONObject.parseObject(obj.get("result").toString()); if(jsonObj==null){ //&&!reslt[0].equals("0") return; } //一、请求并计算云主机信息 Integervms=jsonObj.getInteger(Constants.OverView.VMS); Integerused_vms=jsonObj.getInteger(Constants.OverView.USED_VMS); Integerunused_vms=0; if(vms==null||vms.equals("")){ vms=0; }elseif(used_vms==null||used_vms.equals("")){ used_vms=0; }else{ unused_vms=vms-used_vms; } vo.setCloudPlatformAll(String.valueOf(vms)); vo.setCloudPlatformUsed(String.valueOf(used_vms)); vo.setCloudPlatformUnUsed(String.valueOf(unused_vms)); //二、请求虚拟内核 Integercpus=jsonObj.getInteger(Constants.OverView.CPUS); Integerused_cpus=jsonObj.getInteger(Constants.OverView.USED_CPUS); Integerunused_cpus=0; if(cpus==null||cpus.equals("")){ cpus=0; }elseif(used_cpus==null||used_cpus.equals("")){ used_cpus=0; }else{ unused_cpus=cpus-used_cpus; } vo.setVirtualKernelAll(String.valueOf(cpus)); vo.setVirtualKernelUsed(String.valueOf(used_cpus)); vo.setVirtualKernelUnUsed(String.valueOf(unused_cpus)); //三、请求虚拟内存 Integermems=jsonObj.getInteger(Constants.OverView.MEMS); Integerused_mem=jsonObj.getInteger(Constants.OverView.USED_MEM); Integerunused_mem=0; if(mems==null||mems.equals("")){ mems=0; }elseif(used_mem==null||used_mem.equals("")){ used_mem=0; }else{ unused_mem=mems-used_mem; } vo.setVirtualMemoryAll(String.valueOf(mems)); vo.setVirtualMemoryUsed(String.valueOf(used_mem)); vo.setVirtualMemoryUnUsed(String.valueOf(unused_mem)); //四、请求安全组 Integersecurity_group=jsonObj.getInteger(Constants.OverView.SECURITY_GROUP); Integerused_security_group=jsonObj.getInteger(Constants.OverView.USED_SECURITY_GROUP); Integerunused_security_group=0; if(security_group==null||security_group.equals("")){ security_group=0; }elseif(used_security_group==null||used_security_group.equals("")){ used_security_group=0; }else{ unused_security_group=security_group-used_security_group; } vo.setSecurityGroupAll(String.valueOf(security_group)); vo.setSecurityGroupUsed(String.valueOf(used_security_group)); vo.setSecurityGroupUnUsed(String.valueOf(unused_security_group)); }else{ /**provider连接数*/ HttpClientRequestrequestProvider=newHttpClientGetRequest(ConfigFileLoad.getConfContent("API_IP")+ "/rest/provider_network_cn"); try{ requestProvider.process(newHttpClientResponseHandler(){ @Override publicvoidhandle(Stringresponse,StringretCode,Stringmsg)throwsHttpClientException{ logger.debug("查询一组资源使用情况信息,返回状态:"+retCode+",返回信息:"+msg); provider[0]=JSONObject.parseObject(response); System.out.println(provider[0].get("result").toString()+"rrrrrrrrrrrrr"); StringjsonObj=provider[0].get("result").toString(); reslt[0]=jsonObj; if(jsonObj==null) return; } }); }catch(HttpClientExceptione){ e.printStackTrace(); } //////////////////ManoException是运行是异常 if(reslt[0].equals("0")){ thrownewManoException("0","没建立与openstack的连接,请检查openstack资源中openstack提供者是否已添加"); }else{ thrownewManoException("1","没有数据"); } //thrownewHttpClientException("没建立连接"); } }}); }catch(HttpClientExceptione){ thrownewManoException("1","没建立连接"); } logger.info("SummeryOfResourcesVOis"+vo.toString()); returnvo; } } //运行时异常 packagecom.certusnet.nfv.mano.exception; importjava.util.Locale; importjavax.ws.rs.ext.Provider; importorg.springframework.web.context.ContextLoaderListener; /** *ManoException,统一处理异常编码和异常信息 *@authorBiwei * */ @Provider publicclassManoExceptionextendsRuntimeException{ publicstaticfinallongserialVersionUID=11111111111L; publicManoException(Throwablethrowable){ super(throwable); } publicManoException(Stringcode,Stringmsg){ super(msg); this.code=code; } /** *messagekey */ privateStringcode; /** *messageparams */ privateObject[]params; publicStringgetCode(){ returncode; } publicvoidsetCode(Stringcode){ this.code=code; } publicObject[]getParams(){ returnparams; } publicvoidsetParams(Object[]params){ this.params=params; } }
相关推荐
kentrl 2020-11-10
结束数据方法的参数,该如何定义?-- 集合为自定义实体类中的结合属性,有几个实体类,改变下标就行了。<input id="add" type="button" value="新增visitor&quo
ajaxyan 2020-11-09
zndy0 2020-11-03
学留痕 2020-09-20
Richardxx 2020-11-09
learningever 2020-09-19
chongxiaocheng 2020-08-16
ajaxhe 2020-08-16
lyqdanang 2020-08-16
curiousL 2020-08-03
TONIYH 2020-07-22
时光如瑾雨微凉 2020-07-19
83510998 2020-07-18
坚持着执着 2020-07-16
jiaguoquan00 2020-07-07
李永毅 2020-07-05
坚持着执着 2020-07-05