一、定义需要验证的参数类型
package cn.com.songjy.annotation;
public enum ValidateType {
NotNull,Length,Number
}
二、编写注解类
package cn.com.songjy.annotation;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
@Retention(RetentionPolicy.RUNTIME)
public @interface ValidateAnnotation {
public ValidateType type();
public String errorMsg();
public int length() default 0;
}
三、验证参数
package cn.com.songjy.annotation;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
public class ValidateTools {
public static boolean validate(Class obj, HttpServletRequest request){
//Map<String, String> errors = (Map<String, String>) request.getAttribute("errors");
Map<String, String> errors = new HashMap<String, String>();
Field[] fields = obj.getDeclaredFields();
for (Field field : fields) {
//System.out.println(field.getName());//输出所有属性名称
if(field.isAnnotationPresent(ValidateAnnotation.class)){//只处理包含注解为ValidateAnnotation的属性名称
ValidateAnnotation validateUtil = field.getAnnotation(ValidateAnnotation.class);
ValidateType type = validateUtil.type();
if(ValidateType.NotNull == type)
if(true == isNull(field.getName(), request))
errors.put(field.getName(), validateUtil.errorMsg());
if(ValidateType.Length == type)
if(true == length(field.getName(), request, validateUtil.length()))
errors.put(field.getName(), validateUtil.errorMsg());
if(ValidateType.Number == type)
if(true == isNumber(field.getName(), request))
errors.put(field.getName(), validateUtil.errorMsg());
}
}
return errors.size() == 0;
}
public static boolean isNull(String field, HttpServletRequest request){
if(null==request || null==request.getParameter(field)) return true;
if(0==request.getParameter(field).trim().length()) return true;
return false;
}
public static boolean length(String field, HttpServletRequest request, int length){
if(null==request || null==request.getParameter(field)) return true;
if(length>request.getParameter(field).trim().length()) return true;
return false;
}
public static boolean isNumber(String field, HttpServletRequest request){
if(null==request || null==request.getParameter(field)) return true;
try {
Integer.parseInt(request.getParameter(field));
} catch (NumberFormatException e) {
e.printStackTrace();
return true;
}
return false;
}
}
四、在需验证的实体类中添加注解验证
package cn.com.songjy.model;
import cn.com.songjy.annotation.ValidateType;
import cn.com.songjy.annotation.ValidateAnnotation;
public class User {
@ValidateAnnotation(type=ValidateType.Length, errorMsg="用户名长度不能小于1", length=1)
private String username;
@ValidateAnnotation(type=ValidateType.Number, errorMsg="年龄必须是数字")
private int age;
private String[] interest;
public User(){}
public User(String username, int age){
this.username = username;
this.age = age;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String[] getInterest() {
return interest;
}
public void setInterest(String[] interest) {
this.interest = interest;
}
}
五、在Servlet(Action)调用验证
boolean success = ValidateTools.validate(User.class, request);
SpringMVC 支持Hibernate Validator:http://my.oschina.net/u/241255/blog/297726
来源:oschina
链接:https://my.oschina.net/u/47886/blog/141291