访问Spring控制器中的数据值

时间:2017-09-17 06:39:11

标签: spring spring-mvc

我在html中使用了一个datalist,如下所示:

<input type="text" id="eventName" list="eventname" name="eventname" 
    placeholder="Event category">

    <datalist id="eventname">
        <c:forEach var="eventName2" items="${listDropDown}">
           <option  data-id="${eventName2.eventId}" 
               value="${eventName2.eventName}">
        </c:forEach>
    </datalist>

我使用输入字段的name属性从spring控制器调用该值。但我得到value标记的option属性。

@RequestMapping(value = "/searchForVenues", method = RequestMethod.POST)
     public ModelAndView saveProductInfo(ModelAndView model,@RequestParam("eventname")int eventid) {
         System.out.println(eventid);//some code//
     }

输出是一个例外:

Failed to convert value of type [java.lang.String] to required type [int]; 
 nested exception is java.lang.NumberFormatException: For input string: 
 "Wedding"

更多HTML:

  <div class="col-md-10">
                     <form:form id="signup" action="searchForVenues"  
     method="post" modelAttribute="searchForVenue" >
                         <form:hidden path="id"></form:hidden>
                    <div class="col-md-3"> 
                        <div class="form-group ">



<%--
                    <form:select id="eventName" class="form-control  
        select2" style="width: 100%; -webkit-appearance: none;" 
       path="eventName" multiple="multiple" >
                         <form:option value="0" selected="selected">Select a 
          category</form:option>
                <c:forEach var="eventName2" items="${listDropDown}">
                <form:option  
           value="${eventName2.eventName}">${eventName2.eventName}
         </form:option>
                </c:forEach>
            </form:select> 
                    --%> 

                        <input type="text" id="eventName" list="eventname"  
            placeholder="Event category" >

                        <datalist id="eventname">

                            <c:forEach var="eventName2" 
                items="${listDropDown}">

                                <option  data-id="${eventName2.eventId}" 
                  value="${eventName2.eventName}" >
                                    <input type="hidden" name="eventnames" 
                   id="eventName-hidden" value="${eventName2.eventId}">
                    </c:forEach>
                        </datalist>
                        <input type="hidden" name="eventnames" id="eventName-hidden" value="${eventName2.eventId}">
              </div>
                    </div>
                    <div class="col-md-3">
                        <div class="form-group ">

                            <%--
                        <form:select id="locationName"  class="form-control select2" path="locationName" multiple="multiple">
                             <form:option value="0" selected="selected">Select a location</form:option>
                    <c:forEach var="locationName" items="${dropdownForLocation}">
                    <form:option  value="${locationName.locName}">${locationName.locName}</form:option>
                    </c:forEach>
                </form:select>
                            --%>

                            <input type="text" id="locationName" list="location" placeholder="Location" name="locationname">
                        <datalist id="location">

                            <c:forEach var="locationName" items="${dropdownForLocation}">
                    <option  data-id="${locationName.locId}">${locationName.locName}</option>
                    <input type="hidden" name="locnames" id="locationName-hidden" value="${locationName.locId}">
                    </c:forEach>
                        </datalist>  
              </div>
                    </div>
                    <div class="col-md-3">
                        <div class="form-group">
                            <%--
                        <form:select id="venueTypeName" class="form-control select2" style="width: 100%;" path="venueTypeName">
                            <form:option value="0" selected="selected">Select type of venue</form:option>
                    <c:forEach var="venueTypeName" items="${dropdownForVenueType}">
                    <form:option  value="${venueTypeName.venueTypeName}">${venueTypeName.venueTypeName}</form:option>
                    </c:forEach>
                </form:select>
                            --%>
                            <input type="text" id="venueTypeName" list="venueType" placeholder="Venue type" name="venuetypename"/>
                        <datalist id="venueType" >

                            <c:forEach var="venueTypeName" items="${dropdownForVenueType}">
                                <option  data-id="${venueTypeName.venueTypeId}">${venueTypeName.venueTypeName}</option>
                                <input type="hidden" name="venuenames" id="venueTypeName-hidden" value="${venueTypeName.venueTypeId}">
                    </c:forEach>
                        </datalist>     


              </div>

                    </div >
                    <div class="col-md-1">
                        <input type="submit" class="btn-info" value="Find" id="findbtnn"/>
                    </div>
               </form:form>

                </div>

