我有我的struts.xml:
<s:textfield id="thresholdParameter_1"
name="gmathreshold.distinctBnumberRatio">
</s:textfield></td>
其中 gmathreshold 是一个以 distinctBnumberRatio
作为成员变量的 bean。在我的 bean 中,它是一个 BigDecimal。那么我怎样才能把它设置在我的 Bean.我的意思是如何在struts2中从String
转换为BigDecimal
??
最佳答案
how to convert from String to
BigDecimal
BigDecimal(java.lang.String)构造函数采用 String
参数。
Translates the string representation of a
BigDecimal
into aBigDecimal
. The string representation consists of an optional sign, '+' ( '\u002B') or '-' ('\u002D'), followed by a sequence of zero or more decimal digits ("the integer"), optionally followed by a fraction, optionally followed by an exponent.
例如,
String distinctBnumberRatioStr = "124.20";
BigDecimal distinctBnumberRatio = new BigDecimal(str);
更新
回答你comment
String in JSP to BigDecimal in ActionClass
From docs TypeConversion-BuiltinTypeConversionSupport
类型转换由XWork实现。
XWork 将自动为您处理最常见的类型转换。这包括支持以下各项与字符串之间的转换:
- 字符串
- boolean 值/boolean 值
- 字符/字符
- int/整数、float/Float、long/Long、double/Double
- 日期 - 使用与当前请求关联的区域设置的 SHORT 格式
- 数组 - 假设各个字符串可以转换为各个项目
- 集合 - 如果无法确定对象类型,则假定为 String 并创建一个新的 ArrayList
- 枚举
- BigDecimal 和 BigInteger
请注意,对于数组,类型转换将遵循数组元素的类型,并尝试单独转换每个项目。与任何其他类型转换一样,如果无法执行转换,则使用标准类型转换错误报告来指示处理类型转换时发生的问题。
关于java - struts2的类型转换?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20896322/