JSF Float Conversion

I am using JSF 1.2 with IceFaces 1.8 in the project here.

I have a page that basically represents a large editing grid for a whole group of floating point numbers. This is implemented using inputText fields on a page pointing to a value object with primitive float types

Now that the new requirement will see that some of the fields are null, I wanted to change the value object to use float objects rather than primitive types. I did not think that I would need to do something on the page to post this.

However, when I make the changes, 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 is as follows:

 <ice:inputText value="#{row.targetValue}" size="4"> <f:convertNumber pattern="###.#" /> </ice:inputText> 

I also tried adding <f:convert convertId="javax.faces.Float" /> , but that does not work either! Also, the types of value objects are not changed to Double .

I'm sure that I probably missed something very simple, but I looked at it for a while, and no answers are immediately obvious!

+6
floating-point exception jsf converter
source share
1 answer

After some investigation (see, for example, here , here and here ) that the problem is <f:convertNumber> . It seems that the number it converts depends on the input you give it - it can be an integer or a floating point number. In other words, it does not look at the target type - it just generates an instance of java.lang.Number. This is hardly ideal, although I can’t determine if this is due to the fact that somewhere I am using the old version of JSF or EL or something like that.

There seem to be three solutions:

  • Use java.lang.Number as the type of a value object;
  • Write your own converter;
  • Do not use <f:convertNumber> .

Unfortunately, No. 1 is not an option for me for other reasons, and I don’t want to write my own converter now. However, if I change the code to remove convertNumber, everything seems to work fine. (I also updated the value object type to Double , which was suggested in one of the links I was looking at).

This prevents exceptions, and it looks like JSF is still doing what I want. It is just annoying that it seems you cannot specify convertNumber and the type of conversion in the same instance.

+4
source share

All Articles