1

web.xml

[...]
<context-param>
    <param-name>javax.faces.INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL</param-name>
    <param-value>false</param-value>
</context-param>
[...]

view.xhtml

[...]
<p:selectOneMenu id="input1" value="#{myBean.value1}">
    <f:selectItem itemValue="" itemLabel="..." />
</p:selectOneMenu>

<p:inputText id="input2" value="#{myBean.value2}" />
[...]

MyBean.java

[...]
private String value1;
private String value2;
[...]

I leave input2 blank, submit the form and inspect the values in the managed bean:

  • value2 is an empty String - as expected!
  • value1 is null

Why is value1 null and not an empty String? If I want the value to be an empty String, how can I achieve this?

Update The "problem" is in the class com.sun.faces.renderkit.html_basic.MenuRenderer:

public Object getConvertedValue(...) {
    [...]
    if (RIConstants.NO_VALUE.equals(newValue)) {
        return null;
    }
    [...]
}
Sebi
  • 2,534
  • 2
  • 26
  • 28

1 Answers1

0

You can check this JSF 2 - Bean Validation: validation failed -> empty values are replaced with last valid values from managed bean.

if you want to set the value1 as empty string then you can use this code.

public Object getSubmittedValue() {
if (submittedValue == null && !isValid() && considerEmptyStringNull(FacesContext.getCurrentInstance())) {
    return "";
}
else {
    return submittedValue;
}

}

Community
  • 1
  • 1
Sathesh S
  • 1,253
  • 3
  • 23
  • 54