Hi,

 
When binding a JSF input text to a String property of a JSF ManagedBean, I
want that an empty value is mapped to null and not "" (empty string).

So, on tomcat7 (7.0.56) with JSF(2.2) I can get this to work nicely by
controlling the coercing behavior with the JVM property
org.apache.el.parser.COERCE_TO_ZERO icw JSF
javax.faces.INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL as context
param.

 

When javax.faces.INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL is set to
true, JSF will set the local component value for the input component as null
when the input if left empty.

Next, when the value is actually set by tomcat's EL parser, it will NOT try
to coerce the value when org.apache.el.parser.COERCE_TO_ZERO is set to
false.

This can be seen in org.apache.el.parser.AstValue#216:

 

  if (COERCE_TO_ZERO == true

                || !isAssignable(value, targetClass)) {

            resolver.setValue(ctx, t.base, t.property,

                    ELSupport.coerceToType(value, targetClass));

        } else {

            resolver.setValue(ctx, t.base, t.property, value);

        }

 

So, in this case I get 'null' instead of "" => OK

 

However, if I now try this on tomcat8 (8.0.14), this no longer works.
org.apache.el.parser.AstValue#200 now does this:

 

resolver.setValue(ctx, t.base, t.property,

                ELSupport.coerceToType(value, targetClass));

 

So ELSupport is ALWAYS executed, it will ignore the first 2 IF statements
(as my target type is String):

 

  public static final Object coerceToType(final Object obj,

            final Class<?> type) throws ELException {

 

        if (type == null || Object.class.equals(type) ||

                (obj != null && type.isAssignableFrom(obj.getClass()))) {

            return obj;

        }

 

        if (!COERCE_TO_ZERO) {

            if (obj == null && !type.isPrimitive() &&

                    !String.class.isAssignableFrom(type)) {

                return null;

            }

        }

 

        if (String.class.equals(type)) {

            return coerceToString(obj);

        }

 

And this will eventually call "coerceToString", which will always return ""
if the value was null :

 

public static final String coerceToString(final Object obj) {

        if (obj == null) {

            return "";

 

So, from tomcat8 I always get "" instead of the desired null.

I know this is related to the specification (this goes as far back as
tomcat6) but now I'm getting really confused here as this worked on 6 and 7
but no longer on 8

Is this a bug or am I missing something here?

 

Thanks,

 

Koen.

Reply via email to