注解验证参数的合法性
2013-10-11 16:04
573 查看
一、定义需要验证的参数类型
二、编写注解类
三、验证参数
四、在需验证的实体类中添加注解验证
五、在Servlet(Action)调用验证
SpringMVC 支持Hibernate Validator:http://my.oschina.net/u/241255/blog/297726
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
相关文章推荐
- 验证用户输入的参数合法性的shell脚本
- spring 参数验证注解文档
- Python装饰器实例:调用参数合法性验证
- SpringMVC验证框架Validation自定义注解实现传递参数到国际化资源文件
- Python装饰器实例:调用参数合法性验证
- Python装饰器实例(1):参数合法性验证
- SpringMVC验证框架Validation自定义注解实现传递参数到国际化资源文件
- 自定义注解验证参数
- java利用注解做参数验证
- Python装饰器实例:调用参数合法性验证
- PowerShell中使用正则和ValidateSet验证参数合法性
- 验证用户输入的参数合法性的shell脚本
- SpringMVC验证框架Validation自定义注解实现传递参数到国际化资源文件
- Python装饰器使用实例:验证参数合法性
- Python装饰器使用实例:验证参数合法性
- Python装饰器实例:调用参数合法性验证
- swagger2的常用注解,传递参数的注意使用方法
- Hibernate Validator 参数验证框架
- OBJ-C邮箱合法性验证实现
- Java Restful Web Services (二)——参数注解1