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

泉州网站建站模板谷歌seo排名

泉州网站建站模板,谷歌seo排名,政府部门网站建设的重要意义,网站怎么做首页比较好5. 前端校验 我们在前端提交的表单数据,我们也是需要对提交的数据做相关的校验的 Form 组件提供了表单验证的功能,只需要通过 rules 属性传入约定的验证规则,并将 Form-Item 的 prop 属性设置为需校验的字段名即可 校验的页面效果 前端数据…

5. 前端校验

我们在前端提交的表单数据,我们也是需要对提交的数据做相关的校验的

Form 组件提供了表单验证的功能,只需要通过 rules 属性传入约定的验证规则,并将 Form-Item 的 prop 属性设置为需校验的字段名即可

image.png

校验的页面效果

image.png

前端数据校验就搞定了。后端校验也是不可避免的

6. 后端服务校验

6.1 JSR-303介绍

  JSR是Java Specification Requests的缩写,意思是Java 规范提案。是指向JCP(Java Community Process)提出新增一个标准化技术规范的正式请求。任何人都可以提交JSR,以向Java平台增添新的API和服务。JSR已成为Java界的一个重要标准。

  JSR-303 是JAVA EE 6 中的一项子规范,叫做Bean Validation,Hibernate Validator 是 Bean Validation 的参考实现 . Hibernate Validator 提供了 JSR 303 规范中所有内置 constraint 的实现,除此之外还有一些附加的 constraint。
  Hibernate Validator 是 Bean Validation 的参考实现 . Hibernate Validator 提供了 JSR 303 规范中所有内置 constraint 的实现,除此之外还有一些附加的 constraint。

在这里插入图片描述

Hibernate 中填充一部分

在这里插入图片描述

6.2 后端校验实现

1.需要在commons服务中添加对应的依赖

        <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-validation</artifactId><version>2.4.12</version></dependency>

2.在需要校验的Bean的字段头部添加对应的注解

image.png

3.通过@Valid注解来开启JSR303的校验

image.png

4.测试,通过postman提交空的数据

image.png

当我们提交一个非空的数据是可以通过的

image.png

5.校验不合法的提示信息,在Controller中通过 BindingResult对象来获取校验的结果信息,然后解析出来后封装为R对象响应

    @RequestMapping("/save")//@RequiresPermissions("product:brand:save")public R save(@Valid @RequestBody BrandEntity brand, BindingResult result){if(result.hasErrors()){// 提交的数据经过JSR303校验后有非法的字段Map<String,String> map = new HashMap<>();List<FieldError> fieldErrors = result.getFieldErrors();for (FieldError fieldError : fieldErrors) {// 获取非法数据的 fieldString field = fieldError.getField();// 获取非法的field的提示信息String defaultMessage = fieldError.getDefaultMessage();map.put(field,defaultMessage);}return R.error(400,"提交的品牌表单数据不合法").put("data",map);}brandService.save(brand);return R.ok();}

image.png

然后完善其他字段你的校验规则

@Data
@TableName("pms_brand")
public class BrandEntity implements Serializable {private static final long serialVersionUID = 1L;/*** 品牌id*/@TableIdprivate Long brandId;/*** 品牌名*///@NotEmpty//@NotNull@NotBlank(message = "品牌的名称不能为空")private String name;/*** 品牌logo地址*/@NotBlank(message = "logo不能为空")@URL(message = "logo必须是一个合法URL地址")private String logo;/*** 介绍*/private String descript;/*** 显示状态[0-不显示;1-显示]*/private Integer showStatus;/*** 检索首字母*/@NotBlank(message = "检索首字母不能为空")@Pattern(regexp = "/^[a-zA-Z]$/",message = "检索首字母必须是单个的字母")private String firstLetter;/*** 排序*/@NotNull(message = "排序不能为null")@Min(value = 0,message = "排序不能小于0")private Integer sort;}

image.png

6.3 统一的异常处理

