In Spring's data binding, you can use @ModelAttribute to bind forms to JavaBeans.
But we all know that the data passed from the client is first of all character type. If there are other types on the binding object JavaBean, then type conversion is bound to be required.
At this point, Spring is relatively lacking. Type checking is not performed before binding, and the conversion is performed abruptly.
For example, when converting to a number or date type
public class PersonForm { @Size(min=2, max=30) private String name; @NumberFormat(pattern="#,###") private Integer age; @DateTimeFormat(pattern="yyyy/MM/dd") private Date birthday; }
If the input is not a number type at all, or the input date type is wrong, an exception will be displayed
Failed to convert property value of type [java.lang.String] to required type [java.util.Date] for property birthday; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [@org.springframework.format.annotation.DateTimeFormat java.util.Date] for value
Since you cannot check the data type or format before binding, you can only start with the converted exception message.
The information typed above is the default information, then the search for custom information is in the following order
2.: code + "." + field
3.: code + "." + field type
4.: code
For example, the above numerical type
typeMismatch.age=age must be Integer
typeMismatch.java.lang.Integer={0} must be Integer
typeMismatch=convertion error
If the whole system uses the same message, you can use the field type
In addition, after verification, if there is an error, the error information will be saved in the class FieldError, and the information of the filed will be used as the first parameter. The type of this parameter is DefaultMessageSourceResolvable.
That is to say, in the message setting, you can use {0} to access this parameter, and because this parameter itself is DefaultMessageSourceResolvable, you can also define the file's message in the message.
This is very useful in multiple languages.