绑定到 Integer 属性的 h:inputText 正在提交值 0 而不是 null
我们在 JSF 页面中使用 h:inputText
,它绑定到 Integer
属性(因此可以接受 null
)。当 h:inputText
中没有写入任何值时,表单将提交 0
而不是 null
。我们使用 Trinidad 1.2.2 和 Tomcat 6.0.20(我们也尝试使用 Tomcat 6.0.14,因为我们了解到某些 Tomcat 版本可能会发生这种情况)。
这是如何引起的以及如何解决?
We are using a h:inputText
in a JSF page which is bound to an Integer
property (and thus can accept null
). When there is no value written in the h:inputText
, the form is submitting a 0
instead of null
. We are using Trinidad 1.2.2 and Tomcat 6.0.20 (we also tried with Tomcat 6.0.14 as we read that this could happen with certains Tomcat versions).
How is this caused and how can I solve it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
此“功能”是 EL 中的错误修复的结果,该错误修复是根据 Tomcat 6.0.16 引入的。根据 EL 规范,数字类型的值
null
应该被强制为 0。在 Tomcat 6.0.16 之前,这是“错误地”被强制为空字符串。毕竟,空字符串实际上比零更直观。 Apache 的 Tomcat 人员对此 bug 修复收到了很多抱怨,因此他们根据 Tomcat 6.0.17 引入了一个新的配置设置,类似于 VM 参数,这将禁用此 bug 修复。
顺便说一句,我请求对此规范进行更改,它应该只将空/空原始类型强制为零和空/空非原始
java.lang. lang.Number
类型为空字符串:JSP EL 问题 184。他们没有做太多事情,但这个问题至少得到了很多选票。谁知道...这种行为至少确实非常烦人,因为它不直观。另请参阅:
This "feature" was result of a bugfix in EL which was introduced as per Tomcat 6.0.16. As per chapter 1.18.3 of the EL specification, a value of number type which is
null
should be coerced to 0. Before Tomcat 6.0.16 this was "incorrectly" been coerced to an empty string.After all, an empty string was actually much more intuitive than a zero. The Tomcat guys at Apache got a lot complaints about this bugfix, so they introduced a new configuration setting in flavor of a VM argument as per Tomcat 6.0.17 which would disable this bugfix.
I've by the way requested a change to this spec that it should only coerce empty/null primitive types to zero and empty/null non-primitive
java.lang.Number
types to empty string: JSP EL issue 184. They did not much with it, but the issue at least got a lot of votes. Who knows... This behaviour is at least indeed pretty annoying because it's non-intuitive.See also: