JSF转换器和验证器的调用序列 [英] Invocation Sequence of JSF Converters and Validators

查看:98
本文介绍了JSF转换器和验证器的调用序列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道调用顺序或流程,将如何调用转换器和验证器.我正在共享相同的示例代码:

I want to know the invocation sequence or the flow how the converters and validators will be invoked. I am sharing a sample code for the same:

<f:view>
    <h:form>
        <h:inputText value="#{myBean.field}">
            <f:validateLength minimum="5" maximum="50"></f:validateLength>
            <f:validator validatorId="nameValidator" />
        </h:inputText>
        <br>
        <h:inputText id="date" value="#{myBean.date}">
            <f:convertDateTime pattern="dd-MMM-yyyy" />
            <f:converter converterId="dateConvertor" />
        </h:inputText>
        <br>
        <h:commandButton action="#{myBean.execute}" value="Submit"></h:commandButton>
    </h:form>
    <h:messages></h:messages>
</f:view>

推荐答案

所有 UIInput 组件(<h:inputText>和朋友)的处理顺序与在JSF组件树中出现的顺序相同.在处理此类组件的过程中,首先调用转换器(看起来没有复数!),然后按与在组件上声明的顺序相同的顺序调用验证器.

All UIInput components (<h:inputText> and friends) are processed in the same order as they appear in the JSF component tree. During the processing of such a component, first the converter (look, no plural!) is invoked and then the validators are invoked in the same sequence as they have been declared on the component.

在过于简化的Java代码中,验证阶段的过程大致如此:

In oversimplified Java code, the procedure is roughy like this during validations phase:

for (UIInput input : inputs) {
    String id = input.getClientId(context);
    Object value = input.getSubmittedValue();
    try {
        value = input.getConvertedValue(context, value);
        for (Validator validator : input.getValidators())
           validator.validate(context, input, value);
        }
        input.setSubmittedValue(null);
        input.setValue(value);
    } catch (ConverterException | ValidatorException e) {
        facesContext.addMessage(id, e.getFacesMessage());
        facesContext.validationFailed(); // Skips phases 4+5.
        input.setValid(false);
    }
}

(您可以在因此,基本上,它的顺序与您在XHTML标记中看到的顺序完全相同.仅在您的第二个输入上,第二个转换器才覆盖第一个转换器.一个输入组件只能有一个转换器.从技术上讲,多个转换器毫无意义.

So, basically, it's in exactly the same order as you see in the XHTML markup. Only on your second input, the second converter has overidden the first converter. An input component can have only one converter. Multiple converters is technically not making any sense.

这篇关于JSF转换器和验证器的调用序列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