JAXB 布尔处理异常和 JSF
JAXB 中存在一个已知错误:https://jaxb.dev。 java.net/issues/show_bug.cgi?id=733
JAXB 无法正确生成布尔字段 getter 和 setter,此错误未修复以实现向后兼容性。
存在 JAXB 插件,它将确保生成以下布尔字段的 getter 和 setter:
- 生成 setXXX(Boolean value)
- getXXX()
- 如果布尔属性在 XSD 中指定默认值,则 getXXX() 返回布尔值,
- 如果布尔属性未在 XSD 中指定默认值,则 getXXX() 返回 Boolean。
问题:尝试编辑/查看 JSF 组件(例如复选框)中的 XXX 字段不起作用 - 该组件被禁用。
我使用 Apache Trinidad 1.2。该组件的编码如下:
<tr:selectBooleanCheckbox value="#{MvsDatasetUI.object.mvsDataset.temporary}" id="temporary" converter="javax.faces.Boolean" />
我没有深入追踪这一点,但假设(由下面的解决方法支持)是 JSF EL 解析器(或 Whathaveyou)查找 Boolean getXXX() 方法,因为它没有找到它,该组件被禁用。
解决方法:如果我将 getXXX() 方法更改为返回布尔值,则一切正常。
问题:
- 对于如何以最快的方式集体解决这个问题,您有什么想法?
- 我是否错过了 boolean-getter JAXB 插件的一些自定义?
- 是否有可能(是否有意义)更改 JSF 解析器(或whathaveyou),以便如果未找到 Boolean getXXX(),它将回退到 boolean getXXX()?
我不想手动干预并更改所有生成的 getXXX() 方法以返回布尔值而不是布尔值。
There is a known bug in JAXB: https://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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
I我已经使用 JSF 多年了,但我从未在 JSF 中看到过这个特殊问题。从技术上讲,组件也不可能根据其值的类型而被禁用。当找不到属性或值类型不可转换时,它宁愿抛出
PropertyNotFoundException
或ConverterException
。我使用
和以下 getter 进行了快速测试:public boolean isChecked()
有效美好的。public boolean getChecked()
工作正常。public Boolean isChecked()
在渲染期间抛出PropertyNotFoundException
(正确)。public Boolean getChecked()
工作正常。根据这些信息,我将重新调查问题并审查结论。您是否使用了一些不是您自己编写的自动生成的或第 3 方 JSF 代码?您仔细检查过 JSF 代码吗?不就是在某些
disabled
属性中使用这个属性吗?Boolean
的默认值为null
,而boolean
默认为false
。null
和false
在 EL 中的行为不同。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 aConverterException
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? TheBoolean
has default value ofnull
whileboolean
defaults tofalse
. Bothnull
andfalse
behave differently in EL.