java Action层的校验工具类分享

一个枚举、一个工具

package com.uet.common.utils;

/**
 * 校验
 * 
 * @author 草木全
 * @version 2014年5月24日22:03:00
 */
public enum Validation {
	required("{0}不能为空"), email("{0}的E-mail格式错误"), url("{0}的网址格式错误"), 
	date("{0}格式错误"), number("{0}不是合法的数字"), digits(""), 
	creditcard(""), equalTo(""), accept(""), maxlength("{0}长度不能大于{1}"), minlength("{0}长度不能小于{1}"), 
	rangelength("{0}长度需要介于{1}和{2}之间"), range(""), max("{0}值不能大于{1}"), min("{0}值不能小于{1}"), integer(""), positive(""), 
	positiveInteger(""), mobile(""), phone(""), zipCode(""),  
	username("{0}只允许包含英文、数字和下划线"), prefix("{0}必须是以 {1} 开头的字符串!"), lettersonly("{0}只允许包含字母"), legal("{0}为非法的值"),
	notEqual("{0}输入的不一样");
	
	private String value;

	public String getValue() {
		return value;
	}

	public void setValue(String value) {
		this.value = value;
	}
	
	Validation(String value){
		this.value=value;
	}
}



package com.uet.common.utils;

import java.text.MessageFormat;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang3.StringUtils;
import org.springframework.util.Assert;

public class ValidateUtil {
	private static final String ENTER = "<br/>";
	/**
	 * email正则表达式
	 */
	public static final Pattern EMAIL_PATTERN = Pattern
			.compile("^\\w+(\\.\\w+)*@\\w+(\\.\\w+)+$");
	/**
	 * username正则表达式
	 */
	public static final Pattern USERNAME_PATTERN = Pattern
			.compile("^[0-9a-zA-Z\\.\\-@_]+$");
	public static final Pattern NO_SPECAIL_PATTERN = Pattern
			.compile("^[0-9a-zA-Z\\u4e00-\\u9fa5\\.\\-@_]+$");
	/**
	 * 发生错误放入Map
	 * 
	 * @param errors
	 *            容器
	 * @param validation
	 * @param field
	 *            属性名称:如userName,title,content
	 * @param tips
	 *            用户名 [含在参数args中] 提示名称
	 * @param args
	 *            参数(validation)
	 */
	public static void put(Map<String, String> errors, Validation validation,
			String field, Object... args) {
		if(errors==null){
			errors=new HashMap<String, String>();
		}
		String value = MessageFormat.format(validation.getValue(), args);
		if (errors.containsKey(field)) {
			value = errors.get(field) + ENTER + value;
		} else {
			errors.put(field, value);
		}
	}

	public static void put(Map<String, String> errors, String field,
			String tips, String message) {
		if(errors==null){
			errors=new HashMap<String, String>();
		}
		String value = tips + message;
		if (errors.containsKey(field)) {
			value = errors.get(field) + ENTER + value;
		} else {
			errors.put(field, value);
		}
	}

	public static boolean ifNull(Map<String, String> errors, Object fieldValue,
			String field, String tips) {
		if (fieldValue == null) {
			put(errors, Validation.required, field, tips);
			return true;
		} else {
			return false;
		}
	}

	public static boolean ifEmpty(Map<String, String> errors,
			String fieldValue, String field, String tips) {
		if (StringUtils.isEmpty(fieldValue)) {
			put(errors, Validation.required, field, tips);
			return true;
		} else {
			return false;
		}
	}

	public static boolean ifMaxLength(Map<String, String> errors,
			String fieldValue, String field, String tips, int maxLength) {
		if (fieldValue != null && fieldValue.length() > maxLength) {
			put(errors, Validation.maxlength, field, tips, maxLength);
			return true;
		}
		return false;
	}

	public static boolean ifMinLength(Map<String, String> errors,
			String fieldValue, String field, String tips, int minLength) {
		if (fieldValue != null && fieldValue.length() < minLength) {
			put(errors, Validation.minlength, field, tips, minLength);
			return true;
		}
		return false;
	}
	public static boolean ifNotEqual(Map<String, String> errors,
			String fieldOneValue,String fieldTwoValue, String field, String tips) {
		if (!StringUtils.equals(fieldOneValue, fieldTwoValue)) {
			put(errors, Validation.notEqual, field, tips);
			return true;
		}
		return false;
	}
	public static boolean ifOutOfLength(Map<String, String> errors,
			String fieldValue, String field, String tips, int minLength,
			int maxLength) {
		if (fieldValue == null) {
			put(errors, Validation.minlength, field, tips, minLength);
			return true;
		}
		int len = fieldValue.length();
		if (len < minLength || len > maxLength) {
			put(errors, Validation.rangelength, field, tips,minLength,maxLength);
			return true;
		}
		return false;
	}

	public static boolean ifNotEmail(Map<String, String> errors,
			String fieldValue, String field, String tips) {
		if (fieldValue == null) {
			put(errors, Validation.email, field, tips);
			return true;
		}
		Matcher m = EMAIL_PATTERN.matcher(fieldValue);
		if (!m.matches()) {
			put(errors, Validation.email, field, tips);
			return true;
		}
		return false;
	}

	public static boolean ifNotUsername(Map<String, String> errors,
			String fieldValue, String field, String tips) {
		if (fieldValue == null) {
			put(errors, Validation.username, field, tips);
			return true;
		}
		Matcher m = USERNAME_PATTERN.matcher(fieldValue);
		if (!m.matches()) {
			put(errors, Validation.username, field, tips);
			return true;
		}
		return false;
	}

}



优秀到卓越
分享到:
共 0 条  此列表为空  当前1/1页

© 2014 究问社区 copyRight 豫ICP备13003319号-1