<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<html>
<head>
<title>Reservation Form</title>
<style>
.error {
color: #ff0000;
font-weight: bold;
}
</style>
</head>
<body>
<form:form method="post" modelAttribute="vm">
<form:errors path="*" cssClass="error" />
<table>
<tr>
<td>Name</td>
<td><form:input path="userName" />
</td>
<td><form:errors path="userName" cssClass="error" />
</td>
</tr>
<tr>
<td>email</td>
<td><form:input path="email" />
</td>
<td><form:errors path="email" cssClass="error" />
</td>
</tr>
<tr>
<td colspan="3"><input type="submit" />
</td>
</tr>
</table>
</form:form>
</body>
</html>
5、开启spring的Valid功能
<mvc:annotation-driven />
三、自定义校验类型
1、需要验证的实体 Bean
public class LoginVo {
@NotNull
@IsMobile //自定义的注解
private String mobile;
@NotNull
@Length(min=32)
private String password;
//省略 get set 方法
}
2、自定义 IsMobile 注解类
import static java.lang.annotation.ElementType.ANNOTATION_TYPE;
import static java.lang.annotation.ElementType.CONSTRUCTOR;
import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.ElementType.PARAMETER;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import javax.validation.Constraint;
import javax.validation.Payload;
@Target({ METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER })
@Retention(RUNTIME)
@Documented
@Constraint(validatedBy = {IsMobileValidator.class })
public @interface IsMobile {
//允许为空的属性
boolean required() default true;
//如果校验不通过返回的提示信息
String message() default "手机号码格式错误";
Class<?>[] groups() default { };
Class<? extends Payload>[] payload() default { };
}
3、校验器
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import org.apache.commons.lang3.StringUtils;
import com.imooc.miaosha.util.ValidatorUtil;
//IsMobile:自定义的注解
//String:注解参数类型
public class IsMobileValidator implements ConstraintValidator<IsMobile, String> {
//默认值_false,用于接收注解上自定义的 required
private boolean required = false;
//1、初始化方法:通过该方法我们可以拿到我们的注解
public void initialize(IsMobile constraintAnnotation) {
//constraintAnnotation.required() 接收我们自定义的属性,是否为空
required = constraintAnnotation.required();
}
//2、逻辑处理
public boolean isValid(String value, ConstraintValidatorContext context) {
# 最后
我想问下大家当初选择做程序员的初衷是什么?有思考过这个问题吗?高薪?热爱?
既然入了这行就应该知道,这个行业是靠本事吃饭的,你想要拿高薪没有问题,请好好磨练自己的技术,不要抱怨。有的人通过培训可以让自己成长,有些人可以通过自律强大的自学能力成长,如果你两者都不占,还怎么拿高薪?
架构师是很多程序员的职业目标,一个好的架构师是不愁所谓的35岁高龄门槛的,到了那个时候,照样大把的企业挖他。为什么很多人想进阿里巴巴,无非不是福利待遇好以及优质的人脉资源,这对个人职业发展是有非常大帮助的。
如果你也想成为一名好的架构师,那或许这份**[Java核心架构笔记](https://gitee.com/vip204888/java-p7)**你需要阅读阅读,希望能够对你的职业发展有所帮助。
**中高级开发必知必会:**
自己成长,有些人可以通过自律强大的自学能力成长,如果你两者都不占,还怎么拿高薪?
架构师是很多程序员的职业目标,一个好的架构师是不愁所谓的35岁高龄门槛的,到了那个时候,照样大把的企业挖他。为什么很多人想进阿里巴巴,无非不是福利待遇好以及优质的人脉资源,这对个人职业发展是有非常大帮助的。
如果你也想成为一名好的架构师,那或许这份**[Java核心架构笔记](https://gitee.com/vip204888/java-p7)**你需要阅读阅读,希望能够对你的职业发展有所帮助。
**中高级开发必知必会:**
