Spring bean验证消息解析

时间:2015-04-17 23:20:00

标签: java spring bean-validation thymeleaf

我想做一个非常具体的任务,获取Object中每个字段的所有验证消息。 第一项任务很简单,Annotations中的所有Object字段也是递归的,已经完成了。 (来自html5val dialect for thymeleaf的修改代码)

private List<Annotation> fieldAnnotations() {
    Field field = this.fieldFinder.findField(this.targetClass, this.targetFieldName);
    if (field != null) {
        List<Annotation> annotations = Arrays.asList(field.getAnnotations());
        List<Annotation> toAdd = new ArrayList<>();
        for(Annotation a:annotations)
            if(a.annotationType().isAssignableFrom(Valid.class)){
                toAdd.addAll(new AnnotationExtractor(field.getType()).getAnnotationsForField(this.targetFieldName));
            }
            else
                toAdd.add(a);
        return toAdd;
    }
    return Collections.emptyList();
}

现在我试图通过国际化为每个注释获取消息。

    BeanPropertyBindingResult binding = new BeanPropertyBindingResult(realObject, root);
    for (Annotation constraint : constraints) {
        String message = AnnotationExtractor.getDefaultMessage(constraint);
        binding.rejectValue(fieldName, constraint.annotationType().getSimpleName(), message); 
    }
    List<ObjectError> errors = binding.getAllErrors();
    RequestContext requestContext = (RequestContext) arguments.getContext().getVariables().get(SpringContextVariableNames.SPRING_REQUEST_CONTEXT);
    for(ObjectError e:errors){
        String s =requestContext.getMessage(e, true);
    }

如果MessageSource可以解析某些内容,我会收到国际化的消息,非常好! 遗憾的是,我无法收到默认邮件的消息,例如org.hibernate.validator.constraints.Length.message,但它不是那么有问题(我总是可以通过我的MessageSource提供这些消息。)

为了使这项任务完全正常,我想念一件事,即消息的争论。所以,已解决的消息看起来像这样。 Alias length must be between {2} and {1}BeanPropertyBindingResult有一个方法来拒绝带参数的值,但我不知道如何从Annotation中获取它。它可能是由Validator实施完成的,对吧?

与Spring DataBinder相同的工作是针对无效字段,但我想在HTML5表单验证中获取此消息以获取自定义验证消息。有人知道如何通过Spring内部使用对象推送bean来获取这些消息吗?

还有一件重要的事情,一切都在百里香的语境中(这是我对html5val方言的修改)

1 个答案:

答案 0 :(得分:0)

解决方案很难看。 完整的解决方案是我的HTML5验证方言的分支 https://bitbucket.org/globalbus/html5-validator-dialect-fork/commits/85da11b2c13dfbd90ff05a55014d792cb453d5e2

首先,我需要SpringValidatorAdapter.getArgumentsForConstraints()公开。

public class MyValidationAdapter extends SpringValidatorAdapter{
public MyValidationAdapter(Validator targetValidator) {
    super(targetValidator);
}
@Override
public Object[] getArgumentsForConstraint(String objectName,
        String field, ConstraintDescriptor<?> descriptor) {
    return super.getArgumentsForConstraint(objectName, field, descriptor);
}
@Override
public void processConstraintViolations(
        Set<ConstraintViolation<Object>> violations, Errors errors) {
    super.processConstraintViolations(violations, errors);
}
}

我们需要为构造函数提供一个Validator对象。我们可以从传递给方言的org.thymeleaf.Arguments获得它

this.requestContext = (RequestContext) this.arguments.getContext().getVariables().get(SpringContextVariableNames.SPRING_REQUEST_CONTEXT);

this.validatorFactory = this.requestContext.getWebApplicationContext().getBean(ValidatorFactory.class);

this.validator = new MyValidationAdapter(this.validatorFactory.getValidator());

现在,真的是凌乱的代码

public void processField(Element fieldElement, String fieldName){
    BeanPropertyBindingResult binding = new BeanPropertyBindingResult(
            this.realObject, this.root);
    PropertyDescriptor rootProp = this.beanDescriptor
            .getConstraintsForProperty(fieldName);
    List<PropertyDescriptor> finalProp = new LinkedList<PropertyDescriptor>();
    if (rootProp.isCascaded())// if it's nested, scan all properties for
                            // annotation
        finalProp.addAll(this.validator.getConstraintsForClass(
                rootProp.getElementClass()).getConstrainedProperties());
    else
        finalProp.add(rootProp);
    for (PropertyDescriptor prop : finalProp)
        for (final ConstraintDescriptor<?> desc : prop
                .getConstraintDescriptors()) {
            Annotation constraint = desc.getAnnotation();
            String className = this.beanDescriptor.getElementClass()
                    .getSimpleName();
            className = Character.toLowerCase(className.charAt(0))
                    + className.substring(1);
            String field = className + "." + prop.getPropertyName();
            String errorCode = constraint.annotationType().getSimpleName();
            Object[] errorArgs = this.validator.getArgumentsForConstraint(
                    this.root, field, desc);
            String message = (String) desc.getAttributes().get(ANNOTATION_MESSAGE);
            if (INTERNAL.matcher(message).find())
                message = this.validatorFactory.getMessageInterpolator().interpolate(
                        message, new Context() {

                            public Object getValidatedValue() {
                                return null;
                            }

                            public ConstraintDescriptor<?> getConstraintDescriptor() {
                                return desc;
                            }
                        });
            String[] errorCodes = binding.resolveMessageCodes(errorCode,
                    field);
            binding.addError(new FieldError(binding.getObjectName(), prop
                    .getPropertyName(), null, false, errorCodes, errorArgs,
                    message));
        }

    List<ObjectError> errors = binding.getAllErrors();
    StringBuilder customValidation = new StringBuilder();
    for (ObjectError e : errors) {
        String s = this.requestContext.getMessage(e, true);
        customValidation.append(s);
        if (!s.endsWith("."))
            customValidation.append(". ");

    }
    String onInvalid = String.format("this.setCustomValidity('%s')",
            customValidation.toString());
    fieldElement.setAttribute(ONINVALID_ATTR, onInvalid);
    fieldElement.setAttribute(ONCHANGE_ATTR, "this.setCustomValidity('')");
}

线索在SpringValidatorAdapter.getArgumentsForConstraints()中,带有有效参数,可以为消息解析提供参数。 内部Hibernate消息可以由MessageInterpolator.interpolate()提供 最终解决方案由RequestContext.getMessage()提供。

这不是一个“干净的解决方案”,我必须在更多用例中进行测试。