java - struts2的类型转换?

标签 java javascript jsp jakarta-ee struts2

我有我的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 a BigDecimal. 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/

相关文章:

javascript - 如何在 JSX React 中循环对象

jsp - JSTL forEach 在 JSP 中不起作用

java - 如何从 Scala/Java 执行 OCaml 代码?

java - 无法解决或者不是eclipse中android中的字段错误

javascript - Service Worker 缓存比 http 缓存更快吗?

java - 使用单个 servlet 调用多个方法

java - 创建包含 .ser 文件的以随机数字作为文件夹名称的目录

java - 对象类型和引用类型之间的区别

java - 如何找到最接近另一个数字的数字,即 2 的幂?

javascript - 我想从使用 onkeypress 事件的文本区域获取一个属性