如何使用Primefaces客户端站点验证(CSV)使用Hibernate验证器

时间:2014-10-07 07:35:50

标签: jsf primefaces hibernate-validator

我遵循了Primefaces 5.1指南,并且我能够使用Bean Validation(JSR 349)进行客户端站点验证(CSV)。我有

<context-param>
    <param-name>primefaces.TRANSFORM_METADATA</param-name>
    <param-value>true</param-value>
</context-param>
<context-param>
    <param-name>primefaces.CLIENT_SIDE_VALIDATION</param-name>
    <param-value>true</param-value>
</context-param>

通过此设置,标准@Size@Pattern无需向服务器进行往返即可运行。但是,来自Hibernate Validator的@Email等注释根本不起作用。 用户指南中的Primefaces将第三方注释描述为

  

使用第三方约束(如Hibernate Validator特定注释)时,请使用   BeanValidationMetadataMapper为它们定义ClientValidationConstraint。

BeanValidationMetadataMapper.registerConstraintMapping(Class<? extends Annotation>
constraint, ClientValidationConstraint clientValidationConstraint);

BeanValidationMetadataMapper.removeConstraintMapping(Class<? extends Annotation>
constraint);

这对我来说听起来很神秘。有谁知道设置应该如何?感谢。

1 个答案:

答案 0 :(得分:0)

对于@NotBlank例如:

package com.algaworks.pedidovenda.util;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;

import org.hibernate.validator.constraints.NotBlank;
import org.primefaces.validate.bean.BeanValidationMetadataMapper;

import com.algaworks.pedidovenda.validation.NotBlankClientValidationConstraint;

@WebListener
public class AppContextListener implements ServletContextListener {

@Override
public void contextDestroyed(ServletContextEvent event) {
}

@Override
public void contextInitialized(ServletContextEvent event) {
    System.setProperty("org.apache.el.parser.COERCE_TO_ZERO", "false");

    BeanValidationMetadataMapper.registerConstraintMapping(NotBlank.class, 
            new NotBlankClientValidationConstraint());
}

}

ClientValidationConstraint本身:

package com.algaworks.pedidovenda.validation;

import java.util.HashMap;
import java.util.Map;

import javax.validation.metadata.ConstraintDescriptor;

import org.hibernate.validator.constraints.NotBlank;
import org.primefaces.validate.bean.ClientValidationConstraint;

public class NotBlankClientValidationConstraint implements  ClientValidationConstraint {

public static final String MESSAGE_ID = "{org.hibernate.validator.constraints.NotBlank.message}";

@SuppressWarnings("rawtypes")
@Override
public Map<String, Object> getMetadata(ConstraintDescriptor constraintDescriptor) {
    Map<String, Object> metadata = new HashMap<String, Object>();
    Map attrs = constraintDescriptor.getAttributes();

    Object message = attrs.get("message");

    if (!message.equals(MESSAGE_ID)) {
        metadata.put("data-msg-notblank", message);
    }

    return metadata;
}

@Override
public String getValidatorId() {
    return NotBlank.class.getSimpleName();
}

}

假设代码有效,因为它是&#34;借用&#34;从这里:

Listener

ClientValidationConstraint

不要忘记:

PrimeFaces.validator.NotBlank = {

MESSAGE_ID : 'org.hibernate.validator.constraints.NotBlank.message',

validate : function(element, value) {
    if (value === null || value === undefined || value.trim() === '') {
        var msg = element.data('msg-notblank');
        var label = element.data('p-label');
        var context = PrimeFaces.util.ValidationContext;
        var msgObj;

        if (!msg) {
            msgObj = context.getMessage(this.MESSAGE_ID, label);
        } else {
            var msgObj = {
                summary : msg,
                detail : msg
            }
        }

        throw msgObj;
    }
}

};