Work around for faulty INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL in Mojarra JSF 2.1
Asked Answered
G

1

11

I know there are a number of posts about converting empty string to null in JSF2. The usual prescription is to add the following to web.xml.

<context-param>
  <description>Does not appear to work</description>
  <param-name>javax.faces.INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL</param-name>
  <param-value>true</param-value>
</context-param>

This just does not seem to work - at all. I then created a custom string converter to test if that would work. I explicitly added it as a converter to my inputText (otherwise it does not fire when blank).

When INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL is set to true the converter receives null and the setter for the input text still receives "".

When INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL is set to false (or commented out) the converter receives "" and the setter for the input text receives "" (even after the converter returns null).

@FacesConverter(forClass=java.lang.String.class, value="emptyStringToNull")
public class StringConverter implements Converter, Serializable {
    private static final long serialVersionUID = -1121162636180944948L;
    public Object getAsObject(FacesContext context, UIComponent component, String value) {
        if (value == null || value.trim().isEmpty()) {
            return null;
        }
        return value;
    }

    public String getAsString(FacesContext context, UIComponent component, Object object) {
    if (object == null)
        return null;

    return object.toString();
    }
}

I've event tried (to no avail) to explicitly set the component submitted value in getAsObject:

if (component instanceof EditableValueHolder)
    ((EditableValueHolder) component).setSubmittedValue(null);

I'm using JBoss6 (a snapshot of 6.1 really) and JSF 2.1.1.

Garlandgarlanda answered 10/6, 2011 at 8:47 Comment(0)
J
21

This is not Mojarra specific. This is Tomcat specific (JBoss uses Tomcat as servletcontainer). Add the following VM argument to startup options.

-Dorg.apache.el.parser.COERCE_TO_ZERO=false

To my experience, this one should actually only apply on Number properties (int, long, etc), however since a certain late Tomcat 6.0.x version (at least after 6.0.20) it seems to be broken for strings as well and it is relying on the above VM argument.

On GlassFish 3.x for example it works perfectly fine out the box.

Joanejoanie answered 10/6, 2011 at 12:8 Comment(9)
Well that worked a treat. You know what, I saw that you had previously answered this elsewhere but the post only mentioned Tomcat. Of course I should have realised that this also applies to JBoss AS.Garlandgarlanda
NB : The above does not work if the element is placed within <ui:repeat>, but works if replaced with <c:forEach>Lineman
@Nikhil: I think that you're confusing it with one of lot of bugs of <ui:repeat> in Mojarra. Try with latest MyFaces to see if it behaves different.Joanejoanie
Thank you, BalusC. I used Tomcat 7 and this fix it. Thank you. I have a small question, so in Netbean, I just right click on the Tomcat server and add the JVM option, however, I have to set this option on the real server. Where do I go in tomcat directory to set this JVM option, BalusC?Chrisoula
@Thang: Set JAVA_OPTS environment variable or edit the Tomcat startup script or if it's a Windows service, set it somewhere in that settings panel behind the Apache feather systray icon.Joanejoanie
Using latest glassfish 4.0.1 seems to be broken tooApeman
glassfish 4 has the same problem bugApeman
Can you edit your answer to include the hint that this is broken in glassfish 4.x with EL 3.0 as reported here java.net/jira/browse/JAVASERVERFACES-3071 and here java.net/jira/browse/JAVASERVERFACES_SPEC_PUBLIC-1203 .Treasonable
where should i add -Dorg.apache.el.parser.COERCE_TO_ZERO=false @JoanejoanieProletarian

© 2022 - 2024 — McMap. All rights reserved.