当前位置: 首页 > news >正文

网站建设的原则有哪些方面锦州网站建设动态

网站建设的原则有哪些方面,锦州网站建设动态,深圳做生鲜的网站叫什么,网站建设招标2017Java注解Annotation用起来很方便#xff0c;也越来越流行#xff0c;由于其简单、简练且易于使用等特点#xff0c;很多开发工具都提供了注解功能#xff0c;不好的地方就是代码入侵比较严重#xff0c;所以使用的时候要有一定的选择性。 这篇文章将利用注解#xff0c;来… Java注解Annotation用起来很方便也越来越流行由于其简单、简练且易于使用等特点很多开发工具都提供了注解功能不好的地方就是代码入侵比较严重所以使用的时候要有一定的选择性。 这篇文章将利用注解来做一个Bean的数据校验。 下载 http://download.csdn.net/download/hanghangaidoudou/10139375 项目结构  定义注解 该注解可以验证成员属性是否为空长度提供了几种常见的正则匹配也可以使用自定义的正则去判断属性是否合法同时可以为该成员提供描述信息。 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 package org.xdemo.validation.annotation; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; import org.xdemo.validation.RegexType; /**  * 数据验证  * author Goofy  */ Retention(RetentionPolicy.RUNTIME) Target({ElementType.FIELD,ElementType.PARAMETER}) public interface DV {           //是否可以为空     boolean nullable() default false;           //最大长度     int maxLength() default 0;           //最小长度     int minLength() default 0;           //提供几种常用的正则验证     RegexType regexType() default RegexType.NONE;           //自定义正则验证     String regexExpression() default ;           //参数或者字段描述,这样能够显示友好的异常信息     String description() default ; } 注解的解析 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 package org.xdemo.validation.annotation.support; import java.lang.reflect.Field; import org.xdemo.validation.RegexType; import org.xdemo.validation.annotation.DV; import org.xdemo.validation.utils.RegexUtils; import org.xdemo.validation.utils.StringUtils; /**  * 注解解析  * author Goofy  */ public class ValidateService {           private static DV dv;           public ValidateService() {         super();     }           //解析的入口     public static void valid(Object object) throws Exception{         //获取object的类型         Class? extends Object clazzobject.getClass();         //获取该类型声明的成员         Field[] fieldsclazz.getDeclaredFields();         //遍历属性         for(Field field:fields){             //对于private私有化的成员变量通过setAccessible来修改器访问权限             field.setAccessible(true);             validate(field,object);             //重新设置会私有权限             field.setAccessible(false);         }     }                 public static void validate(Field field,Object object) throws Exception{         String description;         Object value;         //获取对象的成员的注解信息         dvfield.getAnnotation(DV.class);         valuefield.get(object);                   if(dvnull)return;                   descriptiondv.description().equals()?field.getName():dv.description();                   /*************注解解析工作开始******************/         if(!dv.nullable()){             if(valuenull||StringUtils.isBlank(value.toString())){                 throw new Exception(description不能为空);             }         }                   if(value.toString().length()dv.maxLength()dv.maxLength()!0){             throw new Exception(description长度不能超过dv.maxLength());         }                   if(value.toString().length()dv.minLength()dv.minLength()!0){             throw new Exception(description长度不能小于dv.minLength());         }                   if(dv.regexType()!RegexType.NONE){             switch (dv.regexType()) {                 case NONE:                     break;                 case SPECIALCHAR:                     if(RegexUtils.hasSpecialChar(value.toString())){                         throw new Exception(description不能含有特殊字符);                     }                     break;                 case CHINESE:                     if(RegexUtils.isChinese2(value.toString())){                         throw new Exception(description不能含有中文字符);                     }                     break;                 case EMAIL:                     if(!RegexUtils.isEmail(value.toString())){                         throw new Exception(description地址格式不正确);                     }                     break;                 case IP:                     if(!RegexUtils.isIp(value.toString())){                         throw new Exception(description地址格式不正确);                     }                     break;                 case NUMBER:                     if(!RegexUtils.isNumber(value.toString())){                         throw new Exception(description不是数字);                     }                     break;                 case PHONENUMBER:                     if(!RegexUtils.isPhoneNumber(value.toString())){                         throw new Exception(description不是数字);                     }                     break;                 default:                     break;             }         }                   if(!dv.regexExpression().equals()){             if(value.toString().matches(dv.regexExpression())){                 throw new Exception(description格式不正确);             }         }         /*************注解解析工作结束******************/     } } 用到的几个类 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 package org.xdemo.validation; /**  * 常用的数据类型枚举  * author Goofy  *  */ public enum RegexType {           NONE,     SPECIALCHAR,     CHINESE,     EMAIL,     IP,      NUMBER,     PHONENUMBER;       } 其中正则验证类和字符串工具类请参考以下链接 SuperUtil之RegexUtils SuperUtil之StringUtils 使用方法 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 package org.xdemo.validation.test; import org.xdemo.validation.RegexType; import org.xdemo.validation.annotation.DV; public class User {           DV(description用户名,minLength6,maxLength32,nullablefalse)     private String userName;           private String password;           DV(description邮件地址,nullablefalse,regexTypeRegexType.EMAIL)     private String email;                 public User(){}           public User(String userName, String password, String email) {         super();         this.userName  userName;         this.password  password;         this.email  email;     }                       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 getEmail() {         return email;     }     public void setEmail(String email) {         this.email  email;     } } 测试代码 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 package org.xdemo.validation.test; import org.xdemo.validation.annotation.support.ValidateService; /**  * author Goofy  */ public class Test {     public static void main(String[] args){         User usernew User(张三, xdemo.org, 252878950qq.com);         try {             ValidateService.valid(user);         } catch (Exception e) {             e.printStackTrace();         }         usernew User(zhangsan,xdemo.org,xxx);         try {             ValidateService.valid(user);         } catch (Exception e) {             e.printStackTrace();         }         usernew User(zhangsan,xdemo.org,);         try {             ValidateService.valid(user);         } catch (Exception e) {             e.printStackTrace();         }     } } 运行效果
http://www.huolong8.cn/news/489743/

