在处理表单类数据的时候,之前处理过一个表单里面有20条类似的数据,针对这些数据都是通过一个一个的变量进行输入的,一条数据有5个值,20条数据100个值,在controller中的参数中设置100个参数,哦,my god,好让人崩溃,但是确实就这么干了一回,下面是写的参数:
@RequestMapping("/baojia/savebjxx") @ResponseBody public Map<String,Object> savebjxx(@RequestParam("sffg") String sffg,@RequestParam("syxqbrq") String syxqbrq,@RequestParam("jqxqbrq") String jqxqbrq,@RequestParam("jqxbf") String jqxbf,@RequestParam("ccs") String ccs,@RequestParam("jqxqnzbrq") String jqxqnzbrq,@RequestParam("jqxdqts") String jqxdqts, @RequestParam("clssxischoice") String clssxischoice,@RequestParam("clssxbe") double clssxbe,@RequestParam("clssxbf") double clssxbf,@RequestParam("clssxisvalid") String clssxisvalid,@RequestParam("clssxisvote") String clssxisvote, @RequestParam("qcdqischoice") String qcdqischoice,@RequestParam("qcdqxbe") double qcdqxbe,@RequestParam("qcdqxbf") double qcdqxbf,@RequestParam("qcdqxisvalid") String qcdqxisvalid,@RequestParam("qcdqxisvote") String qcdqxisvote, @RequestParam("sydszzrischoice") String sydszzrischoice,@RequestParam("sydszzrxbe") double sydszzrxbe,@RequestParam("sydszzrxbf") double sydszzrxbf,@RequestParam("sydszzrxisvalid") String sydszzrxisvalid,@RequestParam("sydszzrxisvote") String sydszzrxisvote, @RequestParam("sjzwzrxischoice") String sjzwzrxischoice,@RequestParam("sjzwzrxbe") double sjzwzrxbe,@RequestParam("sjzwzrxbf") double sjzwzrxbf,@RequestParam("sjzwzrxisvalid") String sjzwzrxisvalid,@RequestParam("sjzwzrxisvote") String sjzwzrxisvote, @RequestParam("ckzwzrxischoice") String ckzwzrxischoice,@RequestParam("ckzwzrxbe") double ckzwzrxbe,@RequestParam("ckzwzrxbf") double ckzwzrxbf,@RequestParam("ckzwzrxisvalid") String ckzwzrxisvalid,@RequestParam("ckzwzrxisvote") String ckzwzrxisvote, @RequestParam("cshhsxischoice") String cshhsxischoice,@RequestParam("cshhsxbe") double cshhsxbe,@RequestParam("cshhsxbf") double cshhsxbf,@RequestParam("cshhsxisvalid") String cshhsxisvalid,@RequestParam("cshhsxisvote") String cshhsxisvote, @RequestParam("blddpsxischoice") String blddpsxischoice,@RequestParam("blddpsxbe") double blddpsxbe,@RequestParam("blddpsxbf") double blddpsxbf,@RequestParam("blddpsxisvalid") String blddpsxisvalid,@RequestParam("blddpsxisvote") String blddpsxisvote, @RequestParam("ssxischoice") String ssxischoice,@RequestParam("ssxbe") double ssxbe,@RequestParam("ssxbf") double ssxbf,@RequestParam("ssxisvalid") String ssxisvalid,@RequestParam("ssxisvote") String ssxisvote, @RequestParam("zrssxischoice") String zrssxischoice,@RequestParam("zrssxbe") double zrssxbe,@RequestParam("zrssxbf") double zrssxbf,@RequestParam("zrssxisvalid") String zrssxisvalid,@RequestParam("zrssxisvote") String zrssxisvote, @RequestParam("dcjcdddsssischoice") String dcjcdddsssischoice,@RequestParam("dcjcdddsssbe") double dcjcdddsssbe,@RequestParam("dcjcdddsssbf") double dcjcdddsssbf,@RequestParam("dcjcdddsssisvalid") String dcjcdddsssisvalid,@RequestParam("dcjcdddsssisvote") String dcjcdddsssisvote, @RequestParam("dsftyxischoice") String dsftyxischoice,@RequestParam("dsftyxbe") double dsftyxbe,@RequestParam("dsftyxbf") double dsftyxbf,@RequestParam("dsftyxisvalid") String dsftyxisvalid,@RequestParam("dsftyxisvote") String dsftyxisvote, @RequestParam("bjmpcsxischoice") String bjmpcsxischoice,@RequestParam("bjmpcsxbe") double bjmpcsxbe,@RequestParam("bjmpcsxbf") double bjmpcsxbf,@RequestParam("bjmpcsxisvalid") String bjmpcsxisvalid,@RequestParam("bjmpcsxisvote") String bjmpcsxisvote, @RequestParam("bjmpszxischoice") String bjmpszxischoice,@RequestParam("bjmpszxbe") double bjmpszxbe,@RequestParam("bjmpszxbf") double bjmpszxbf,@RequestParam("bjmpszxisvalid") String bjmpszxisvalid,@RequestParam("bjmpszxisvote") String bjmpszxisvote, @RequestParam("bjmpjdcdqxischoice") String bjmpjdcdqxischoice,@RequestParam("bjmpjdcdqxbe") double bjmpjdcdqxbe,@RequestParam("bjmpjdcdqxbf") double bjmpjdcdqxbf,@RequestParam("bjmpjdcdqxisvalid") String bjmpjdcdqxisvalid,@RequestParam("bjmpjdcdqxisvote") String bjmpjdcdqxisvote, @RequestParam("bjmpsjxischoice") String bjmpsjxischoice,@RequestParam("bjmpsjxbe") double bjmpsjxbe,@RequestParam("bjmpsjxbf") double bjmpsjxbf,@RequestParam("bjmpsjxisvalid") String bjmpsjxisvalid,@RequestParam("bjmpsjxisvote") String bjmpsjxisvote, @RequestParam("bjmpckxischoice") String bjmpckxischoice,@RequestParam("bjmpckxbe") double bjmpckxbe,@RequestParam("bjmpckxbf") double bjmpckxbf,@RequestParam("bjmpckxisvalid") String bjmpckxisvalid,@RequestParam("bjmpckxisvote") String bjmpckxisvote, @RequestParam("bjmpcshhssxischoice") String bjmpcshhssxischoice,@RequestParam("bjmpcshhssxbe") double bjmpcshhssxbe,@RequestParam("bjmpcshhssxbf") double bjmpcshhssxbf,@RequestParam("bjmpcshhssxisvalid") String bjmpcshhssxisvalid,@RequestParam("bjmpcshhssxisvote") String bjmpcshhssxisvote, @RequestParam("bjmpzrssxischoice") String bjmpzrssxischoice,@RequestParam("bjmpzrssxbe") double bjmpzrssxbe,@RequestParam("bjmpzrssxbf") double bjmpzrssxbf,@RequestParam("bjmpzrssxisvalid") String bjmpzrssxisvalid,@RequestParam("bjmpzrssxisvote") String bjmpzrssxisvote, @RequestParam("bjmpssxischoice") String bjmpssxischoice,@RequestParam("bjmpssxbe") double bjmpssxbe,@RequestParam("bjmpssxbf") double bjmpssxbf,@RequestParam("bjmpssxisvalid") String bjmpssxisvalid,@RequestParam("bjmpssxisvote") String bjmpssxisvote, @RequestParam("bjmpdcjcdddshxischoice") String bjmpdcjcdddshxischoice,@RequestParam("bjmpdcjcdddshxbe") double bjmpdcjcdddshxbe,@RequestParam("bjmpdcjcdddshxbf") double bjmpdcjcdddshxbf,@RequestParam("bjmpdcjcdddshxisvalid") String bjmpdcjcdddshxisvalid,@RequestParam("bjmpdcjcdddshxisvote") String bjmpdcjcdddshxisvote){
说实话,不想用类似的方式再写一个类似的功能了,之后查了下,发现在spring里面有更好的处理方式,就是通过提交表单的方法,所以这里仔细的研究一下如果通过表单提交该如何处理类似的问题;
表单处理主要分为两部分,一部分是表单的展示,一部分是处理表单的输入;
1.处理表单的输入
1.1.注解形式的参数接收
1.1.1. HttpServletRequest可以直接定义在参数的列表,通过该请求可以传递参数
@Override public int setAllIni(HttpServletRequest request) { int ret = 1; Map<String, Object> map = new HashMap<String,Object>(); //续保相关 map.put("iniName", "xuBaoZaiBeiPingAn"); map.put("iniValue", request.getParameter("xuBao")); ret = zaiBeiDao.updateIniZaiBei(map); if(ret != 1){ return 0; } map.put("iniName", "xuBaoZaiBeiPingAnDelayTime"); map.put("iniValue", request.getParameter("xuBaoDelayTime")); ret = zaiBeiDao.updateIniZaiBei(map); if(ret != 1){ return 0; }
1.1.2. 在参数列表上直接定义要接收的参数名称,只要参数名称能匹配的上就能接收所传过来的数据, 可以自动转换成参数列表里面的类型,注意的是值与类型之间是可以转换的
1.1.2.1,传递多种不同类型的参数:
url:http://baoxian.qa.ms.netease.com/vehicle/input?city_code=23223&license_no=12345&license_owner=kang&frame_no=9090&engine_no=8080&vehicle_name=4343&enroll_date=2015-08-07&seat_count=6
@RequestMapping(value="/vehicle/input",method=RequestMethod.POST) @ResponseBody public String vehicleInput(@RequestParam("city_code") String citycode,@RequestParam("license_no") String licenseNo, @RequestParam(value="license_owner",required=false) String licenseOwner, @RequestParam(value="frame_no",required=false) String frameNo, @RequestParam(value="engine_no",required=false) String engineNo,@RequestParam(value="vehicle_name",required=false) String vehivleName, @RequestParam(value="enroll_date",required=false) String enrollDate,@RequestParam(value="seat_count",required=false) String seatCount){ LogConstant.runLog.info("输入的参数:citycode:" + citycode + " license_no:" + licenseNo + " license_owner:" + licenseOwner + " frame_no:" + frameNo + " engine_no:" + engineNo + " vehicle_name:" + vehivleName + " enroll_date:" + enrollDate + " seat_count:" + seatCount); if(StringUtils.isEmpty(citycode) || StringUtils.isEmpty(licenseNo) || StringUtils.isEmpty(licenseOwner)){ return "无效的参数设置!"; } String result = carTypeMockService.vehicleInput(citycode,licenseNo,licenseOwner,frameNo,engineNo,vehivleName,enrollDate,seatCount); return result; }
url:http://localhost/mvc/test/toPerson2.do?name=tom&name=jack
/** * 对数组的接收,定义为同名即可 */ @RequestMapping("/toPerson2.do") public String toPerson2(String[] name) { for (String result : name) { System.out.println(result); } return "jsp1/index"; }
1.1.2.3.传递自定义对象(可多个)
url:http://localhost/mvc/test/toPerson3.do?name=zhangsan&age=14&address=china&birthday=2000-2-11
User 定义的属性有:name,age,并且有各自属性的对应的set方法以及toString方法
Person定义的属性有:name,age.address,birthday,并且有各自属性的对应的set方法以及toString方法
/** * * 传递的参数的名字必须要与实体类的属性set方法后面的字符串匹配的上才能接收到参数,首字符的大小写不区分 * 请求中传的参数只要是能和参数列表里面的变量名或者实体里面的set后面的字符串匹配的上就能接收到 * */ @RequestMapping("/toPerson3.do") public String toPerson3(Person person, User user) { System.out.println(person); System.out.println(user); return "jsp1/index"; }
function setAllIni(){ var data = {}; data.xuBao = $("#setXuBao").val(); data.xuBaoDelayTime = $("#xuBaoDelayTime").val(); ...... $.ajax({ url: '/zaiBeiPingAn/setAllIni.html', type: 'POST', data: data, dataType: 'json', async: false, success: function(json){ if(json.retCode=="200"){ bootbox.alert({ message: '批量设置成功', buttons: { ok: { label: '确定', className: 'btn-myStyle', } }, callback: function() { window.location.reload(); } }); }else{ bootbox.alert("批量设置失败"); } } });里面只是显示了表单里面的一条数据,其他数据格式类似;
1.3.表单提交
表单ftl页面:
<#include "../../inc/core.ftl"> <@htmHead title="保险Mock平台" otherJs=["/js/insurance/pingan/zaiBei.js", "/js/common/bootbox.js", "/js/date/WdatePicker.js"] /> <@htmlNavHead /> <div id="page-wrapper"> <div class="row"> <div class="col-lg-12"> <h1 class="page-header">灾备测试</h1> </div> </div> <br><br> <div class="row"> <div class="col-lg-12"> <div class="col-lg-3"> <div class="form-group input-group"> <span class="input-group-addon">续保校验</span> <select id="setXuBao" class="form-control" onchange="changeDelayTime('xb','setXuBao','xuBaoDelayTime')"> <option value="success">成功</option> <option value="delay">延时</option> <option value="failure">失败</option> <option value="session">session失效</option> </select> </div> </div> <div class="col-lg-3" id="xb-delay" style="display: none;"> <div class="form-group input-group"> <span class="input-group-addon">延时时长</span> <input id="xuBaoDelayTime" class="form-control"> </div> </div> <button class="btn btn-primary" type="button" onClick="setZaiBei('xuBao')">设置</button> </div> </div> <div class="row"> <div class="col-lg-12"> <div class="col-lg-3"> <div class="form-group input-group"> <span class="input-group-addon">生日校验</span> <select id="setShengRi" class="form-control" onchange="changeDelayTime('sr','setShengRi','shengRiDelayTime')"> <option value="success">成功</option> <option value="delay">延时</option> <option value="failure">失败</option> <option value="session">session失效</option> </select> </div> </div> <div class="col-lg-3" id="sr-delay" style="display: none;"> <div class="form-group input-group"> <span class="input-group-addon">延时时长</span> <input id="shengRiDelayTime" class="form-control"> </div> </div> <button class="btn btn-primary" type="button" onClick="setZaiBei('shengRi')">设置</button> </div> </div> <div class="row"> <div class="col-lg-12"> <button class="btn btn-primary" type="button" onClick="setAllIni()">批量设置</button> <button class="btn btn-primary" type="button" onClick="setBackUp()">还原设置</button> </div> </div> </div>
@RequestMapping("/zaiBeiPingAn/setAllIni") @ResponseBody public Map<String, Object> setAllIni(HttpServletRequest request, Model model){ Map<String, Object> retMap = new HashMap<String, Object>(); int ret = zaiBeiService.setAllIni(request); if(ret == 1){ retMap.put("retCode", 200); retMap.put("retDesc", "批量设置成功"); }else{ retMap.put("retCode", -1); retMap.put("retDesc", "批量设置失败"); } return retMap; }
@Override public int setAllIni(HttpServletRequest request) { int ret = 1; Map<String, Object> map = new HashMap<String,Object>(); //续保相关 map.put("iniName", "xuBaoZaiBeiPingAn"); map.put("iniValue", request.getParameter("xuBao")); ret = zaiBeiDao.updateIniZaiBei(map); if(ret != 1){ return 0; } map.put("iniName", "xuBaoZaiBeiPingAnDelayTime"); map.put("iniValue", request.getParameter("xuBaoDelayTime")); ret = zaiBeiDao.updateIniZaiBei(map); if(ret != 1){ return 0; } //生日相关 map.put("iniName", "shengRiZaiBeiPingAn"); map.put("iniValue", request.getParameter("shengRi")); ret = zaiBeiDao.updateIniZaiBei(map); if(ret != 1){ return 0; } map.put("iniName", "shengRiZaiBeiPingAnDelayTime"); map.put("iniValue", request.getParameter("shengRiDelayTime")); ret = zaiBeiDao.updateIniZaiBei(map); if(ret != 1){ return 0; } return ret; }这里描述了两条记录的情况,我们来看下运行情况:
参考文章:
1.http://alog2012.iteye.com/blog/2040214