什么时候在 UIComponent 上调用 setValue 和 setSubmittedValue? [英] When are setValue and setSubmittedValue called on UIComponent?

查看:15
本文介绍了什么时候在 UIComponent 上调用 setValue 和 setSubmittedValue?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我正确地结合了 BalusC 2006 年伟大帖子中包含的信息 http://balusc.blogspot.ch/2006/09/debug-jsf-lifecycle.html 与 Optimus Prime 更早的帖子 http://cagataycivici.wordpress.com/2005/12/28/jsf_component_s_value_local/ 我得到以下信息:

If I correctly combined the information contained in BalusC's great 2006 post http://balusc.blogspot.ch/2006/09/debug-jsf-lifecycle.html with Optimus Prime's even earlier post http://cagataycivici.wordpress.com/2005/12/28/jsf_component_s_value_local/ I get the following:

我的理解:

  1. 在 APPLY_REQUEST_VALUES 阶段,
    • 输入值被设置为 UI 组件的 submitValue 属性(例如 inputComponent.setSubmittedValue("test")).
  1. During the APPLY_REQUEST_VALUES phase,
    • the input value is set to a submittedValue property of the UI component (e.g. inputComponent.setSubmittedValue("test")).
  • 如果需要,从 submitValue 属性(可能是 inputComponent.getSubmittedValue())中读取相同的值并用于转换.
  • 如果转换成功或跳过,则将结果设置为组件的 value 属性(例如 inputComponent.setValue("test")).
  • 此外,submittedValue 会立即再次被删除(例如 inputComponent.setSubmittedValue(null))
  • 从 UI 组件的 value 属性(大概是 inputComponent.getValue())读取(转换后的)值并进行验证.
  • 验证后,将读取支持 bean/模型的存储值(例如 myBean.getInputValue())并与新转换和验证的值进行比较.如果不同,将调用 valueChangeListener 方法.
  • the same values are read from the submittedValue property (presumably inputComponent.getSubmittedValue()) and used for conversion, if necessary.
  • If the conversion was successful or skipped, the result is set to a value property of the component (e.g. inputComponent.setValue("test")).
  • Also, the submittedValue is erased again immediately (e.g. inputComponent.setSubmittedValue(null))
  • the (converted) value is read from the value property of the UI component (presumably inputComponent.getValue()) and validated.
  • after validation, the backing bean/model's stored value is read (e.g. myBean.getInputValue()) and compared with the newly converted and validated value. If different, the valueChangeListener method(s) will be called.
  • 新转换和验证的值最终存储在支持 bean 的属性字段中(例如 myBean.setInputValue("test")).
  • the newly converted and validated value is finally stored in the backing bean's property field (e.g. myBean.setInputValue("test")).

问题:

  • 这是正确的吗?
  • 是否缺少完整了解 POST 和将输入值保存到支持 bean 之间的内容?
  • 在 Input Component 上使用 immediate="true",我们只是将这些事件转移到 APPLY_REQUEST_VALUES 阶段,还是我们改变的不仅仅是事件的时间/顺序?

推荐答案

几乎正确.组件的本地值仅在转换验证成功时设置.之后,提交的值设置为 null.您可以在 UIInput#validate() 方法(行号符合 JSF 2.1 API):

Almost correct. The component's local value is only set when conversion and validation is successful. After that, the submitted value is set to null. You can find the entire process of the validations phase in a rather self-documenting way in the UIInput#validate() method (line numbers are conform JSF 2.1 API):

934    public void validate(FacesContext context) {
935 
936         if (context == null) {
937             throw new NullPointerException();
938         }
939 
940         // Submitted value == null means "the component was not submitted
941         // at all".  
942         Object submittedValue = getSubmittedValue();
943         if (submittedValue == null) {
944             return;
945         }
946 
947         // If non-null, an instanceof String, and we're configured to treat
948         // zero-length Strings as null:
949         //   call setSubmittedValue(null)
950         if ((considerEmptyStringNull(context)
951              && submittedValue instanceof String 
952              && ((String) submittedValue).length() == 0)) {
953             setSubmittedValue(null);
954             submittedValue = null;
955         }
956 
957         Object newValue = null;
958 
959         try {
960             newValue = getConvertedValue(context, submittedValue);
961         }
962         catch (ConverterException ce) {
963             addConversionErrorMessage(context, ce);
964             setValid(false);
965         }
966 
967         validateValue(context, newValue);
968 
969         // If our value is valid, store the new value, erase the
970         // "submitted" value, and emit a ValueChangeEvent if appropriate
971         if (isValid()) {
972             Object previous = getValue();
973             setValue(newValue);
974             setSubmittedValue(null);
975             if (compareValues(previous, newValue)) {
976                 queueEvent(new ValueChangeEvent(this, previous, newValue));
977             }
978         }
979 
980     }

至于 UIInput 组件上的 immediate 属性,是的,这只是将验证转移到应用请求值阶段.另见 UIInput#processDecodes()UIInput#processValidators(),有一个检查UIInput#isImmediate().

As to the immediate attribute on the UIInput component, yes this merely shifts the validation to the apply request values phase. See also the source code of UIInput#processDecodes() and UIInput#processValidators(), there's a check on UIInput#isImmediate().

这篇关于什么时候在 UIComponent 上调用 setValue 和 setSubmittedValue?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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