Java 怎么调用JSF验证器(validator)

2019年8月14日22:02:27 评论 31

1.简介

Java 怎么调用JSF验证器(validator)?——请看下文。

2.代码

/**
 * 版权所有 编程十万个怎么办(www.tah1986.com)
 */

 
import javax.el.ValueExpression;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.faces.el.EvaluationException;
import javax.faces.el.MethodBinding;
import javax.faces.el.ValueBinding;
import javax.faces.validator.Validator;
import javax.faces.validator.ValidatorException;
import java.util.Collection;

public class Main{
    static void callValidators(FacesContext context, UIInput input,
            Object convertedValue) {
        
        Validator[] validators = input.getValidators();
        for (int i = 0; i < validators.length; i++) {
            Validator validator = validators[i];
            try {
                validator.validate(context, input, convertedValue);
            } catch (ValidatorException e) {
                input.setValid(false);

                String validatorMessage = input.getValidatorMessage();
                if (validatorMessage != null) {
                    context.addMessage(input.getClientId(context),
                            new FacesMessage(FacesMessage.SEVERITY_ERROR,
                                    validatorMessage, validatorMessage));
                } else {
                    FacesMessage facesMessage = e.getFacesMessage();
                    if (facesMessage != null) {
                        facesMessage
                                .setSeverity(FacesMessage.SEVERITY_ERROR);
                        context.addMessage(input.getClientId(context),
                                facesMessage);
                    }
                    Collection<FacesMessage> facesMessages = e
                            .getFacesMessages();
                    if (facesMessages != null) {
                        for (FacesMessage message : facesMessages) {
                            message.setSeverity(FacesMessage.SEVERITY_ERROR);
                            context.addMessage(input.getClientId(context),
                                    message);
                        }
                    }
                }
            }
        }

        
        
        MethodBinding validatorBinding = input.getValidator();
        if (validatorBinding != null) {
            try {
                validatorBinding.invoke(context, new Object[] { context,
                        input, convertedValue });
            } catch (EvaluationException e) {
                input.setValid(false);
                Throwable cause = e.getCause();
                if (cause instanceof ValidatorException) {
                    String validatorMessage = input.getValidatorMessage();
                    if (validatorMessage != null) {
                        context.addMessage(input.getClientId(context),
                                new FacesMessage(
                                        FacesMessage.SEVERITY_ERROR,
                                        validatorMessage, validatorMessage));
                    } else {
                        FacesMessage facesMessage = ((ValidatorException) cause)
                                .getFacesMessage();
                        if (facesMessage != null) {
                            facesMessage
                                    .setSeverity(FacesMessage.SEVERITY_ERROR);
                            context.addMessage(input.getClientId(context),
                                    facesMessage);
                        }
                        Collection<FacesMessage> facesMessages = ((ValidatorException) cause)
                                .getFacesMessages();
                        if (facesMessages != null) {
                            for (FacesMessage message : facesMessages) {
                                message.setSeverity(FacesMessage.SEVERITY_ERROR);
                                context.addMessage(
                                        input.getClientId(context), message);
                            }
                        }
                    }
                } else {
                    throw e;
                }
            }
        }
    }
}

 

3.总结

示例代码演示了 Java 如何调用JSF验证器(validator),具体步骤参见示例代码。

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: