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

来源:互联网 发布:php 500 error_log 编辑:程序博客网 时间:2024/06/08 14:03
首先是页面

此页面时一个表单

只要是选中的数据,就往后台传,没选中但是有数据,则不传递。此页面的代码如下
<form id="polForm" method="post">                <table class="width01" cellpadding="0" cellspacing="0" border="0">                    <tr>                        <td colspan="2"><span class="fweight01 fsize01">策略名称</span></td>                        <td><input name="jifei" type="text" class="width02 height01" value="计费策略1" /></td>                    </tr>                    <tr>                        <td colspan="2"><span class="fweight01 fsize01">策略编号</span></td>                        <td><input type="text" class="width02 height01" name="bianhao"/></td>                    </tr>                    <tr>                    <td>                        <a onclick="changeBox01()"><input id="box01" class="marginl01" type="checkbox" /></a>                        </td>                        <td class="tl"><span class="span01 span02">免费时长:</span></td>                        <td class="tl">                        <span style="float:left;">                            <input id="fee01" name="fee01" class="width03 easyui-textbox" /><span class="span03">分钟</span>                            <input id="fee02" name="fee02"class="width03 marginl01 easyui-combobox" value="超时计费" />                            </span>                            <span id="dis01" class="span02">                            <a onclick="changeHtml()"><input id="box02" type="checkbox" /></a><span class="span03 span04">超过</span>                            <input id="fee03" name="fee03" class="width07 easyui-textbox" value="60" /><span class="span03">分钟后计算全部费用</span>                            </span>                        </td>                    </tr>                    <tr>                    <td></td>                        <td></td>                        <td class="tl"><span id="dis02" class="color01">不到30分钟不收费,超过30分钟只收超时费用。</span></td>                    </tr>                    <tr>                    <td><a onclick="disAppear()"><input id="box03" class="marginl01" type="checkbox" /></a></td>                        <td class="tl"><span class="span01 span02">免费时段:</span></td>                        <td class="tl">                        <span style="float:left;">                            <input id="fee04" name="fee04" class="width07 easyui-timespinner" data-options="showSeconds:false" /><span class="span03 span04">--</span>                            <input id="fee05" name="fee05"  class="width07 easyui-timespinner" data-options="showSeconds:false" />                            </span>                            <span id="dis03" style="display:none;">                            <a class="marginl02" id="btn1"><img src="../common/images/greenpolicy.png" /></a>                            <input id="fee06" class="width05 easyui-combobox" value="期间驶入驶出免费" />                           </span>                        </td>                    </tr>                    <tr>                    <td><a onclick="changeBox04()"><input id="box04" class="marginl01" type="checkbox" /></a></td>                        <td class="tl"><span class="span01 span02">计费循环单位:</span></td>                        <td class="tl">                        <input id="fee07" class="width06 easyui-combobox" value="停放总时长24小时为一天" />                        </td>                    </tr>                    <tr>                    <td><a onclick="changeBox05()"><input id="box05" class="marginl01" type="checkbox" /></a></td>                        <td class="tl"><span class="span01 span02">白天时段:</span></td>                        <td class="tl">                        <input id="fee08" class="width07 easyui-timespinner" data-options="showSeconds:false" value="06:00" /><span class="span03 span04">--</span>                            <input id="fee09" class="width07 easyui-timespinner" data-options="showSeconds:false" value="18:00" />                        </td>                    </tr>                    <tr>                    <td><a onclick="changeBox06()"><input id="box06" class="marginl01" type="checkbox" /></a></td>                        <td class="tl"><span class="span01 span02">计费方式:</span></td>                        <td class="tl">                        <span style="float:left;"><input id="fee10" class="width03 easyui-combobox" value="全天" /></span>                            <span id="dis04" class="marginl01"><input id="fee11" class="width03 easyui-combobox" value="按时段计费" /></span>                             <span id="dis05" class="marginl01" style="display:none">                            <span>白天:</span><input id="fee12" class="width03 easyui-combobox" value="按时段计费" />                                <span>夜间:</span><input id="fee13" class="width03 easyui-combobox" value="按时段计费" />                            </span>                        </td>                    </tr>                    <tr>                    <td><a onclick="timeType()"><input id="box07" class="marginl01" type="checkbox" /></a></td>                        <td class="tl"><span class="span01 span02">按时段:</span></td>                        <td class="tl">                        <input id="fee14" class="width07 easyui-timespinner" data-options="showSeconds:false" value="06:00" /><span class="span03 span04">--</span>                            <input id="fee15" class="width07 easyui-timespinner" data-options="showSeconds:false" value="18:00" />                            <input id="fee16" class="width08 easyui-combobox marginl01" value="固定收费" />                            <span class="span03 span04">共</span><input id="fee17" class="width07 easyui-textbox" /><span class="span03 span04">元</span>                            <a href="#" class="marginl02" id="btn2" style="display:none;"><img src="../common/images/greenpolicy.png" /></a>                        </td>                    </tr>                    <tr>                    <td><a onclick="timeSize()"><input id="box08" class="marginl01" type="checkbox" /></a></td>                        <td class="tl"><span class="span01 span02">按时长:</span></td>                        <td class="tl" id="fee22">                        <input class="width09 easyui-textbox" value="1" /><span class="span03 span04">-</span>                            <input class="width09 easyui-textbox" value="5" /><span class="span03">小时</span>                            <span class="span02">按</span>                            <input class="width09 easyui-textbox" value="5" /><span class="span03 span04">元/</span>                            <input class="width09 easyui-textbox" value="5" />                            <span class="span03 span04">分钟</span>                            <a href="#" class="marginl02" id="btn3" style="display:none;"><img src="../common/images/greenpolicy.png" /></a>                        </td>                    </tr>                    <tr>                    <td></td>                        <td></td>                        <td class="tl" id="fee18">                        <span class="span04">大于</span>                            <input class="width10 easyui-textbox" value="5" /><span class="span03">小时</span>                            <span class="span02">按</span>                            <input class="width09 easyui-textbox" value="5" /><span class="span03 span04">元/</span>                            <input class="width09 easyui-textbox" value="5" />                            <span class="span03 span04">分钟</span>                        </td>                    </tr>                    <tr>                    <td><a onclick="changeBox09()"><input id="box09" class="marginl01" type="checkbox" /></a></td>                        <td class="tl"><span class="span01 span02">过夜费:</span></td>                        <td class="tl" id="fee19">                        <input class="width07 easyui-timespinner" value="22:00" /><span class="span03 span04">开始,夜间时段停车超过</span>                            <input class="width09 easyui-textbox" value="5" /><span class="span03 span04">分钟</span>                            <span class="span03 span04">需增加</span>                            <input class="width09 easyui-textbox" value="5" /><span class="span03">元过夜费</span>                        </td>                    </tr>                    <tr>                    <td><a onclick="changeBox10()"><input id="box10" class="marginl01" type="checkbox" /></a></td>                        <td class="tl"><span class="span01 span02">收费限额:</span></td>                        <td class="tl" id="fee20">                        <input class="width08 easyui-combobox" value="每天相同的限额" />                            <input class="width07 easyui-textbox" value="20" /><span class="span03">元</span>                        </td>                    </tr>                </table>            </form>


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


js文件中的写法如下:
<span style="white-space:pre"></span>var arr = $('#polForm').serialize();$.param(arr);console.log("表单序列化=============="+arr);$.ajax({url:"/epark/testt.do",data:arr,type:"post",dataType:"json",success:function(data){alert("测试发送接收成功");},error:function(){console.log("失败");}})

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



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

@RequestMapping("/testt")@ResponseBodypublic void login(Book arr){        System.out.println(arr.toString);}

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


2 0
原创粉丝点击