最近在项目中用到Jquery,感觉真的不错,开源的插件也比较多。
项目中,我随手记录一些常用的方法。也是刚刚学习,有问题大家一起讨论,
希望能留下宝贵意见!
首先,在JQuery中AJAX请求一般有
$.post(url,data,callback) $.get(url,data,callback)
$.ajax (options)...
以post为例JSON数据处理方式:
1. html code:
<div id="export" style="position:absolute;z-index:auto;display: none"> <input id="exportPath" type="file"/> <input type="button" id="exportOK" value="OK"/> <input type="button" id="exportCancel" value="Cancel"/> <span>exporting...</span> </div>
2.jQuery code:
$('#OK').click( function(){ $('#import span').show(); $.post( path+"/importinfo/importFile.do", {filePath:$('#filePath').val()}, function(data) { if ("error" == data) { $('#import span').hide(); jAlert('warning','please select sdFile!','warning'); return; } if ("errornull" == data) { $('#import span').hide(); jAlert('error','the sdfile cannot be empty!','error'); return; } if ("errorimport" == data) { $('#import span').hide(); jAlert('error','import error!','error'); return; } $('#import').hide('normal'); var da =eval(data); var msg ='all num: ' + da[0]["all"] + '; imported num:' + da[0]["imported"] + '; failed num: '+ da[0]["failed"]; jAlert('success',msg,'success'); }) );
3.由post url执行的action. java code
@RequestMapping public void importFile(@RequestParam("filePath") String filePath, String isDuplicate, String isHaltOnError, String isEmpty, HttpServletResponse response) { logger.info("preview sdfile action...."); if (StringUtils.isEmpty(filePath) || !"sdf".equalsIgnoreCase(StringUtils.substringAfterLast( filePath, "."))) { MessageUtils.outputJSONResult("error", response); throw new IllegalArgumentException("cant not find sdf file."); } File inputFile = new File(filePath); if (!inputFile.isFile() || inputFile.length() <= 0) { MessageUtils.outputJSONResult("errornull", response); throw new IllegalArgumentException("file error!"); } ImporterCondition ic = new ImporterCondition(); ic.setAllowDuplicate(true); ic.setHaltOnError(true); ic.setEmptyStructuresAllowed(false); int[] rs = compoundService.batchRegister(inputFile, ic); if (rs[1] <= 0) { MessageUtils.outputJSONResult("errorimport", response); } Map<String, String> map = new HashMap<String, String>(3); map.put("all", String.valueOf(rs[1])); map.put("imported", String.valueOf(rs[0])); map.put("failed", String.valueOf(rs[2])); MessageUtils.outputJSONResult(JSONArray.fromObject(JSONObject.fromObject(map)).toString(), response);//create jsonArray } //MessageUtils.java public class MessageUtils { public static void outputJSONResult(String result, HttpServletResponse response) { try { response.setHeader("ContentType", "text/json"); response.setCharacterEncoding("utf-8"); PrintWriter pw = response.getWriter(); pw.write(result); pw.flush(); pw.close(); } catch (IOException e) { e.printStackTrace(); } } public static void outputXMLResult(String result, HttpServletResponse response) { try { response.setHeader("ContentType", "xml"); response.setCharacterEncoding("utf-8"); PrintWriter pw = response.getWriter(); pw.write(result); pw.flush(); pw.close(); } catch (IOException e) { e.printStackTrace(); } } }
XML处理方式:
1.html代码 (用的是jmesa第三包完成页面table的封装,它的功能很强大,正在学习中..