jsfjsf-2selectoneradionull-checknull-character

h:selectOneRadio is passing null character instead of null value


In my JSF pagecode, I have something similar to the following:

<h:selectOneRadio value="#{pagecode.attending}" required="true" id="attendingRadio" binding="#{attendingRadio}">
    <f:selectItem itemValue="Y" itemLabel="Yes"/>
    <f:selectItem itemValue="N" itemLabel="No"/>
    <f:ajax event="click" render="attendingDetails" execute="attendingRadio"/>
</h:selectOneRadio>
<h:panelGroup id="attendingDetails">
    <h:panelGroup rendered="#{pagecode.showForm(attendingRadio.value)}">
        ...
    </h:panelGroup>
</h:panelGroup>

In my pagecode backing bean, I have something like the following:

public class Pagecode {
    protected Character attending;

    public Character getAttending() {
        return attending;
    }

    public void setAttending(Character attending) {    
        this.attending=attending;
    }

    public boolean showForm(Character c) {
        if (c==null) {
            return false;
        }
        (other stuff)...
    }

}

The behavior I expect is:

  1. The page loads. The radio button is not defined to an initial value, so neither Yes nor No is selected. However, since required="true", the user will get a validation error if they leave it blank (and they do).
  2. The first time the page loads, #{pagecode.showForm(attendingRadio.value)} is called, I would expect that a null object would be passed, and showForm would return false.

After doing some troubleshooting, I determined that what is actually happening is that instead of null object being passed, i.e. equivalently showForm(null), I am instead getting the equivalent of showForm(new Character('\u0000')); it passing the Unicode character for null instead.

Is there a way to make JSF pass the Java null object instead of a null character when my h:selectOneRadio does not have a value selected? I am using Apache MyFaces JSF 2.0 on WebSphere Portal 8.0.

As a side note, I did try to add the following to my web.xml, and it unfortunately did not help:

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

Solution

  • Character/char is simply a bad data type for the value you want to represent.

    Use an enum instead.

    public enum Choice {
        Y, N;
    }
    

    private Choice attending;
    

    Or, likely better, a Boolean.

    private Boolean attending;
    

    <f:selectItem itemValue="true" itemLabel="Yes" />
    <f:selectItem itemValue="false" itemLabel="No" />