JSF 浮点转换

发布于 2024-08-21 06:48:22 字数 909 浏览 3 评论 0原文

我在此处的项目中使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

窝囊感情。 2024-08-28 06:48:22

经过一番调查(参见此处此处以及此处 是问题所在。它转换成的数字似乎取决于您提供的输入 - 它可以是整数或浮点数。换句话说,它不查看目标类型 - 它只是生成 java.lang.Number 的实例。这不太理想,尽管我无法确定这是否是因为我在某个地方使用旧版本的 JSF 或 EL 或类似的东西。

似乎有三种解决方案:

  1. 使用 java.lang.Number 作为值对象类型;
  2. 编写自己的转换器;
  3. 不要使用

不幸的是,由于其他原因,#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:

  1. Use java.lang.Number as your value object type;
  2. Write your own converter;
  3. Don't use <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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文