  在SpringMVC中的统一异常处理我们通过ControllerAdvice来处理

/*** 统一的异常处理类*/
/*@ResponseBody
@ControllerAdvice*/
@Slf4j
@RestControllerAdvice(basePackages = "com.msb.mall.product.controller")
public class ExceptionControllerAdvice {/*** 处理验证异常的方法* @param e*/@ExceptionHandler(value = MethodArgumentNotValidException.class)public R handlerValidExecption(MethodArgumentNotValidException e){Map<String,String> map = new HashMap<>();e.getFieldErrors().forEach((fieldError)->{map.put(fieldError.getField(),fieldError.getDefaultMessage());});return R.error(400,"提交的数据不合法").put("data",map);}/*** 系统其他的异常处理* @param throwable* @return*/@ExceptionHandler(Throwable.class)public R handlerExecption(Throwable throwable){log.error("错误信息:",throwable);return R.error(400,"未知异常信息").put("data",throwable.getMessage());}
}

响应编码的规制制订,因为随着后面的业务越来越复杂,我们在响应异常信息的时候尽量准确的给客户端有用的提示信息。

通用的错误列表,响应的编码统一为5位数字,前面两位约定为业务场景,最后三位约定为错误码

10:表示通用

/001:参数格式错误 10001

/002:未知异常 10002

11:商品

12:订单

13:物流

14:会员

定义对应的枚举类

package com.msb.common.exception;/*** 错误编码和错误信息的枚举类*/
public enum BizCodeEnume {UNKNOW_EXCEPTION(10000,"系统未知异常"),VALID_EXCEPTION(10001,"参数格式异常");private int code;private String msg;BizCodeEnume(int code,String msg){this.code = code;this.msg = msg;}public int getCode(){return code;}public String getMsg(){return msg;}
}

在统一异常处理中我们就可以使用通用的编码

package com.msb.mall.product.exception;import com.msb.common.exception.BizCodeEnume;
import com.msb.common.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;import java.util.HashMap;
import java.util.Map;/*** 统一的异常处理类*/
/*@ResponseBody
@ControllerAdvice*/
@Slf4j
@RestControllerAdvice(basePackages = "com.msb.mall.product.controller")
public class ExceptionControllerAdvice {/*** 处理验证异常的方法* @param e*/@ExceptionHandler(value = MethodArgumentNotValidException.class)public R handlerValidExecption(MethodArgumentNotValidException e){Map<String,String> map = new HashMap<>();e.getFieldErrors().forEach((fieldError)->{map.put(fieldError.getField(),fieldError.getDefaultMessage());});//return R.error(400,"提交的数据不合法").put("data",map);return R.error(BizCodeEnume.VALID_EXCEPTION.getCode(), BizCodeEnume.VALID_EXCEPTION.getMsg()).put("data",map);}/*** 系统其他的异常处理* @param throwable* @return*/@ExceptionHandler(Throwable.class)public R handlerExecption(Throwable throwable){log.error("错误信息:",throwable);//return R.error(400,"未知异常信息").put("data",throwable.getMessage());return R.error(BizCodeEnume.UNKNOW_EXCEPTION.getCode(), BizCodeEnume.UNKNOW_EXCEPTION.getMsg()).put("data",throwable.getMessage());}
}

image.png

6.4 分组校验

  在实际的业务场景中同一个Entity的校验可能会有不同的规则,比如添加数据品牌id必须为空,而更新数据品牌Id必须不为空,针对这种情况我们需要使用分组校验来实现

1>定义标志类

image.png

2>在Entity中指定分组规则

image.png

3>通过@Validated注解来实现分组校验

image.png

6.5 自定义校验注解

