Annotation used in the domain class.
Annotation | Description | Example |
---|---|---|
@NumberFormat | Converts a string in the specified format to a numeric type. | @NumberFormat(pattern="#,###") |
@DateTimeFormat | Converts a string in the specified format to a date type. | @DateTimeFormat(pattern="yyyy/MM/dd") |
Form.java
import org.springframework.format.annotation.DateTimeFormat;
public class SignupForm {
@DateTimeFormat(pattern="yyyy/MM/dd")
private Date birthday;
}
Create messages.properties
in the resouces directory and write as follows.
There is no need for ** double quotation marks ** after the equal.
pattern | Description method | Description example |
---|---|---|
1 | typeMismatch. |
typeMismatch.signupForm.age=Please enter a number |
2 | typeMismatch. |
typeMismatch.age=Please enter a number |
3 | typeMismatch. |
typeMismatch.int=Please enter a number |
Annotation used in the domain class.
Classification | Annotation | Description |
---|---|---|
Bean Validation | @NotNull | Check that it is not null |
@NotEmpty | Check that the string or Collection is not null or empty | |
@NutBlank | Check that the string is not only null, empty string, whitespace | |
@Max | Check if it is less than or equal to the specified value | |
@Min | Check if it is more than the specified value | |
@Size | Check if the length of the string and the size of the Collection are within the specified range | |
@AssertTrue | Check if true | |
@AssertFale | Check if false | |
@Pattern | Check if it matches the specified regular expression | |
Check if the string is in the form of an email address | ||
Hibernate Validator | @Range | Check if the value is within the specified range |
@Length | Check if the length of the string is within the specified range | |
@CreditCardNumber | Check if the string is in credit card number format | |
@URL | Check if the string is in URL format |
An error will occur at ** NG **.
Annotation | null | Empty string | Blank |
---|---|---|---|
@NotNull | NG | OK | OK |
@NotEmpty | NG | NG | OK |
@NotBlank | NG | NG | NG |
Form.java
//When the classification is Bean Validation
// javax.validation.Import from constraints
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Email;
import javax.validation.constraints.Pattern;
//Classification is Hibernate Validator
// org.hibernate.validator.Import from constraints
import org.hibernate.validator.constraints.Length;
public class SignupForm {
//Required input, email address format
@NotBlank
@Email
private String userId;
@Length(min=4, max=8)
@Pattern(regexp="^[a-zA-Z0-9]+$")
private String password;
}
Add to the same messages.properties
as the data binding error message
pattern | Description method | Description example |
---|---|---|
1 | NotBlank.signupForm.userId=Please enter your user ID | |
2 | NotBlank.uerId=Please enter your user ID | |
3 | NotBlank.java.lang.Sting ≒ required input | |
4 | NotBlank=Required input | |
5 | require_check=Required |
Recommended Posts