views:

1029

answers:

2

Hello, i make a JSF 2.0 Application and i use many h:inputText fields to input data in my database. Some fields are not required

<h:inputText value="#{registerBean.user.phoneNumber}" id="phoneNumber" >
                            <f:validateLength maximum="20" />
                        </h:inputText><br/>

When the user leave this field empty JSF returns "" instead of NULL.

How can i fix this behavior without check every String with

if(string.equals(""){ string = null}

Thanks.

+3  A: 

You can configure JSF 2.x to interpret empty submitted values as null by the following context-param in web.xml (which has a pretty long name, that'll also be why I couldn't recall it ;) ):

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



For reference and for ones who are interested, in JSF 1.2 (and thus not 1.1 or older because it's by design not possible to have a Converter for java.lang.String) this is workaroundable with the following Converter:

public class EmptyToNullConverter implements Converter {

    public Object getAsObject(FacesContext facesContext, UIComponent component, String value) {
        if (value == null || value.trim().length() == 0) {
            if (component instanceof EditableValueHolder) {
                ((EditableValueHolder) component).setSubmittedValue(null);
            }
            return null;
        }
        return value;
    }

    public String getAsString(FacesContext facesContext, UIComponent component, Object value) {
        return (value == null) ? null : value.toString();
    }

}

...which needs to be registered in faces-config.xml as follows:

<converter>
    <converter-for-class>java.lang.String</converter-for-class>
    <converter-class>com.example.EmptyToNullConverter</converter-class>
</converter>

For JDK6 purists, the value.trim().length() == 0 can also be replaced by value.trim().isEmpty() ;)

BalusC
Thank you very much
ThreeFingerMark
You're welcome.
BalusC
A: 

Hello, i hope this is the righ way to say that i can't find a solution for the Problem.

I have add the context-param to my web.xml but it has no result. I use a Tomcat 6.0.24 Server with this two context-param: javax.faces.PROJECT_STAGE = Development javax.faces.VALIDATE_EMPTY_FIELDS = true

ThreeFingerMark
In the future, just edit your question :) As to the problem: what if you remove one or both of the other parameters? I have tested it at Glassfish v3 by the way. I'll test at Tomcat later this day when I have chance.
BalusC
Yes, i have remove the other parameters but without a result.I wait for your test. Thanks
ThreeFingerMark
I was able to reproduce the same problem. The cause seems to be inside Tomcat's EL implementation. We can't do anything against it than hacking in it. Until they get it officially fixed and released, I would suggest to use the `Converter` as shown in my answer. It works fine.
BalusC
Thank you for your Help. I use now Glasfish and it works like a charm
ThreeFingerMark