相关文章:

  • 基于django的电子商务网站设计深圳网站建设吗
  • 网页区设计网站诊断下载ppt模板免费的网站
  • 网站开发设计文档模板wordpress 钻石 插件
  • 织梦cms网站模板前端兼职一个静态页面报价
  • 重庆网站建设首选卓光广西哪家公司做网站的
  • 企业网站个人备案wordpress安装模版500
  • 深圳h5响应式网站建设中国建筑招聘
  • 安徽做网站公司哪家好一点公司网站怎么规范管理的
  • 主题教育网站建立网页与网站设计
  • jsp网站开发详解下载北京关键词优化服务
  • 模板网站怎么做才美观营销推广ppt
  • 重庆营销型网站随做的好百度竞价排名案例分析
  • 网站仿制网站开发税收标准
  • 网站一个月个人网页设计论文的开题报告
  • 快站app下载建立网站用英语怎么说
  • 好站站网站建设二手书网站建设的目的
  • 青岛建设公司网站手机网站模板更换方法
  • 网站维护的要求包括哪些微信小程序低代码平台
  • 网站的pv统计功能怎样做网站建设公司做网站需要注意什么
  • 网站自己推广怎么做网页开发工作室
  • 国内外做gif的网站阿里巴巴seo排名优化
  • 苍南县规划建设局网站专业网站建设提供商
  • 能进入各种网站的浏览器最新国际新闻摘抄
  • php网站开发练手项目免费帮朋友做网站
  • 郑州做网站 艾特沧州网站制作多少钱
  • c 小说网站开发教程如何网站专题制作
  • 国内flex做的网站商务网站建设ppt模板
  • wordpress怎么ftp建站网络工程师工资一般多少的
  • 17一起做网站童装手工制作迷你抓娃娃机
  • 知名网站建设平台网站安装教程