Javascript 在SpringMVCJava中,如何将表单值转换为json,然后从jsp页面提交给rest控制器

Javascript 在SpringMVCJava中,如何将表单值转换为json,然后从jsp页面提交给rest控制器,javascript,jquery,json,spring,rest,Javascript,Jquery,Json,Spring,Rest,过去三天我一直在努力。我不知道如何将表单值转换为json,然后将其传递给spring mvc rest控制器。请帮助我 下面是脚本: $(函数(){ $('#formId')。提交(函数(事件){ event.preventDefault();//防止提交此表单 var userJson=JSON.stringify(jQuery('#formId').serializeArray(); $.ajax({ 类型:“POST”, url:“/Owner/addprop”, 数据:userJson,

过去三天我一直在努力。我不知道如何将表单值转换为json,然后将其传递给spring mvc rest控制器。请帮助我

下面是脚本:

$(函数(){ $('#formId')。提交(函数(事件){ event.preventDefault();//防止提交此表单 var userJson=JSON.stringify(jQuery('#formId').serializeArray(); $.ajax({ 类型:“POST”, url:“/Owner/addprop”, 数据:userJson, contentType:“应用程序/json;字符集=utf-8”, 数据类型:“json”, 成功:功能(数据){ 警报(数据);//正确处理 }, 失败:函数(errMsg){ 警报(errMsg);//以正确的方式处理它 } }); 返回false; }); }); 这是我的表格:

<springform:form method="post" action="" commandName="property" id="formId"
 name="myform" >


    <div class="section">
        <div class="container">
            <div class="row">
                <div class="col-xs-12">







                        <div class="col-md-6">
                            Property/House No:
                            <spring:bind path="property.property_houseno">
                                <input name="property_houseno" class="form-control" type="text"
                                     value="${hno}" id="property_houseno" />
                            </spring:bind>
                            <p id="demo4" cssClass="error" />
                            <springform:errors path="property_houseno" cssClass="error" />

                        </div>
                        <div class="col-md-6">
                            <span> Location<i style="color: red;">*</i> <spring:bind
                                    path="property.property_loc">
                                    <div id="locationField">
                                        <input id="autocomplete" name="property_loc"
                                            placeholder="Search for location" onFocus="geolocate()"
                                            type="text" class="form-control" value="${locn}" required></input>
                                    </div>
                                </spring:bind>
                                <p id="demo6" cssClass="error" /> <springform:errors
                                    path="property_loc" cssClass="error" />
                        </div>
                        <div class="col-md-6">
                            street Name:
                            <spring:bind path="property.property_streetname">
                                <input name="property_streetname" class="form-control"
                                    value="${stname}" id="route" />
                            </spring:bind>
                            <p id="demo5" cssClass="error" />
                            <springform:errors path="property_streetname" cssClass="error" />
                        </div>
                        <div class="col-md-6">
                            <span>Pincode </span><i style="color: red;">*</i>
                            <spring:bind path="property.property_pin">
                                <input name="property_pin" class="form-control"
                                    id="postal_code" pattern="[0-9]{6}" value="${pcode}"
                                    title="Pincode should contain 6 digitis" required />
                            </spring:bind>
                            <p id="demo7" cssClass="error" />
                            <springform:errors path="property_pin" cssClass="error" />
                        </div>
                        <div class="col-md-6">
                            City:
                            <spring:bind path="property.property_city">
                                <input path="property_city" class="form-control"
                                    name="property_city" type="text" value="${cit}" id="locality" />
                            </spring:bind>
                            <p id="demo8" cssClass="error" />
                            <springform:errors path="property_city" cssClass="error" />
                        </div>
                        <div class="col-md-6">
                            State:
                            <spring:bind path="property.property_state">
                                <input path="property_state" class="form-control"
                                    name="property_state" type="text" value="${state}"
                                    id="administrative_area_level_1" />
                            </spring:bind>
                            <p id="demo9" cssClass="error" />
                            <springform:errors path="property_state" cssClass="error" />
                        </div>
                        <div class="col-md-6">
                            Area:
                            <spring:bind path="property.subarea">
                                <input path="subarea" class="form-control" name="subarea"
                                    type="text" value="${area}" id="sublocality_level_1" />
                            </spring:bind>
                            <p id="demo9" cssClass="error" />
                            <springform:errors path="subarea" cssClass="error" />
                        </div>
                        <div class="col-md-6">
                            Country:
                            <spring:bind path="property.country">
                                <input path="country" class="form-control" name="country"
                                    value="${cntry}" type="text" id="country" />
                            </spring:bind>
                            <p id="demo9" cssClass="error" />
                            <springform:errors path="country" cssClass="error" />
                        </div>

                    </fieldset>

                    <fieldset style="border: 1px solid silver; padding: 30px">
                        <legend class="text-center">Property Details</legend>
                        <div class="col-md-6" >
                            Property Type:<i style="color: red;">*</i>
                            <spring:bind path="property.property_type">
                                <div id="pdi2" style="display: block">
                                    <select name="property_type" onchange="setSubProp()" onload="setSubProp()" class="form-control"
                                        id="property_type" required>

                                        <option value="">Select</option>
                                        <option value="Residential">Residential</option>
                                        <option value="Commercial">Commercial</option>


                                    </select>
                                </div>
                            </spring:bind>
                        <%--    <p id="demo1" cssClass="error" />
                            <springform:errors path="property_type" cssClass="error" /> --%>
                        </div>
                        <div class="col-md-6" id="resi_sub_cat">
                            Property Type(Sub Category):<i style="color: red;">*</i>
                            <spring:bind path="property.property_type_sub">
                                <div id="pts" style="display: block">
                                    <select name="property_type_sub" class="form-control"
                                        id="property_type_sub1" >

                                        <option value="">Select</option>
                                        <option value="Apartment">Apartment</option>
                                        <option value="Independent">Independent</option>
                                        <option value="Row House">Row House</option>
                                        <option value="Twin House">Twin House</option>
                                        <option value="Bungalow">Bungalow</option>
                                        <option value="Farmhouse">Farmhouse</option> -->

                                    </select>
                                </div>
                            </spring:bind>
                            <p id="demo1" cssClass="error" />
                            <springform:errors path="property_type" cssClass="error" />
                        </div>

                        <%-- <div class="col-md-6" id="comm_sub_cat">
                            Property Type(Sub Category):<i style="color: red;">*</i>
                            <spring:bind path="property.property_type_sub">
                                <div id="pdi12" style="display: block">
                                    <select name="property_type_sub" class="form-control"
                                        id="property_type_sub2" >

                                        <option value="">Select</option>
                                        <option value="Office">Office</option>
                                        <option value="Retail">Retail</option>
                                        <option value="Leisure">Leisure</option>
                                        <option value="Industrial">Industrial</option>


                                    </select>
                                </div>
                            </spring:bind>
                            <p id="demo1" cssClass="error" />
                            <springform:errors path="property_type" cssClass="error" />
                        </div> --%>
                        <%-- <div class="col-md-6">
                            Status<i style="color: red;">*</i>&nbsp; <a href="#"
                                data-html="true" data-toggle="tooltip" data-placement="bottom"
                                title="<b>For sell</b> : Selecting &#8220;for sell&#8221;, property will be published for sell. This means property will show-up in search results when user search for property which are on sale. Property will show-up if any of search parameters match.
                         <br /> <br />   <b>For rent</b> : Selecting &#8220;for rent&#8221; property will be published for rent. This property will show-up in search results of properties on rent,only if its on lease. Adding lease will enable the user to provide the lease details like rent amount, bond amount etc.

返回pmService.addPerson(r);谢谢你回复乔治。但我的问题是,在控制器中并没有任何错误,我要问的是如何将表单值转换为json,然后将其发送到提到的控制器。请看你是否能帮我。你有没有试过这个可能的副本,谢谢你回复Kenz和oZOli。。我试过了,但没用。。表单未提交到服务器。我没有到达我要去的地方,返回pmService.addPerson(r);谢谢你回复乔治。但我的问题是,在控制器中并没有任何错误,我要问的是如何将表单值转换为json,然后将其发送到提到的控制器。请看你是否能帮我。你有没有试过这个可能的副本,谢谢你回复Kenz和oZOli。。我试过了,但没用。。表单未提交到服务器。我不会走错方向的
                            <div id="pdi2" style="display: block">
                                <spring:bind path="property.property_status">
                                    <select name="property_status" class="form-control"
                                        id="property_status" onchange="setDyna()" required>
                                        <option value="">select</option>
                                        <option value="for Rent">for Rent</option>
                                        <option value="for Sale">for Sell</option>
                                        <option value="none">None</option>

                                    </select>
                                </spring:bind>

                            </div>
                            <p id="demo15" cssClass="error" />
                            <springform:errors path="property_status" cssClass="error" />
                        </div> --%>




                        <div class="col-md-6" id="bedrum">
                            Bedrooms<i style="color: red;">*</i>
                            <div id="pdi2" style="display: block">
                                <spring:bind path="property.property_bedroom">
                                    <select name="property_bedroom" class="form-control"
                                        id="property_bedroom" >
                                        <option value="">select</option>

                                        <option value="1">1</option>
                                        <option value="2">2</option>
                                        <option value="3">3</option>
                                        <option value="4">4</option>
                                        <option value="5">5</option>
                                        <option value="studio apartment">Studio Apartment</option>
                                    </select>
                                </spring:bind>
                            </div>

                            <p id="demo10" cssClass="error" />
                            <springform:errors path="property_bedroom" cssClass="error" />
                        </div>

                        <div class="col-md-6">
                            Parking Type<i style="color: red;">*</i>

                            <div id="pdi2" style="display: block">
                                <spring:bind path="property.property_ptype">
                                    <select name="property_ptype" class="form-control"
                                        id="property_ptype" onchange="checkWheel();" required>
                                        <option value="">select</option>
                                        <option value="2 Wheeler">2 Wheeler</option>
                                        <option value="4 Wheeler">4 Wheeler</option>
                                        <option value="Both">Both</option>
                                        <option value="No Parking">No Parking</option>

                                    </select>
                                </spring:bind>
                            </div>

                            <p id="demo14" cssClass="error" />
                            <springform:errors path="property_ptype" cssClass="error" />
                        </div>
                        <div class="col-md-6">
                            Parking Spaces<i style="color: red;">*</i>

                            <div id="pdi2" style="display: block">
                                <spring:bind path="property.property_carspace">
                                    <select name="property_carspace" class="form-control"
                                        id="property_carspace" required>
                                        <option value="">select</option>
                                        <option value="1">1</option>
                                        <option value="2">2</option>
                                        <option value="3">3</option>
                                        <option value="4">4</option>
                                        <option value="5">5</option>
                                        <option value="0">0</option>
                                    </select>
                                </spring:bind>
                            </div>

                            <p id="demo11" cssClass="error" />
                            <springform:errors path="property_carspace" cssClass="error" />
                        </div>
                        <div class="col-md-6">
                            Furnished<i style="color: red;">*</i>

                            <div id="pdi2" style="display: block">
                                <spring:bind path="property.property_furnish">
                                    <select name="property_furnish" class="form-control"
                                        id="property_furnish" required>
                                        <option value="">select</option>
                                        <option value="Furnished">Furnished</option>
                                        <option value="Semi-Furnished">Semi-Furnished</option>
                                        <option value="Un-Furnished">Un-Furnished</option>

                                    </select>
                                </spring:bind>
                            </div>

                            <p id="demo12" cssClass="error" />
                            <springform:errors path="property_furnish" cssClass="error" />
                        </div>
                        <div class="col-md-6">
                            Bathrooms<i style="color: red;">*</i>

                            <div id="pdi2" style="display: block">
                                <spring:bind path="property.property_bathrooms">
                                    <select name="property_bathrooms" class="form-control"
                                        id="property_bathrooms" required>
                                        <option value="">select</option>
                                        <option value="1">1</option>
                                        <option value="2">2</option>
                                        <option value="3">3</option>
                                        <option value="4">4</option>
                                        <option value="5">5</option>

                                    </select>
                                </spring:bind>
                            </div>

                            <p id="demo13" cssClass="error" />
                            <springform:errors path="property_bathrooms" cssClass="error" />
                        </div>


                        <div class="col-md-6" id="avb">
                            Property Available Date:
                            <spring:bind path="property.property_availabledate">
                                <input name="property_availabledate" class="form-control"
                                    type="date" value="${adate}" id="property_availabledate" />
                            </spring:bind>

                            <p id="demo2" cssClass="error" />
                            <springform:errors path="property_availabledate"
                                cssClass="error" />
                        </div>

                        <div class="col-md-6">
                            Property Registration no:
                            <spring:bind path="property.property_regno">
                                <input name="property_regno" class="form-control" type="text"
                                    value="${pregno}" id="property_regno" />
                            </spring:bind>

                            <p id="demo2" cssClass="error" />
                            <springform:errors path="property_regno" cssClass="error" />
                        </div>

                        <div class="col-md-6">
                            Carpet Area:
                            <spring:bind path="property.sqft">
                                <input name="sqft" class="form-control" id="sqft"
                                    value="${bltparea}" pattern="[0-9]{2,10}" />
                            </spring:bind>

                            <p id="demo13" cssClass="error" />
                            <springform:errors path="sqft" cssClass="error" />
                        </div>

                        <div class="col-md-6">
                            <span>Please select a image to upload (Max 6 photos Max
                                size 5MB) : <a href="#" style="margin-bottom: 55;"
                                data-toggle="tooltip" data-placement="bottom"
                                title="Among the set of images which you select, the first selected image will be primary image. So select the images accordingly and upload"><img
                                    src="<c:url value="/resources/images/info-icon.png"/>" /></a>
                            </span>

                            <spring:bind path="property.file">
                                <input name="file" class="form-control" type="file" id="updf"
                                    accept="image/gif, image/jpeg" multiple
                                    onchange="checkSize()" />
                                <span class="style21"><springform:errors path="file"
                                        cssClass="error" /> </span>
                            </spring:bind>
                            <p id="demo13" cssClass="error" />
                            <springform:errors path="file" cssClass="error" />
                        </div>

                        <div class="col-md-6" id="selling">
                            Property Selling Price:
                            <spring:bind path="property.property_sellingprice">
                                <input name="property_sellingprice" class="form-control"
                                    type="text" value="${sellingp}" pattern="[0-9]{1,15}"
                                    title="Selling price should contain min. 1 and max.15 digits only."
                                    id="property_sellingprice" />
                            </spring:bind>

                            <p id="demo2" cssClass="error" />
                            <springform:errors path="property_sellingprice" cssClass="error" />
                        </div>

                        <div class="col-md-6">
                            Amenities&nbsp; <a href="#" data-toggle="tooltip"
                                data-placement="bottom"
                                title="Press Ctrl key to select multiple Amenities"><img
                                src="<c:url value="/resources/images/info-icon.png"/>" /></a>

                            <spring:bind path="property.amenities">
                                <!--  <select name="amenities" class="form-control" id="property_status" onchange="setDyna()" required> -->
                                <select name="amenities" class="form-control" id="amenities"
                                    onchange="setDyna()" multiple>
                                    <option value="pool">Swimming Pool</option>
                                    <option value="park">Playground/park</option>
                                    <option value="gym">Gym/Fitness Center</option>
                                    <option value="security">Gate Security</option>
                                    <option value="club">Club House</option>
                                    <option value="intercom">Intercom</option>
                                    <option value="parking">Visitor Parking</option>
                                    <option value="lift">Lift</option>

                                </select>
                            </spring:bind>

                            <p id="demo15" cssClass="error" />
                            <springform:errors path="amenities" cssClass="error" />
                        </div>

                        <div class="col-md-6">
                            Description:
                            <spring:bind path="property.property_note">
                                <textarea name="property_note" class="form-control"
                                    id="property_note" value="${desc}" rows="4" cols="20">
                            </textarea>
                            </spring:bind>
                        </div>


                    </fieldset>
                </div>
            </div>
        </div>
    </div>

    <div class="col-md-5 col-centered">
        <table class="table table-borderless">

            <td colspan="3"><div align="center">
                    <input type="Submit" class="btn btn-info" id="submit"
                        name="Submit" value="Submit" /> &nbsp; &nbsp; &nbsp; &nbsp; <input
                        type="reset" class="btn btn-info" name="Reset" value="Reset" />
                    &nbsp; &nbsp; &nbsp; &nbsp; <input type="button" name="b"
                        class="btn btn-info" value="Back" onclick="history.back();" />
                </div></td>
            </tr>

        </table>
    </div>



</springform:form>
@RequestMapping(value="/addprop",method=RequestMethod.POST,headers="Accept=application/json" )
    public @ResponseBody PropRepo addpropr(@RequestBody PropRepo r, ModelMap map,HttpServletRequest request){


            pmService.addPerson(r);
            return r;


      }