1.编写工具类
import org.thymeleaf.util.StringUtils;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class ValidationUtils {
public static boolean matches(String text,String pattern){
Pattern p = Pattern.compile(pattern);
Matcher m = p.matcher(text);
return m.find();
}
public static boolean isEmailAdress(String text){
String emailPattern = "^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
return matches(text ,emailPattern);
}
public static boolean isEmpty(String text){
if(StringUtils.isEmpty(text)){
return true;
}else{
return false;
}
}
public static boolean isOverLength(String text,int length){
if(text.length()>length){
return true;
}else{
return false;
}
}
public static boolean digitsAndLetterOnly(String text){
String pattern="^[a-zA-Z0-9]*$";
return matches(text,pattern);
}
public static boolean containsSpecialWord(String text){
String regEx = "[ _`~!@#$%^&*()+=|{}':;',\\[\\].<>/?~!@#¥%……&*()——+|{}【】‘;:”“’。,、?]|\n|\r|\t";
return matches(text, regEx);
}
}
2.实体类介绍
import java.util.Objects;
public class SimpleUser {
private int id ;
private String username;
private String password;
private String realname;
private int status;
@Override
public String toString() {
return "SimpleUser{" +
"id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
", realname='" + realname + '\'' +
", status=" + status +
'}';
}
public SimpleUser() {
}
public SimpleUser(String username, String password, String realname,int status) {
this.username = username;
this.password = password;
this.realname = realname;
this.status=status;
}
public int getStatus() { return status; }
public void setStatus(int status) { this.status = status; }
public Boolean isEnable(){return status==1;}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getRealname() {
return realname;
}
public void setRealname(String realname) {
this.realname = realname;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
SimpleUser that = (SimpleUser) o;
return id == that.id;
}
@Override
public int hashCode() {
return Objects.hash(id);
}
}
3.调用类中的方法用private封装调用
private void ValidateUser(SimpleUser user){
if(ValidationUtils.isEmpty(user.getUsername())){
throw new IllegalArgumentException("用户名为空");
}
if(!ValidationUtils.digitsAndLetterOnly(user.getUsername())){
throw new IllegalArgumentException("用户名只能包含数字或英文");
}
if(ValidationUtils.isOverLength(user.getUsername(),10)){
throw new IllegalArgumentException("用户名超长");
}
if(ValidationUtils.containsSpecialWord(user.getRealname())){
throw new IllegalArgumentException("真实姓名含非法字符");
}
if(ValidationUtils.isEmpty(user.getRealname())){
throw new IllegalArgumentException("真实姓名为空");
}
if(ValidationUtils.isOverLength(user.getRealname(),10)){
throw new IllegalArgumentException("真实姓名超长");
}
if(ValidationUtils.containsSpecialWord(user.getRealname())){
throw new IllegalArgumentException("真实姓名包含非法字符");
}
}