The meaning of the change in bean support is not reflected in the user interface

The component is connected by binding the value to the bean property.

<h:inputText id="number" value="#{backingBean.number}" validator="#{backingBean.validateNumber}" />

In the verification method, the number value changes

public void validateNumber(FacesContext facesContext, UIComponent component, Object value) {
    String inputValue = (String) value;

    if (inputValue.length() == 9) {
        inputValue = "0" + inputValue;
        ((UIInput) component).setSubmittedValue(inputValue);
        ((UIInput) component).setValue(inputValue);
        setNumber(inputValue);
    }
}

During debugging, I can verify that the value is actually changing, but at the rendering stage, the new value is somehow overridden by the old value. This should have something to do with me, not understanding the JSF life cycle, but the way I see it, I change both the value of the op property to which the component is bound in the user interface, and because I have a binding to the real one component, I also change the value of the component and submitValue to be sure (to find the problem), and yet the change does not affect the int UI?

Any ideas?

+3
1

. Converter , Validator. , () .

public void EnterpriseNumberConverter implements Converter {

    @Override
    public Object getAsObject(FacesContext context, UIComponent component, String value) {
        if (value.length() == 9) {
            value = "0" + value;
        }
        return value;
    }

    @Override
    public String getAsString(FacesContext context, UIComponent component, Object value) {
        return (String) value;
    }

}

, , , :

  • 2: (input- UIInput request - HttpServletRequest)

    input.setSubmittedValue(request.getParameter(input.getClientId()));
    
  • 3: .

    Object value = input.getSubmittedValue();
    try {
        value = input.getConvertedValue(facesContext, value);
    } catch (ConverterException e) {
        // ...
        return;
    }
    try {
        for (Validator validator : input.getValidators())
            validator.validate(facesContext, input, value);
        }
        input.setSubmittedValue(null);
        input.setValue(value); // You see?
    } catch (ValidatorException e) {
        // ...
    }
    
  • 4: .

    bean.setProperty(input.getValue());
    
+3

Source: https://habr.com/ru/post/1778970/


All Articles