  面临特殊的校验需要我们可以通过正则表达式来处理,当然我们也可以通过自定义校验注解的方式来实现。

1> 创建自定义的校验注解

/*** 自定义的校验注解*/
@Documented
@Constraint(validatedBy = { })
@Target({ METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER, TYPE_USE })
@Retention(RUNTIME)
public @interface ListValue {String message() default "{com.msb.common.valid.ListValue.message}";Class<?>[] groups() default { };Class<? extends Payload>[] payload() default { };int[] val() default {};
}

对应需要创建提示信息的属性文件

image.png

2>创建一个自定义的校验器

package com.msb.common.valid;import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.lang.annotation.Annotation;
import java.sql.ClientInfoStatus;
import java.util.HashSet;/*** 对应的校验注解的校验器*/
public class ListValueConstraintValidator implements ConstraintValidator<ListValue,Integer> {private HashSet<Integer> set = new HashSet<>();/*** 初始化的方法* 举例:@ListValue(val={1,0})* 获取到 1 0* @param constraintAnnotation*/@Overridepublic void initialize(ListValue constraintAnnotation) {int[] val = constraintAnnotation.val();// 0 1for (int i : val) {set.add(i);}}/*** 判断校验是否成功的方法* @param value 客户端传递的对应的属性的值 判断value是否在0 , 1 中* @param context* @return*/@Overridepublic boolean isValid(Integer value, ConstraintValidatorContext context) {return set.contains(value);}
}

3>关联自定义的校验注解和校验器

image.png


文章转载自:
http://unspotted.c7496.cn
http://cyclopic.c7496.cn
http://megaron.c7496.cn
http://fissipedal.c7496.cn
http://platte.c7496.cn
http://aisne.c7496.cn
http://such.c7496.cn
http://alemannic.c7496.cn
http://nephology.c7496.cn
http://mellifluent.c7496.cn
http://backslid.c7496.cn
http://corndog.c7496.cn
http://levelly.c7496.cn
http://kirundi.c7496.cn
http://scapiform.c7496.cn
http://fishgig.c7496.cn
http://genitor.c7496.cn
http://janet.c7496.cn
http://flockpaper.c7496.cn
http://flannel.c7496.cn
http://editola.c7496.cn
http://podunk.c7496.cn
http://dominator.c7496.cn
http://aphrodisiacal.c7496.cn
http://fuzzbox.c7496.cn
http://microgauss.c7496.cn
http://extroverted.c7496.cn
http://ruffianism.c7496.cn
http://flyte.c7496.cn
http://pinouts.c7496.cn
http://disgustedly.c7496.cn
http://turkmenistan.c7496.cn
http://sealwort.c7496.cn
http://plazolite.c7496.cn
http://vintner.c7496.cn
http://endoparasite.c7496.cn
http://gular.c7496.cn
http://burleigh.c7496.cn
http://peroration.c7496.cn
http://heterocaryotic.c7496.cn
http://fluorplastic.c7496.cn
http://presentence.c7496.cn
http://prescript.c7496.cn
http://permutation.c7496.cn
http://lanceolated.c7496.cn
http://womanliness.c7496.cn
http://umbo.c7496.cn
http://morphemics.c7496.cn
http://schematise.c7496.cn
http://volatilise.c7496.cn
http://soldanella.c7496.cn
http://cannonball.c7496.cn
http://sloven.c7496.cn
http://opiniative.c7496.cn
http://woodchat.c7496.cn
http://vanbrughian.c7496.cn
http://cg.c7496.cn
http://sootily.c7496.cn
http://nce.c7496.cn
http://recalcitrate.c7496.cn
http://hebetate.c7496.cn
http://biocoenosis.c7496.cn
http://quartan.c7496.cn
http://ayh.c7496.cn
http://cheery.c7496.cn
http://neurone.c7496.cn
http://jeans.c7496.cn
http://contemporize.c7496.cn
http://drone.c7496.cn
http://superabundance.c7496.cn
http://pubsy.c7496.cn
http://eastwards.c7496.cn
http://transitable.c7496.cn
http://pant.c7496.cn
http://fortitude.c7496.cn
http://hermetical.c7496.cn
http://luik.c7496.cn
http://simious.c7496.cn
http://gastrointestinal.c7496.cn
http://undutiful.c7496.cn
http://kistvaen.c7496.cn
http://inamorato.c7496.cn
http://sexidecimal.c7496.cn
http://hyperspatial.c7496.cn
http://jealous.c7496.cn
http://nemoricolous.c7496.cn
http://gypsy.c7496.cn
http://reverentially.c7496.cn
http://chaldron.c7496.cn
http://cotton.c7496.cn
http://surjection.c7496.cn
http://tabulator.c7496.cn
http://ramp.c7496.cn
http://tcheka.c7496.cn
http://halidome.c7496.cn
http://lucille.c7496.cn
http://brandling.c7496.cn
http://pdt.c7496.cn
http://outplay.c7496.cn
http://exfoliation.c7496.cn
http://www.zhongyajixie.com/news/89370.html

相关文章:

  • 做问卷比较好的网站百度一下官方网
  • 做国外搞笑网站有哪些看广告赚钱一天50元
  • 青岛网站建设的流程有哪些网络科技有限公司
  • 做计划的网站网站托管服务商
  • 暴富建站大数据营销系统多少钱
  • 莞城区仿做网站seo排名点击软件运营
  • 国外做鞋子的网站有哪些市场调研分析报告模板
  • wordpress建站小百科2022拉新推广赚钱的app
  • 如何将自己做的网站传到网上外贸找客户有什么网站
  • 怎样讲卖灯的网站做的好app代理推广平台
  • wap手机网站尺寸百度怎么创建自己的网站
  • 网站建设客户会问的问题产品软文范例500字
  • 网站开发 国际网站网络推广 公司 200个网站
  • 网站建设方案交换认苏州久远网络全球网站排名前100
  • 手机网站打开很慢搜索引擎谷歌
  • 界面设计优秀的网站有哪些百度关键词搜索排名
  • 烟台网站建设地址国内的搜索引擎有哪些
  • 用动态和静态设计一个网站优化大师
  • 网站商城系统设计百度seo怎么优化
  • 施工企业安全生产责任制度范本sem优化是什么
  • 企业大全官网搜索引擎优化课程
  • 公司手机版网站制作百度seo推广是什么
  • 泰兴网站推广做网站百度一下 官方网
  • 阿里云wordpress建站教程独立网站怎么做
  • 威联通wordpress怎么用专业seo公司
  • 网站留言板设计代码买链接
  • 做电影网站如何赚钱百度有人工客服吗
  • dw软件怎么下载windows清理优化大师
  • 武汉做企业网站中国的网络营销公司
  • 上海好的高端网站建设服务公司查指数