简体   繁体   English

类型不匹配,Spring BindException

[英]Type mismatch, Spring BindException

When I input alphabets such as " adsf " in the input field, spring tries to bind it to my price( a double) property of my "Code" object and this causes the bind exception. 当我在输入字段中输入诸如“ adsf ”之类的字母时,spring尝试将其绑定到我的“ Code”对象的price(double)属性,这将导致绑定异常。 What can I do to avoid this error, if I wanted to keep the field as a double? 如果我想将该字段保留为两倍,该如何避免该错误?

POJO POJO

@entity
@Table(name=“CODE”)
public class Code{

    @Column(name=“PRICE”)
    @NotNull
    @DecimalMax(value=“999999999999.999999”)
    @DemimalMin(value=“0.0”)
    private Double price;

    //getters and setters

}

My controller 我的控制器

@RequestMapping(value=“validateField”, method=RequestMethod.POST, produces=“application/json”)
public FieldValidatinResult validateField(final Code code, final String field){
    //return statement
}

html page HTML页面

<input type=“text” class=“form-control validatable” id=“price”></input>

Error message: 错误信息:

    Field error in object ‘code’ on field ‘price’: rejected value [adsf]; 
codes[typeMismatch.code.price,typeMismatch.price,typeMismatch.java.lang.Double,typeMismatch]; arguments[org.springframework.context.support.DefaultMessageSourceResolvable: 
codes [code.price,price]; arguments []; default message [price]]; default message 
[Failed to convert property value of type 'java.lang.String' to required type 
'java.lang.Double' for property 'price'; nested exception is 
java.lang.NumberFormatException: For input string: "adsf"]

I would not say that validations on client side are 'best' as they can be easily avoided ( take action url from form, post some garbage ), but its probably the easiest to do. 我不会说在客户端进行的验证是“最佳”的,因为可以很容易地避免它们(从表单中获取action url,发布一些垃圾),但这可能是最容易做到的。

I would recommend to add validation also on server side. 我建议也在服务器端添加验证。 You have your annotations ready, now you have to use @Valid on your Code code and BindingResult parameter. 您已经准备好注释,现在必须在Code codeBindingResult参数上使用@Valid http://codetutr.com/2013/05/28/spring-mvc-form-validation/ <- this seems to be some useful tutorial. http://codetutr.com/2013/05/28/spring-mvc-form-validation/ <-这似乎是一些有用的教程。

Error clearly says 错误清楚地说

[Failed to convert property value of type 'java.lang.String' to required type 'java.lang.Double' for property 'price'; [无法将类型'java.lang.String'的属性值转换为属性'price'的必需类型'java.lang.Double'; nested exception is java.lang.NumberFormatException: For input string: "adsf"] 嵌套的异常为java.lang.NumberFormatException:对于输入字符串:“ adsf”]

User has provided a non number input and when it is parsed as double on server it is throwing NumberFormatException . 用户提供了一个非数字输入,当在服务器上将其解析为double时,将NumberFormatException Best way to solve this is to handle it client side. 解决此问题的最佳方法是在客户端进行处理。 Add a validation like below before submitting the form 在提交表单之前,添加如下所示的验证

if (!isNaN(parseInt($("#price").val()))) {
    //proceed with form submit
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM