`

h:inputText return a empty string instead of NULL

    博客分类:
  • JSF
阅读更多

You can configure JSF 2.x to interpret empty submitted values as null by the following context-param in web.xml:
<context-param>
    <param-name>javax.faces.INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL</param-name>
    <param-value>true</param-value>
</context-param>

Here is a long explanation. Section 11.1.3 of the JSF spec says:

javax.faces.INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL -- If this param is set, and
calling toLowerCase().equals("true") on a String representation of its value returns true, any
implementation of UIInput.validate() must take the following additional action.
If the javax.faces.INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL context parameter value is
true (ignoring case), and UIInput.getSubmittedValue() returns a zero-length String call
UIInput.setSubmittedValue(null) and continue processing using null as the current submitted value.

This is indeed being done. However, when UIInput.updateModel() is called, that triggers the EL Evaluation, which will coerce the value based on its expected type. If it sees that the expected type is a String then it will convert a null to empty string. So eventually, your managed bean will be set with empty string instead of null. That behavior is governed by the EL spec.

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics