JSF 2.2 interpret empty string submitted values as null not working
Asked Answered
G

6

8

I have migrated from Java EE 6 to Java EE 7, and now with JSF 2.2 the context param INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL seems not work. In JSF 2.1 I set it to "true" and it works perfectly, but now I get always blank strings.

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

Can anyone say something about it?

Gastritis answered 24/10, 2013 at 19:51 Comment(2)
Might be a bug in the implementation. Can you debug the environment to confirm if it is a bug in the mojarra sources?Culminate
possible duplicate of javax.faces.INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL does not work anymore since Java EE 7 / EL 3.0Cohen
C
7

The same happens with glassfish 4 with the latest Mojarra-2.2.5 as well as Wildfly 8 Final . . . I have seen multiple bug reports on this, Manfried Riem says "It was determined this is an EL issue and the EL implementation has been fixed to fix this", but not sure if that means Updating Mojarra fixes it, because it does not in glassfish 4. I also updated the el, and that did not work either.

Cammack answered 19/2, 2014 at 11:47 Comment(0)
T
3

Unfortunately there seems to be a bug in Glassfish 4.

See:

INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL doesn't work at all

and:

Empty String as Null is not effective in 2.2.0

Thedrick answered 3/12, 2013 at 6:47 Comment(0)
A
3

Apache el implementation do the empty string (or 0 int value). You can find it in org.apache.el.parser.AstValue class:

public void setValue(EvaluationContext ctx, Object value)
        throws ELException {
    Target t = getTarget(ctx);
    ctx.setPropertyResolved(false);
    ELResolver resolver = ctx.getELResolver();

    // coerce to the expected type
    Class<?> targetClass = resolver.getType(ctx, t.base, t.property);
    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);
    }
    if (!ctx.isPropertyResolved()) {
        throw new PropertyNotFoundException(MessageFactory.get(
                "error.resolver.unhandled", t.base, t.property));            
    }
}

You can set COERCE_TO_ZERO to false (-Dorg.apache.el.parser.COERCE_TO_ZERO=false).

Or use other el impl:

    <dependency>
        <groupId>org.glassfish.web</groupId>
        <artifactId>el-impl</artifactId>
        <version>2.2</version>      
    </dependency>

And set context-param:

    servletContext.setInitParameter("com.sun.faces.expressionFactory", "com.sun.el.ExpressionFactoryImpl");

That was el-api side.

Other side is JSF. You have to set this context-param for JSF:

servletContext.setInitParameter("javax.faces.INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL", "true");

Sorry for my english!

Alisealisen answered 27/3, 2015 at 14:32 Comment(0)
E
0

Things are even more confusing now. I am using JSF 2.2.8-SNAPSHOT and although the value is interpreted as null during JSF bean validation, the actual value set is an empty String. This means that other components doing validation, e.g. JPA fail length validation as String value is "".

As this apparently requires a spec change, I wouldn't expect this any time soon. The jira also contains a workaround which is described in this post.

Ps. This could be the right behavior as the documentation states that null is passed to the bean validation framework, but this is not intuitive at all.

Eyepiece answered 6/7, 2014 at 9:9 Comment(0)
Z
0

There's a JVM property for the application server that helped me in a similar case. Please see Work around for faulty INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL in Mojarra JSF 2.1

Zohara answered 23/2, 2015 at 8:29 Comment(0)
M
-1

I have tried below and got worked. we need to add this entry at web.xml

<context-param>
    <param-name>javax.faces.VALIDATE_EMPTY_FIELDS</param-name>
    <param-value>true</param-value>
</context-param>
Mas answered 30/10, 2015 at 7:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.