表单用serialize()序列化并且通过ajax向后台传值,后台方法参数传递问题



首先是页面 表单用serialize()序列化并且通过ajax向后台传值,后台方法参数传递问题_第1张图片

此页面时一个表单

只要是选中的数据,就往后台传,没选中但是有数据,则不传递。此页面的代码如下
[html] view plain copy
  1. <form id="polForm" method="post">  
  2.                 <table class="width01" cellpadding="0" cellspacing="0" border="0">  
  3.                     <tr>  
  4.                         <td colspan="2"><span class="fweight01 fsize01">策略名称span>td>  
  5.                         <td><input name="jifei" type="text" class="width02 height01" value="计费策略1" />td>  
  6.                     tr>  
  7.                     <tr>  
  8.                         <td colspan="2"><span class="fweight01 fsize01">策略编号span>td>  
  9.                         <td><input type="text" class="width02 height01" name="bianhao"/>td>  
  10.                     tr>  
  11.                     <tr>  
  12.                         <td>  
  13.                             <a onclick="changeBox01()"><input id="box01" class="marginl01" type="checkbox" />a>  
  14.                         td>  
  15.                         <td class="tl"><span class="span01 span02">免费时长:span>td>  
  16.                         <td class="tl">  
  17.                             <span style="float:left;">  
  18.                                 <input id="fee01" name="fee01" class="width03 easyui-textbox" /><span class="span03">分钟span>  
  19.                                 <input id="fee02" name="fee02"class="width03 marginl01 easyui-combobox" value="超时计费" />  
  20.                             span>  
  21.                             <span id="dis01" class="span02">  
  22.                                 <a onclick="changeHtml()"><input id="box02" type="checkbox" />a><span class="span03 span04">超过span>  
  23.                                 <input id="fee03" name="fee03" class="width07 easyui-textbox" value="60" /><span class="span03">分钟后计算全部费用span>  
  24.                             span>  
  25.                         td>  
  26.                     tr>  
  27.                     <tr>  
  28.                         <td>td>  
  29.                         <td>td>  
  30.                         <td class="tl"><span id="dis02" class="color01">不到30分钟不收费,超过30分钟只收超时费用。span>td>  
  31.                     tr>  
  32.                     <tr>  
  33.                         <td><a onclick="disAppear()"><input id="box03" class="marginl01" type="checkbox" />a>td>  
  34.                         <td class="tl"><span class="span01 span02">免费时段:span>td>  
  35.                         <td class="tl">  
  36.                             <span style="float:left;">  
  37.                                 <input id="fee04" name="fee04" class="width07 easyui-timespinner" data-options="showSeconds:false" /><span class="span03 span04">--span>  
  38.                                 <input id="fee05" name="fee05"  class="width07 easyui-timespinner" data-options="showSeconds:false" />  
  39.                             span>  
  40.                             <span id="dis03" style="display:none;">  
  41.                                 <a class="marginl02" id="btn1"><img src="../common/images/greenpolicy.png" />a>  
  42.                                 <input id="fee06" class="width05 easyui-combobox" value="期间驶入驶出免费" />  
  43.                             span>  
  44.                         td>  
  45.                     tr>  
  46.                     <tr>  
  47.                         <td><a onclick="changeBox04()"><input id="box04" class="marginl01" type="checkbox" />a>td>  
  48.                         <td class="tl"><span class="span01 span02">计费循环单位:span>td>  
  49.                         <td class="tl">  
  50.                             <input id="fee07" class="width06 easyui-combobox" value="停放总时长24小时为一天" />  
  51.                         td>  
  52.                     tr>  
  53.                     <tr>  
  54.                         <td><a onclick="changeBox05()"><input id="box05" class="marginl01" type="checkbox" />a>td>  
  55.                         <td class="tl"><span class="span01 span02">白天时段:span>td>  
  56.                         <td class="tl">  
  57.                             <input id="fee08" class="width07 easyui-timespinner" data-options="showSeconds:false" value="06:00" /><span class="span03 span04">--span>  
  58.                             <input id="fee09" class="width07 easyui-timespinner" data-options="showSeconds:false" value="18:00" />  
  59.                         td>  
  60.                     tr>  
  61.                     <tr>  
  62.                         <td><a onclick="changeBox06()"><input id="box06" class="marginl01" type="checkbox" />a>td>  
  63.                         <td class="tl"><span class="span01 span02">计费方式:span>td>  
  64.                         <td class="tl">  
  65.                             <span style="float:left;"><input id="fee10" class="width03 easyui-combobox" value="全天" />span>  
  66.                             <span id="dis04" class="marginl01"><input id="fee11" class="width03 easyui-combobox" value="按时段计费" />span>   
  67.                             <span id="dis05" class="marginl01" style="display:none">  
  68.                                 <span>白天:span><input id="fee12" class="width03 easyui-combobox" value="按时段计费" />  
  69.                                 <span>夜间:span><input id="fee13" class="width03 easyui-combobox" value="按时段计费" />  
  70.                             span>  
  71.                         td>  
  72.                     tr>  
  73.                     <tr>  
  74.                         <td><a onclick="timeType()"><input id="box07" class="marginl01" type="checkbox" />a>td>  
  75.                         <td class="tl"><span class="span01 span02">按时段:span>td>  
  76.                         <td class="tl">  
  77.                             <input id="fee14" class="width07 easyui-timespinner" data-options="showSeconds:false" value="06:00" /><span class="span03 span04">--span>  
  78.                             <input id="fee15" class="width07 easyui-timespinner" data-options="showSeconds:false" value="18:00" />  
  79.                             <input id="fee16" class="width08 easyui-combobox marginl01" value="固定收费" />  
  80.                             <span class="span03 span04">span><input id="fee17" class="width07 easyui-textbox" /><span class="span03 span04">span>  
  81.                             <a href="#" class="marginl02" id="btn2" style="display:none;"><img src="../common/images/greenpolicy.png" />a>  
  82.                         td>  
  83.                     tr>  
  84.                     <tr>  
  85.                         <td><a onclick="timeSize()"><input id="box08" class="marginl01" type="checkbox" />a>td>  
  86.                         <td class="tl"><span class="span01 span02">按时长:span>td>  
  87.                         <td class="tl" id="fee22">  
  88.                             <input class="width09 easyui-textbox" value="1" /><span class="span03 span04">-span>  
  89.                             <input class="width09 easyui-textbox" value="5" /><span class="span03">小时span>  
  90.                             <span class="span02">span>  
  91.                             <input class="width09 easyui-textbox" value="5" /><span class="span03 span04">元/span>  
  92.                             <input class="width09 easyui-textbox" value="5" />  
  93.                             <span class="span03 span04">分钟span>  
  94.                             <a href="#" class="marginl02" id="btn3" style="display:none;"><img src="../common/images/greenpolicy.png" />a>  
  95.                         td>  
  96.                     tr>  
  97.                     <tr>  
  98.                         <td>td>  
  99.                         <td>td>  
  100.                         <td class="tl" id="fee18">  
  101.                             <span class="span04">大于span>  
  102.                             <input class="width10 easyui-textbox" value="5" /><span class="span03">小时span>  
  103.                             <span class="span02">span>  
  104.                             <input class="width09 easyui-textbox" value="5" /><span class="span03 span04">元/span>  
  105.                             <input class="width09 easyui-textbox" value="5" />  
  106.                             <span class="span03 span04">分钟span>  
  107.                         td>  
  108.                     tr>  
  109.                     <tr>  
  110.                         <td><a onclick="changeBox09()"><input id="box09" class="marginl01" type="checkbox" />a>td>  
  111.                         <td class="tl"><span class="span01 span02">过夜费:span>td>  
  112.                         <td class="tl" id="fee19">  
  113.                             <input class="width07 easyui-timespinner" value="22:00" /><span class="span03 span04">开始,夜间时段停车超过span>  
  114.                             <input class="width09 easyui-textbox" value="5" /><span class="span03 span04">分钟span>  
  115.                             <span class="span03 span04">需增加span>  
  116.                             <input class="width09 easyui-textbox" value="5" /><span class="span03">元过夜费span>  
  117.                         td>  
  118.                     tr>  
  119.                     <tr>  
  120.                         <td><a onclick="changeBox10()"><input id="box10" class="marginl01" type="checkbox" />a>td>  
  121.                         <td class="tl"><span class="span01 span02">收费限额:span>td>  
  122.                         <td class="tl" id="fee20">  
  123.                             <input class="width08 easyui-combobox" value="每天相同的限额" />  
  124.                             <input class="width07 easyui-textbox" value="20" /><span class="span03">span>  
  125.                         td>  
  126.                     tr>  
  127.                 table>  
  128.             form>  


