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

宁波教育平台网站建设安卓app开发实例教程

宁波教育平台网站建设,安卓app开发实例教程,天空网站开发者,网页布局的设计步骤目标 自定义一个用于校验邮政编码格式的注解ZipCode#xff0c;能够和现有的 Validation 兼容#xff0c;使用方式和其他校验注解保持一致#xff08;使用 Valid 注解接口参数#xff09;。 校验逻辑 有效格式 不能包含空格#xff1b;应为6位数字#xff1b; 不校验…目标 自定义一个用于校验邮政编码格式的注解ZipCode能够和现有的 Validation 兼容使用方式和其他校验注解保持一致使用 Valid 注解接口参数。 校验逻辑 有效格式 不能包含空格应为6位数字 不校验非空 邮政编码校验的是格式不校验是否为空null 或 空字符串。如果邮政编码为空直接通过校验 核心代码 需要定义的内容包含两个部分注解ZipCode和 校验器ZipCodeValidator。 注解ZipCode package com.example.core.validation.zipcode;import javax.validation.Constraint; import javax.validation.Payload; import java.lang.annotation.Documented; import java.lang.annotation.Retention; import java.lang.annotation.Target;import static java.lang.annotation.ElementType.FIELD; import static java.lang.annotation.RetentionPolicy.RUNTIME;/*** 邮政编码。字符串必须是格式正确的邮政编码。正确格式为6位数字。* p* {code null} 或 空字符串是有效的能够通过校验。* p* 支持的类型字符串** author songguanxun* since 1.0*/ Target({FIELD}) Retention(RUNTIME) Documented Constraint(validatedBy ZipCodeValidator.class) public interface ZipCode {/*** return the error message template*/String message() default 邮政编码格式错误;/*** return the groups the constraint belongs to*/Class?[] groups() default {};/*** return the payload associated to the constraint*/Class? extends Payload[] payload() default {};} 校验器ZipCodeValidator package com.example.core.validation.zipcode;import com.example.core.constant.PatternConstant; import org.springframework.util.ObjectUtils;import javax.validation.ConstraintValidator; import javax.validation.ConstraintValidatorContext; import java.util.regex.Pattern;/*** 邮政编码格式校验器*/ public class ZipCodeValidator implements ConstraintValidatorZipCode, String {Overridepublic void initialize(ZipCode constraintAnnotation) {ConstraintValidator.super.initialize(constraintAnnotation);}Overridepublic boolean isValid(String value, ConstraintValidatorContext context) {if (ObjectUtils.isEmpty(value)) {return true;}if (value.contains( )) {resetMessage(context, 邮政编码格式错误不能包含空格);return false;}if (!isValid(value)) {resetMessage(context, 邮政编码格式错误);return false;}return true;}// 格式为6位数字private static final Pattern PATTERN Pattern.compile(PatternConstant.ZIP_CODE);/*** 是有效的格式*/private boolean isValid(CharSequence input) {return PATTERN.matcher(input).matches();}/*** 重置提示信息*/private void resetMessage(ConstraintValidatorContext context, String messageTemplate) {context.disableDefaultConstraintViolation();context.buildConstraintViolationWithTemplate(messageTemplate).addConstraintViolation();}} 用到的常量 package com.example.core.constant;/*** 模式-常量*/ public class PatternConstant {/*** 全部为数字*/public static final String NUMBERS ^\\d*$;/*** 邮政编码6位数字*/public static final String ZIP_CODE ^\\d{6}$; } 使用 ZipCode 放在需要校验格式的 邮政编码 字段上。 新增用户Param package com.example.web.response.model.param;import com.example.core.constant.PatternConstant; import com.example.core.validation.zipcode.ZipCode; import io.swagger.v3.oas.annotations.media.Schema; import lombok.Data;Schema(name 新增用户Param) public class UserAddParam {// 其他字段ZipCodeSchema(description 邮政编码, example 201100, pattern PatternConstant.ZIP_CODE)private String zipCode;} 校验效果 包含空格 不是6位数字 数字超过6位 包含非数字的字符
http://www.dnsts.com.cn/news/257301.html

相关文章:

  • 江苏省建设教育协会网站高清视频服务器内存
  • 五金网站建设网页编辑哪个键
  • 绍兴建设开发有限公司网站首页上海市公共招聘网官网
  • 澄迈网站建设网站排名关键词
  • 贵州水电建设局网站网站地图设计
  • 应用网站模板如何生成网站
  • 天津市网站建站制作微信小程序开发多少钱
  • wordpress里的导航用什么成都百度推广优化创意
  • 厦门微网站建设公司搞笑图片网站源码
  • 招商加盟网站大全网络公司怎么运营
  • 自适应网站的代表wordpress 取消分类目录
  • 上海网站推广提供商如何建设自己的小说网站
  • 论述网站建设的具体步骤有哪些网站建设分为
  • html5国内网站wordpress 多备份
  • 庆安建设局网站wordpress旅游公司主题
  • 无广告自助建站网站什么情况要更新
  • 企业网站后台管理系统设计上海门票
  • 如何做一个主题网站昆明网站搜索优化
  • 个人网站发布怎么做长10米宽3米的店面设计
  • 做一借款撮合网站学习资料黄页网站免费
  • 海宁网站设计2016手机网站制作规范
  • 肇庆建设银行招聘网站做标书的专业网站
  • 个人网站设计结构图十三五关于网站建设
  • 莆田哪里有学做网站的没学历最吃香的职业
  • 个人信息网站建设的心得体会godaddy 网站怎么建设
  • 投资网站维护婚恋网站建设
  • 佛山网站建设公司哪家性价比高门户网站介绍
  • 电子商务网站加密网站后台尺寸一般做多大的
  • 用二级页面做网站的源代码帝国cms和wordpress哪个更好
  • 蚌埠北京网站建设西安建设工程信息网 人员信息