UIInput#getValue() and getLocalValue() after validation succeeds return different values

旧巷老猫 提交于 2019-12-24 17:28:03

问题


Here, it is mentioned by the author.

If it's[COMPONENT] marked valid, then both returns the same value, namely the submitted, converted and validated value.

Consider a very simple snippet:

<h:form>
            <h:inputText value="#{bean.inputValue}" 
                         binding="#{bean.htmlInputText}"      
                         validator="nameValidator" /><br/>
            <h:commandButton value="Submit" action="#{bean.action}" />
</h:form>

with a @RequestScoped backing bean-

public Integer inputValue = 5;
public HtmlInputText htmlInputText;

public void action(){
        System.out.println(" getSubmittedValue() "+htmlInputText.getSubmittedValue());
        System.out.println(" isLocalValueSet() "+ htmlInputText.isLocalValueSet());
        System.out.println(" getValue() " + htmlInputText.getValue());
        System.out.println(" getLocalValue() " +htmlInputText.getLocalValue());
}

On pressing the submit button, output is-

 getSubmittedValue() null    AS EXPECTED, since Conversion & Validation succeded
 isLocalValueSet() false
 getValue() 25               AS EXPECTED, since Conversion & Validation succeded
 getLocalValue() null        Why NULL? IN WHAT CONTEXT HAS THE AUTHOR SAID SO

回答1:


You're checking the local value during invoke application phase.

The local value is cleared out during update model values phase.

The author is talking in context of process validations phase.


To clarify, here's the full process:

RESTORE_VIEW

  • Restore getSubmittedValue(), isValid(), getLocalValue() and isLocalValueSet() from JSF view state, if any.

APPLY_REQUEST_VALUES

  • Do setValid(true) and setSubmittedValue(request.getParameter(getClientId())).

PROCESS_VALIDATIONS

  • Convert/validate getSubmittedValue().
    • If valid, do setValue(convertedAndValidatedValue), setLocalValueSet(true), setSubmittedValue(null). Do note that setValue() effectively behaves as setLocalValue().
    • If invalid, do setValid(false) and skip update model values and invoke application phases.

UPDATE_MODEL_VALUES

  • If valid and local value set, do bean.setProperty(getLocalValue()) and reset getSubmittedValue(), isValid(), getLocalValue() and isLocalValueSet() to their default values of null, false, null and false.

INVOKE_APPLICATION

  • Invoke bean.method().

RENDER_RESPONSE

  • If getSubmittedValue() is not null, render it, else if isLocalValueSet() returns true, render getLocalValue(), else render bean.getProperty().
  • Save getSubmittedValue(), isValid(), getLocalValue() and isLocalValueSet() in JSF view state, if changed.


来源:https://stackoverflow.com/questions/36907050/uiinputgetvalue-and-getlocalvalue-after-validation-succeeds-return-differen

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!