weixin_33701294 2018-06-20 19:51 采纳率: 0%
浏览 429

ModelAttribute为null

I use spring boot 2

Controller part

@PostMapping("template/new/samplings")
@ResponseBody
public SamplingsDto save(@ModelAttribute SamplingsDto samplings) {
    return samplingsService.save(samplings);
}

I try to do save a form

$("#samplingsForm").submit(function (e){
    e.preventDefault();

    var receptionDate =  $("#samplingsReceptionDatePicker").data('daterangepicker').startDate.format('YYYY-MM-DD');
    var buildDate =  $("#samplingsBuildDatePicker").data('daterangepicker').startDate.format('YYYY-MM-DD');

    var form = transForm.serialize('#samplingsForm');

    form.receptionDate=receptionDate;
    form.buildDate=buildDate;

    form = JSON.stringify(form);

    $.ajax({
        type:"post",
        url: "/template/new/samplings",
        data: form,
        contentType: "application/json",
        dataType : "json",
        success: function(data){
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
        }
    });

});

With chrome request payload is

"{" "samplingsId":"", "buildDate":"2018-06-20", "receptionDate":"2018-06-20", "productTypesId":"1", "productsId":"15", "}"

On the server model attribute field are null

Edit

public class SamplingsDto {

        private Integer samplingsId;

        private Integer productTypesId;

        private Integer productsId;

        private LocalDate receptionDate;

        private LocalDate buildDate;

        //get set
    }
  • 写回答

2条回答 默认 最新

  • weixin_33717117 2018-06-20 20:21
    关注

    First, add setters and getters for the properties of SamplingsDto. (I would also use Lombok for setters/getters)

    public class SamplingsDto {
        private Integer samplingsId;
    
        private Integer productTypesId;
    
        private Integer productsId;
    
        private LocalDate receptionDate;
    
        private LocalDate buildDate;
    
        public Integer getSamplingsId() {
            return samplingsId;
        }
    
        public void setSamplingsId(Integer samplingsId) {
            this.samplingsId = samplingsId;
        }
    
        public Integer getProductTypesId() {
            return productTypesId;
        }
    
        public void setProductTypesId(Integer productTypesId) {
            this.productTypesId = productTypesId;
        }
    
        public Integer getProductsId() {
            return productsId;
        }
    
        public void setProductsId(Integer productsId) {
            this.productsId = productsId;
        }
    
        public LocalDate getReceptionDate() {
            return receptionDate;
        }
    
        public void setReceptionDate(LocalDate receptionDate) {
            this.receptionDate = receptionDate;
        }
    
        public LocalDate getBuildDate() {
            return buildDate;
        }
    
        public void setBuildDate(LocalDate buildDate) {
            this.buildDate = buildDate;
        }
    }
    

    Then you can use it as follows:

    @PostMapping("template/new/samplings")
    public SamplingsDto save(@RequestBody SamplingsDto samplings) {
        return samplingsService.save(samplings);
    }
    
    评论

报告相同问题?