Search code examples
javastruts2floating-pointognl

How do I set a float without using an OGNL converter?


I have a class that looks like this:

public class Foobar {
   private float value;
   public void setValue(float value) {
      this.value = value;
   }
}

I then have a webpage (Struts2) that passed in a variable foobar.value.

<input type="text" name="foobar.value" value="123.456">

I then get this error:

ognl.MethodFailedException: Method "setValue" failed for object Foobar@19d373d [java.lang.NoSuchMethodException: setValue([Ljava.lang.String;)]
    at ognl.OgnlRuntime.callAppropriateMethod(OgnlRuntime.java:823)

I found this website that talks about creating converters http://www.opensymphony.com/ognl/html/DeveloperGuide/typeConversion.html

Doesn't OGNL and Struts2 have support for setting a primitive float by default?


Solution

  • OK, I figured it out. The above code DOES work. But this does NOT work.

     <input type="text" name="foobar.value" value="-123.456">
    

    Apparently OGNL interprets the negative number as a String. I'm not sure how to deal with this without a converter. It's nice to know that it will handle positive float and Float natively.

    I did some further research on this about found this bug. http://issues.apache.org/struts/browse/WW-2971

    It's closed but the version 2.1.8 isn't in Maven and the latest version of Struts2 doesn't rely on it yet. :(

    I think I'll be writing a converter for now.