表单要进行序列化,必须添加给需要的标签添加name属性,否则无用


js文件中的写法如下:
[javascript] view plain copy
  1. "white-space:pre">        var arr = $('#polForm').serialize();  
  2.         $.param(arr);  
  3.         console.log("表单序列化=============="+arr);  
  4.           
  5.         $.ajax({  
  6.             url:"/epark/testt.do",  
  7.             data:arr,  
  8.             type:"post",  
  9.             dataType:"json",  
  10.             success:function(data){  
  11.                 alert("测试发送接收成功");  
  12.             },  
  13.             error:function(){  
  14.                 console.log("失败");  
  15.             }  
  16.         })  

注意,ajax方法中data传递的是arr,那么问题来了,后台的方法接受时,参数是怎么传递的?
首先,创建一个实体类,要求类的属性和表单中name对应,如下图



然后就是Controller方法了,如下所示,直接把前台传过来的参数写成对应的实体类,会自动进行封装

[java] view plain copy
  1. @RequestMapping("/testt")  
  2. @ResponseBody  
  3. public void login(Book arr){  
  4.         System.out.println(arr.toString);  
  5. }  

经过测试,可以实现如下功能:
      如果form表单中元素有我们需要的数据,如果被选中则提交,不被选中则不提交,前台传递到后台的参数,会自动封装到对应的实体类中

你可能感兴趣的:(Json)