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 EmptyToNullStringConverter implements Converter {
public Object getAsObject(FacesContext facesContext, UIComponent component, String submittedValue) {
if (submittedValue == null || submittedValue.isEmpty()) {
if (component instanceof EditableValueHolder) {
((EditableValueHolder) component).setSubmittedValue(null);
}
return null;
}
return submittedValue;
}
public String getAsString(FacesContext facesContext, UIComponent component, Object modelValue) {
return (modelValue == null) ? "" : modelValue.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.EmptyToNullStringConverter</converter-class>
</converter>
In case you're not on Java 6 yet, replace submittedValue.empty()
by submittedValue.length() == 0
.
See also
phoneNumber
? ;) AFAIK this is the default behaviour in JSF 1.x and workaroundable with aConverter
in JSF 1.2 (not in 1.1 or older). I just did little test in JSF 2.0 and it's indeed also the default behaviour and workaroundable with aConverter
. But I was also almost certain that I've read somewhere before that you can configure JSF 2.x to setnull
in case of an empty string by some specialinit-param
. I only don't recall anymore which one it is and where I've read it :( – Herrle