简体   繁体   中英

JSF Float Conversion

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!

After some investigation (see eg 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM