zoukankan      html  css  js  c++  java
  • springmvc validator和springContext validator的区别

    1.springContext validator 依赖于代理实现

    MethodValidationInterceptor

            Set<ConstraintViolation<Object>> result;
    
            try {
                result = execVal.validateParameters(
                        invocation.getThis(), methodToValidate, invocation.getArguments(), groups);
            }
            catch (IllegalArgumentException ex) {
                // Probably a generic type mismatch between interface and impl as reported in SPR-12237 / HV-1011
                // Let's try to find the bridged method on the implementation class...
                methodToValidate = BridgeMethodResolver.findBridgedMethod(
                        ClassUtils.getMostSpecificMethod(invocation.getMethod(), invocation.getThis().getClass()));
                result = execVal.validateParameters(
                        invocation.getThis(), methodToValidate, invocation.getArguments(), groups);
            }
            if (!result.isEmpty()) {
                throw new ConstraintViolationException(result);
            }
    
            Object returnValue = invocation.proceed();

    2.springmvc validator 在 RequestResponseBodyMethodProcessor 中处理

    @Override
        public void validate(Object target, Errors errors, Object... validationHints) {
            if (this.targetValidator != null) {
                processConstraintViolations(
                        this.targetValidator.validate(target, asValidationGroups(validationHints)), errors);
            }
        }

    主要区别:mvc会帮我们对返回的Set<ConstraintViolation<Object>> violations 进行封装,易于返回

        protected void processConstraintViolations(Set<ConstraintViolation<Object>> violations, Errors errors) {
            for (ConstraintViolation<Object> violation : violations) {
                String field = determineField(violation);
                FieldError fieldError = errors.getFieldError(field);
                if (fieldError == null || !fieldError.isBindingFailure()) {
                    try {
                        ConstraintDescriptor<?> cd = violation.getConstraintDescriptor();
                        String errorCode = determineErrorCode(cd);
                        Object[] errorArgs = getArgumentsForConstraint(errors.getObjectName(), field, cd);
                        if (errors instanceof BindingResult) {
                            // Can do custom FieldError registration with invalid value from ConstraintViolation,
                            // as necessary for Hibernate Validator compatibility (non-indexed set path in field)
                            BindingResult bindingResult = (BindingResult) errors;
                            String nestedField = bindingResult.getNestedPath() + field;
                            if (nestedField.isEmpty()) {
                                String[] errorCodes = bindingResult.resolveMessageCodes(errorCode);
                                ObjectError error = new ObjectError(
                                        errors.getObjectName(), errorCodes, errorArgs, violation.getMessage());
                                error.wrap(violation);
                                bindingResult.addError(error);
                            }
                            else {
                                Object rejectedValue = getRejectedValue(field, violation, bindingResult);
                                String[] errorCodes = bindingResult.resolveMessageCodes(errorCode, field);
                                FieldError error = new FieldError(errors.getObjectName(), nestedField,
                                        rejectedValue, false, errorCodes, errorArgs, violation.getMessage());
                                error.wrap(violation);
                                bindingResult.addError(error);
                            }
                        }
                        else {
                            // got no BindingResult - can only do standard rejectValue call
                            // with automatic extraction of the current field value
                            errors.rejectValue(field, errorCode, errorArgs, violation.getMessage());
                        }
                    }
                    catch (NotReadablePropertyException ex) {
                        throw new IllegalStateException("JSR-303 validated property '" + field +
                                "' does not have a corresponding accessor for Spring data binding - " +
                                "check your DataBinder's configuration (bean property versus direct field access)", ex);
                    }
                }
            }
        }
    if (binder.getBindingResult().hasErrors() && isBindExceptionRequired(binder, parameter)) {
                        throw new MethodArgumentNotValidException(parameter, binder.getBindingResult());
                    }
  • 相关阅读:
    jquery $.getJSON()跨域请求
    JQuery 字符串转时间格式
    php Function ereg() is deprecated的解决方法
    ucenter 验证码看不到的解决办法
    C#通过UserAgent判断智能设备(Android,IOS,PC,Mac)
    gpio_irq出现错误genirq: Setting trigger mode 6 for irq 168 failed (gpio_set_irq_type+0x0/0x230)
    驱动模块的加载(linux4.1.15)!
    电压环控制逻辑!
    用电阻检测大电流时2线电阻的PCB画法。
    比较两点压差(比如两点温度相差太大),超过范围,做出动作!
  • 原文地址:https://www.cnblogs.com/z-test/p/11578115.html
Copyright © 2011-2022 走看看