第①步:引入依赖
<!--数据校验-->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>4.3.1.Final</version>
</dependency>
<!--validation api-->
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>1.0.0.GA</version>
</dependency>
第②步:配置Spring.xml
<!--:参数方法名解析器-->
<bean id="methodNameResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!--扫描包下所有的被标注的类-->
<context:component-scan base-package="cn.happy.day15Validator"/>
<!--配置验证器-->
<bean id="myvalidator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean">
<property name="providerClass" value="org.hibernate.validator.HibernateValidator"/>
</bean>
<!--绑定验证器-->
<mvc:annotation-driven validator="myvalidator"/>
第③步:实体类注解
package cn.happy.day15Validator;
import org.hibernate.validator.constraints.NotEmpty;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.Pattern;
import javax.validation.constraints.Size;
/**
* Created by Administrator on 2018/3/31.
*/
public class UserInfo {
/**
* Min:设置最小值
* Max:设置最大值
* Message:打印信息
*/
//指定年龄在18-120之间
@Min(value = 18,message = "年龄不能小于18岁")
@Max(value = 120,message = "年龄不能大于100岁")
private Integer age;
/**
* 三种验证方式
* @NotEmpty 用于集合
* @NotNull 用于基本类型
* @NotBlank 用于引用类型
* @Size 设置大小
*/
//非空验证
@NotEmpty(message = "用户名不能为空")
@Size(max = 20,min = 6,message = "用户名不能大于20个字符或小于6个字符")
private String name;
/**
* @Pattern rehexp匹配正则
*/
@NotEmpty(message = "邮箱不能为空")
@Pattern(regexp = "^\\w+@\\w+/.\\w+$",message = "邮箱格式错误")
private String email;
@Pattern(regexp = "^1[35789]\\d{9}$",message = "手机号格式错误")
private String phone;
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
第④步:配置处理器
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.validation.Valid;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* Created by Administrator on 2018/3/30.
*/
@Controller
public class FirstController {
@RequestMapping("/first")
/**
* @Valid 验证对象
* BindingResult 绑定结果
* Model 视图对象
*/
public String doFirst(@Valid UserInfo userInfo, BindingResult br, Model mv) throws Exception {
//判断br中的错误数
if(br.getErrorCount()>0){ //证明至少有一个数据错误
//获取信息
FieldError name = br.getFieldError("name");
FieldError age = br.getFieldError("age");
FieldError phone = br.getFieldError("phone");
FieldError email = br.getFieldError("email");
//获取错误信息
if(name!=null){
String namemsg = name.getDefaultMessage();
//把错误信息返回到页面
mv.addAttribute("namemsg",namemsg);
}
if(age!=null){
String agemsg = age.getDefaultMessage();
mv.addAttribute("agemsg",agemsg);
}
if(phone!=null){
String phonemsg = phone.getDefaultMessage();
mv.addAttribute("phonemsg",phonemsg);
}
if(email!=null){
String emailmsg = email.getDefaultMessage();
mv.addAttribute("emailmsg",emailmsg);
}
//返回请求View
return "Validator";
}
return "doSecond";
}
}
配置完成!
阿里云年末钜惠,27款产品低至2折,有需要的可以看一下https://m.aliyun.com/act/team1212?params=N.rLGEYu0Jk2