JAXB boolean handling oddities and JSF
There is a known bug in JAXB: http开发者_开发百科s://jaxb.dev.java.net/issues/show_bug.cgi?id=733
JAXB does not properly generate boolean field getters and setters, this bug is left unfixed for backwards compatibility.
A JAXB plugin exists and will ensure that the following getters and setters for boolean fields are generated:
- setXXX(Boolean value) is generated
- getXXX() is generated
- If the boolean attribute specifies default value in the XSD, then getXXX() returns boolean,
- If the boolean attribute does not specify default in the XSD, then getXXX() returns Boolean.
Problem: trying to edit/view the XXX field in a JSF component (such as checkbox) does not work - the component is disabled.
I use Apache Trinidad 1.2. The component is coded as follows:
<tr:selectBooleanCheckbox value="#{MvsDatasetUI.object.mvsDataset.temporary}" id="temporary" converter="javax.faces.Boolean" />
I have not traced this in depth but the assumption (backed by the workaround below) is that JSF EL resolver (or whathaveyou) looks for Boolean getXXX() method and since it does not find it, the component is disabled.
Workaround: If I change the getXXX() method to return Boolean, then everything goes.
Questions:
- What are your ideas on how to address this problem en-masse in the fastest way possible?
- Have I missed some customization for the boolean-getter JAXB plugin?
- Is it possible (does it make sense) to alter JSF resolver (or whathaveyou) so that if Boolean getXXX() is not found, it will fall back to boolean getXXX()?
I would prefer not to manually intervene and change all the generated getXXX() methods to return Boolean instead of boolean.
Problem: trying to edit/view the XXX field in a JSF component (such as checkbox) does not work - the component is disabled.
I've worked with JSF for ages, but I've never seen this particular issue in JSF. It's technically also impossible that a component get disabled based on the type of its value. It would rather have thrown a PropertyNotFoundException
or a ConverterException
whenever the property cannot be found or the value type is unconvertible.
I did a quick test with the <h:selectBooleanCheckbox value="#{bean.checked}" />
and the following getters:
public boolean isChecked()
works fine.public boolean getChecked()
works fine.public Boolean isChecked()
throwsPropertyNotFoundException
during render (correct).public Boolean getChecked()
works fine.
Based on this information, I'd reinvestigate the problem and review the conclusions. Aren't you using some autogenerated or 3rd party JSF code which you thus haven't written yourself? Have you checked the JSF code closely? Isn't it just using this property in some disabled
attribute? The Boolean
has default value of null
while boolean
defaults to false
. Both null
and false
behave differently in EL.
精彩评论