jsr344-experts@javaserverfaces-spec-public.java.net

[jsr344-experts] javax.faces.INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL side effects

From: Leonardo Uribe <lu4242_at_gmail.com>
Date: Tue, 26 Jul 2011 16:04:47 -0500

Hi

Checking how javax.faces.INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL
web config param, introduced in JSF 2.0 / JSF 2.1, I notice some side
effects. These problems where reported on:

https://issues.apache.org/jira/browse/MYFACES-3008
https://issues.apache.org/jira/browse/MYFACES-3181

The first problem is about what happens when you have a h:inputText or
some similar component, you attach a validator that prevent not null
values (like with extval @NotNull) and
javax.faces.INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL is set to
true. If the input component has already a value and it gets removed
by the user, a validation error occurs as expected but the old value
of the model gets displayed instead of the wrong value (which is the
empty string).

Obviously in this case, there was a submitted value, but
EditableValueHolder interface does not allow to check when a submitted
value was set. It exists isLocalValueSet() but there is no
isSubmittedValueSet(). The only way to fix this is to extend
EditableValueHolder interface at spec level.

The second problem is how
javax.faces.INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL affects
components like h:selectOneMenu. Consider this example:

web.xml

  <context-param>
    <param-name>javax.faces.INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL</param-name>
    <param-value>false</param-value>
  </context-param>

nullSelect.xhtml

<h:form id="mainForm" prependId="false">
    <h:panelGrid columns="2">
        <h:outputLabel for="level" value="Name: " />
       <h:selectOneMenu id="level" value="#{testNullSelectBean.level}"
label="Level">
         <f:selectItem itemValue="#{null}" itemLabel="Unknown"/>
         <f:selectItem itemValue="HIGH" itemLabel="High"/>
         <f:selectItem itemValue="MEDIUM" itemLabel="Medium"/>
         <f:selectItem itemValue="LOW" itemLabel="Level"/>
       </h:selectOneMenu>
        <h:commandButton id="button" value="Submit"
action="#{testNullSelectBean.action}"/>
        <h:messages showDetail="true" showSummary="false"/>
    </h:panelGrid>
</h:form>

The following html is rendered for the menu:

<select id="level" name="level" size="1">
        <option value="" selected="selected">Unknown</option>
        <option value="HIGH">High</option>
        <option value="MEDIUM">Medium</option>
        <option value="LOW">Level</option>
</select>

Some users get surprised that
javax.faces.INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL does not
applies on this case, but it was defined at UIInput level, so all
descendants of UIInput should be affected. I checked MyFaces and
Mojarra and both do the same in this case: convert empty string to
null no matter if the web config param is set to false. I know the
param was added for textbox components, so I'm not sure about what to
do in this case.

regards,

Leonardo Uribe