2017-06-22 25 views
-1

我使用Spring啓動春天開機RequestParam類總是空

我的問題,當我發佈表單類(VehicleBrand等)始終爲空,但其他人(vinNumber,板)不爲空..

我該如何解決呢..

感謝

控制器

@Transactional 
    @PostMapping 
    String vehicleAddSubmit(
      @RequestParam String plate, 
      @RequestParam String vinNumber, 
      @RequestParam VehicleBrand vehicleBrand 
    ){ 
     Vehicle vehicle = new Vehicle(); 
     vehicle.setVehicleBrand(vehicleBrand); 
     vehicle.setEngineNumber(engineNumber); 
     vehicle.setVinNumber(vinNumber); 
    } 

查看

 <form th:attr="[email protected]{${#mvc.url('VC#vehicleAddSubmit').build()}}" method="post" class="form-horizontal"> 
      <input type="text" class="form-control" name="plate" placeholder="Plaka"/> 
     <input type="text" class="form-control" name="vinNumber" placeholder="Şase"/> 
     <select id="vBrand" name="vehicleBrand" class="form-control"> 
      <option value="">Marka Seçin</option> 
      <option value=""></option> 
      <option th:each="vBrand : ${vBrands}"               
        th:value="${vBrand.id}"                
        th:text="${vBrand.name}"> 
      </option> 
    </select> 
</form> 

回答

0

@ReqeustParam以不同的方式工作。這裏是Spring Documentation關於@RequestParam的片段

表示方法參數應該綁定到Web請求參數的註釋。

對於POST請求,您應該有@RequestBody來獲取模型/對象。 Spring會爲你做轉換。再次,Spring Documentation的另一個片段關於@RequestBody

指示方法參數的註釋應該綁定到Web請求的主體。請求的主體通過HttpMessageConverter傳遞,以根據請求的內容類型解析方法參數。

@PostMapping 
String vehicleAddSubmit(@Request VehicleForm vehicleform){ 
    //do that you need to do with this vehicle form here. 
} 

包括您在本VehicleForm需要的所有領域。

欲瞭解更多信息,請參閱Spring Documentation。