1

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>
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Chatoyancy
  • 143
  • 1
  • 17

1 Answers1

1

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" />
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • While I do agree, it is unfortunately not something I can change easily in this project -- the code example I provided is a simplification. The Character attending actually isn't in the main Pagecode class, but within another object that is a property of the subclass, and unfortunately, that class is part of a shared library used by several other projects. I'd like to change it, but for the moment, I can't. Even for just curiosity's sake, would there be a way to make the h:selectOneRadio send null instead of the null character? Seems like an odd implementation. – Chatoyancy Jan 17 '16 at 03:57
  • 1
    Blame EL, not JSF. You could try disabling COERCE_TO_ZERO. Related: http://balusc.omnifaces.org/2015/10/the-empty-string-madness.html – BalusC Jan 17 '16 at 09:00
  • I just tested this with the COERCE_TO_ZERO argument and can verify that it fixed it. The behavior is related to that issue. Thanks! – Chatoyancy Mar 24 '16 at 15:36