JSF 浮点转换
我在此处的项目中使用 JSF 1.2 和 IceFaces 1.8。
我有一个页面基本上是一大堆浮点数字段的大编辑网格。这是通过页面上的 inputText
字段指向具有原始 float
类型的值对象来实现的。
现在,由于新要求看到某些字段可为空,我想更改值对象使用Float
对象而不是基本类型。我认为我不需要对页面做任何事情来适应这一点。
但是,当我进行更改时,出现以下错误:
/pages/page.xhtml @79,14 value="#{row.targetValue}": java.lang.IllegalArgumentException: 参数类型不匹配
并且
/pages/page.xhtml @79,14 value="#{row.targetValue}": java.lang.IllegalArgumentException: java.lang.ClassCastException@1449aa1
页面如下所示:
<ice:inputText value="#{row.targetValue}" size="4">
<f:convertNumber pattern="###.#" />
</ice:inputText>
我也尝试添加
也在其中,但这似乎也不起作用!将值对象类型更改为 Double 也不起作用。
我确信我可能错过了一些非常简单的东西,但我已经盯着这个有一段时间了,没有立即明显的答案!
I'm using JSF 1.2 with IceFaces 1.8 in a project here.
I have a page which is basically a big edit grid for a whole bunch of floating-point number fields. This is implemented with inputText
fields on the page pointing at a value object with primitive float
types
Now, as a new requirement sees some of the fields be nullable, I wanted to change the value object to use Float
objects rather than primitive types. I didn't think I'd need to do anything to the page to accomodate this.
However, when I make the change I get the following error:
/pages/page.xhtml @79,14 value="#{row.targetValue}": java.lang.IllegalArgumentException: argument type mismatch
And
/pages/page.xhtml @79,14 value="#{row.targetValue}": java.lang.IllegalArgumentException: java.lang.ClassCastException@1449aa1
The page looks like this:
<ice:inputText value="#{row.targetValue}" size="4">
<f:convertNumber pattern="###.#" />
</ice:inputText>
I've also tried adding in <f:convert convertId="javax.faces.Float" />
in there as well but that doesn't seem to work either! Neither does changing the value object types to Double
.
I'm sure I'm probably missing something really simple but I've been staring at this for a while now and no answers are immediately obvious!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
经过一番调查(参见此处,此处以及此处)
是问题所在。它转换成的数字似乎取决于您提供的输入 - 它可以是整数或浮点数。换句话说,它不查看目标类型 - 它只是生成 java.lang.Number 的实例。这不太理想,尽管我无法确定这是否是因为我在某个地方使用旧版本的 JSF 或 EL 或类似的东西。似乎有三种解决方案:
。不幸的是,由于其他原因,#1 不适合我,而且我现在不想编写自己的转换器。但是,如果我更改代码以删除 ConvertNumber,一切似乎都可以正常工作。 (我还将值对象类型更新为 Double,这是我查看的链接之一中建议的)。
这可以防止出现异常,而且看起来 JSF 仍在做我希望它做的事情。只是令人烦恼的是,您似乎无法在同一实例中指定
convertNumber
和转换类型。After some investigation (see e.g. here, here and here) that
<f:convertNumber>
is the problem. It seems that the number it converts to is dependent on the input you give it - it could be an integer or a floating point number. In other words, it doesn't look at the target type - it just generates an instance of java.lang.Number. Which is hardly ideal, although I can't determine whether this is because somewhere I'm using an old version of JSF or EL or something like that.There seem to be three solutions:
java.lang.Number
as your value object type;<f:convertNumber>
.Unfortunately #1 isn't an option for me for other reasons, and I don't want to write my own converter at the moment. However if I change the code to remove the convertNumber, everything seems to work OK. (I've also updated the value object type to
Double
which was suggested in one of the links I looked at).That prevents the exceptions, and it looks like JSF is still doing what I want it to do. Just annoying that it seems you can't specify
convertNumber
and the convert type in the same instance.