本文介绍了如何在JSF 2.2中使用方法参数级别验证?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个适用于我的bean setter方法的bean验证程序.我没有出现JSF验证错误,而是引发了异常.有没有办法使这项工作有效,或者我应该使用传统的JSF验证器?

I have created a bean validator that I apply to my bean setter method. Instead of getting a JSF validation error, I get an exception raised. Is there a way to make this work, or I should go with a traditional JSF validator?

//Bean Method
public void setGuestPrimaryEmail(@ValidEmail String email){
   guest.getEmails().get(0).setValue(email);
}

//Validator interface
@Target({ElementType.FIELD,ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
@Constraint(validatedBy = EmailValidator.class)
public @interface ValidEmail {

    String message() default "{invalid}";

    Class<? extends Payload>[] payload() default {};

    Class<?>[] groups() default {};

}

//Validator impl
public class EmailValidator implements ConstraintValidator<ValidEmail, String> {

    private Pattern p;

    @Override
    public void initialize(ValidEmail constraintAnnotation) {
        p = java.util.regex.Pattern
                .compile("[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?");
    }

    @Override
    public boolean isValid(String value, ConstraintValidatorContext context) {
        if (DothatUtils.isEmpty(value)) {
            return true;
        }

        boolean invalid = !p.matcher(value).matches();
        if (invalid)
            return false;

        return true;
    }

}

例外:

2013-11-30T20:58:41.747+0000|SEVERE: javax.faces.component.UpdateModelException:
javax.el.ELException: /index.xhtml @144,86 value="....":
javax.validation.ConstraintViolationException: 1 constraint violation(s) occurred during method validation.

注意:我将GF4与JSF 2.2.4一起使用.如果我将自定义注释放在字段上,它将按预期工作.

Note: I am using GF4 with JSF 2.2.4. If I place my custom annotation on the field, it works as expected.

推荐答案

ElementType.PARAMETER.

来自 JSR303 1.0规范:

约束注释可以针对以下任何ElementType:

Constraint annotations can target any of the following ElementTypes:

  • FIELD用于受约束的属性
  • METHOD用于受约束的吸气剂
  • TYPE用于受约束的豆子
  • ANNOTATION_TYPE用于构成其他约束的约束
  • FIELD for constrained attributes
  • METHOD for constrained getters
  • TYPE for constrained beans
  • ANNOTATION_TYPE for constraints composing other constraints

虽然其他ElementType未被禁止,但是提供者不必识别和处理对此类类型的约束.内置类型确实支持PARAMETERCONSTRUCTOR以允许Bean验证提供程序特定的扩展.对于自定义注释,采用相同的方法被认为是一种好习惯.

While other ElementTypes are not forbidden, the provider does not have to recognize and process constraints placed on such types. Built-in types do support PARAMETER and CONSTRUCTOR to allow Bean Validation provider specific extensions. It is considered good practice to follow the same approach for custom annotations.

您确实必须将约束注释放在属性(由ElementType.FIELD标识)或吸气剂(由ElementType.METHOD标识)上.请注意,不支持受约束的设置器!

You really have to put the constraint annotation on the property (identified by ElementType.FIELD) or on the getter (identified by ElementType.METHOD). Note that a constrained setter is not supported!

这篇关于如何在JSF 2.2中使用方法参数级别验证?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-02 17:37