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.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…