问题:类型转换错误
错误:
2019-08-30 09:40:34.048 [http-nio-18080-exec-6] WARN o.s.w.s.m.support.DefaultHandlerExceptionResolver - Resolved exception caused by Handler execution: org.springframework.validation.BindException: org.springframework.validation.BeanPropertyBindingResult: 1 errors
Field error in object 'tbActivityMerchants' on field 'id': rejected value [1,024]; codes [typeMismatch.tbActivityMerchants.id,typeMismatch.id,typeMismatch.java.lang.Integer,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [tbActivityMerchants.id,id]; arguments []; default message [id]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'java.lang.Integer' for property 'id'; nested exception is java.lang.NumberFormatException: For input string: "1,024"]
分析:
1、类型转换错误,前端提交到后台的值是string类型,而后端接收的对象该字段类型是int类型
2、本来该字段是int类型的,字段的值超过3位数,就会变成string类型,值里面会多个逗号,如果不对逗号进行处理,提交到后端的内容就成为string类型了 如:1,000
解决:
1、后端接收前端提交的内容是一个对象,故不建议在后端做转换处理,只能在前端提交数据前进行类型转换处理;
2、前端解决方法脚本代码:
var id=document.getElementById("id").value;// 获取输入内容 id=id.replace(/,/g,"");//内容替换 逗号替换掉 document.getElementById("id").value=id;// 处理后的值再给提交的内容重新赋值