如何在不使用 OGNL 转换器的情况下设置浮点数?
我有一个如下所示的类:
public class Foobar {
private float value;
public void setValue(float value) {
this.value = value;
}
}
然后我有一个传入变量 foobar.value 的网页(Struts2)。
<input type="text" name="foobar.value" value="123.456">
然后我收到此错误:
ognl.MethodFailedException: Method "setValue" failed for object Foobar@19d373d [java.lang.NoSuchMethodException: setValue([Ljava.lang.String;)]
at ognl.OgnlRuntime.callAppropriateMethod(OgnlRuntime.java:823)
我发现这个网站谈论创建转换器 http:// /www.opensymphony.com/ognl/html/DeveloperGuide/typeConversion.html
OGNL 和 Struts2 默认情况下不支持设置原始浮点数吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果将值更改为 Float(对象),这有效吗?
If you change value to Float (the Object), does that work?
好吧,我明白了。上面的代码确实有效。但这不起作用。
显然 OGNL 将负数解释为字符串。我不知道如何在没有转换器的情况下处理这个问题。很高兴知道它将原生处理正浮动和浮动。
我对此做了一些进一步的研究,发现了这个错误。 http://issues.apache.org/struts/browse/WW-2971
它已经关闭,但 2.1.8 版本不在 Maven 中,并且最新版本的 Struts2 还不依赖它。 :(
我想我现在会写一个转换器。
OK, I figured it out. The above code DOES work. But this does NOT work.
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.