2017-06-22 23 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。