//下載導入包------------------------------------------------------------------- <dependencies> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-validator</artifactId> <version>5.1.3.Final</version> </dependency> <dependency> <groupId>javax.el</groupId> <artifactId>el-api</artifactId> <version>1.0</version> </dependency> </dependencies> //bean的標籤用法------------------------------------------------------------ private String id;//主鍵id @NotNull(message = "身份證不能爲空") @Size(min = 15, max = 18, message = "身份證長度15-18位") private String idcard;//身份證號碼 private String name;//姓名 //引入包-------------------------------------------------------------------- import javax.validation.ConstraintViolation; import javax.validation.Validation; import javax.validation.Validator; import javax.validation.ValidatorFactory; //後臺java獲取字段校驗後信息------------------------------------------------------------ ValidatorFactory factory = Validation.buildDefaultValidatorFactory(); Validator validator = factory.getValidator(); Set<ConstraintViolation<InfoApplyVo>> constraintViolations = validator.validate(paramVo); for (ConstraintViolation constraintViolation : constraintViolations) { msg = msg+"["+constraintViolation.getMessageTemplate()+"]"; }
完美~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~java