渲染html:

 <!-- Content Wrapper. Contains page content -->
  <div class="content-wrapper">
    <!-- Content Header (Page header) -->


    <!-- Main content -->
    <section class="content">

      <!-- Default box -->
      <div class="box">
        <div class="box-header with-border">
<!--          //<h3 class="box-title">Title</h3>-->

          <div class="box-tools pull-right">
<!--            <button type="button" class="btn btn-box-tool" data-widget="collapse" data-toggle="tooltip" title="Collapse">
              <i class="fa fa-minus"></i></button>
            <button type="button" class="btn btn-box-tool" data-widget="remove" data-toggle="tooltip" title="Remove">
              <i class="fa fa-times"></i></button>-->
          </div>
        </div>
        <div class="box-body">
            <div class="row">
                <div class="col-md-1"></div>
                <div class="col-md-10">
                     <form id="signup" action="searchForVenues" method="post">
                         <input id="id" name="id" type="hidden" value="0"/>
                    <div class="col-md-3"> 
                        <div class="form-group ">



                        <input type="text" id="eventName" list="eventname"  placeholder="Event category" >

                        <datalist id="eventname">



                                <option  data-id="1" value="Wedding">
                                    <input type="hidden" name="eventnames" id="eventName-hidden" value="1">


                                <option  data-id="2" value="Birthday">
                                    <input type="hidden" name="eventnames" id="eventName-hidden" value="2">


                                <option  data-id="3" value="Bouvat">
                                    <input type="hidden" name="eventnames" id="eventName-hidden" value="3">


                                <option  data-id="4" value="Akd">
                                    <input type="hidden" name="eventnames" id="eventName-hidden" value="4">


                                <option  data-id="5" value="Khatna">
                                    <input type="hidden" name="eventnames" id="eventName-hidden" value="5">


                                <option  data-id="6" value="Normal Party">
                                    <input type="hidden" name="eventnames" id="eventName-hidden" value="6">

                        </datalist>
                        <input type="hidden" name="eventnames" id="eventName-hidden" value="">
              </div>
                    </div>
                    <div class="col-md-3">
                        <div class="form-group ">



                            <input type="text" id="locationName" list="location" placeholder="Location" name="locationname">
                        <datalist id="location">


                    <option  data-id="1">Dhanmondi</option>
                    <input type="hidden" name="locnames" id="locationName-hidden" value="1">

                    <option  data-id="2">Gulshan</option>
                    <input type="hidden" name="locnames" id="locationName-hidden" value="2">

                    <option  data-id="3">Shantinagar</option>
                    <input type="hidden" name="locnames" id="locationName-hidden" value="3">

                    <option  data-id="4">Baridhara DOHS</option>
                    <input type="hidden" name="locnames" id="locationName-hidden" value="4">

                    <option  data-id="5">Bashundhara Baridhara</option>
                    <input type="hidden" name="locnames" id="locationName-hidden" value="5">

                    <option  data-id="6">Banani</option>
                    <input type="hidden" name="locnames" id="locationName-hidden" value="6">

                    <option  data-id="7">Paltan</option>
                    <input type="hidden" name="locnames" id="locationName-hidden" value="7">

                        </datalist>  
              </div>
                    </div>
                    <div class="col-md-3">
                        <div class="form-group">

                            <input type="text" id="venueTypeName" list="venueType" placeholder="Venue type" name="venuetypename"/>
                        <datalist id="venueType" >


                                <option  data-id="1">Restaurant</option>
                                <input type="hidden" name="venuenames" id="venueTypeName-hidden" value="1">

                                <option  data-id="2">Convention Hall</option>
                                <input type="hidden" name="venuenames" id="venueTypeName-hidden" value="2">

                                <option  data-id="3">Community Center</option>
                                <input type="hidden" name="venuenames" id="venueTypeName-hidden" value="3">

                        </datalist>     


              </div>

                    </div >
                    <div class="col-md-1">
                        <input type="submit" class="btn-info" value="Find" id="findbtnn"/>
                    </div>
               </form>

1 个答案:

答案 0 :(得分:1)

这是因为你初始化你的选择:

 <option  data-id="${eventName2.eventId}" value="${eventName2.eventName}">

在html表单中,选项组件会将您对 value 字段影响的内容返回给服务器。你正在影响它一个字符串并期待一个int。

<option  data-id="${eventName2.eventId}" value="${eventName2.eventId}">${eventName2.eventName}<option/>

应该做你想做的事。