Invocation Sequence of JSF Converters and Validators

前端 未结 1 1402
臣服心动
臣服心动 2020-12-19 08:18

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:

         


        
相关标签:
1条回答
  • 2020-12-19 08:43

    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.

    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);
        }
    }
    

    (you can see the real source code in UIInput#validate() method)

    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.

    0 讨论(0)
提交回复
热议问题