简体   繁体   English

更改后备bean中的值未反映在UI中

[英]change value in backing bean not reflected in UI

The component is wired via value binding to a backing bean property. 组件通过值绑定连接到支持bean属性。

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

In the validation method the number value is changed 在验证方法中,数值被更改

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

While debugging I can verify that the value is in fact being changed but during the rendering phase the new value is somehow overridden by the old value. 在调试时,我可以验证该值实际上是在被更改,但在呈现阶段,新值以某种方式被旧值覆盖。 This must have something to do with me misunderstanding the JSF lifecycle, but the way I see it I am changing both the value op the property to which to component is bound in the UI and because I have a hook to the actual component I also change the component's value and submittedValue to be sure (to find the problem) and still the change isn't reflected int he UI? 这必须与我误解JSF生命周期有关,但是我看到它的方式我正在改变组件在UI中绑定的属性值,因为我有一个实际组件的钩子我也改变了组件的值和submittedValue以确定(找到问题)并且仍然没有在UI中反映出更改?

Any ideas?? 有任何想法吗??

You're using the wrong tool for the job. 你正在使用错误的工具来完成工作。 You should use a Converter for this, not a Validator . 您应该使用Converter ,而不是Validator A validator is to validate the value, not to change (convert) the value. 验证器用于验证值,而不是更改 (转换)值。

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

}

As to why it isn't possible in a validator, this is what is basically happening: 至于为什么在验证器中不可能,这就是基本上发生的事情:

  • Phase 2: apply request values ( input is UIInput and request is HttpServletRequest ) 阶段2:应用请求值( inputUIInputrequestHttpServletRequest

     input.setSubmittedValue(request.getParameter(input.getClientId())); 
  • Phase 3: validation phase. 阶段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) { // ... } 
  • Phase 4: update model values phase. 阶段4:更新模型值阶段。

     bean.setProperty(input.getValue()); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM