I have the following code:
xwork-conversion.properties
java.math.BigDecimal=demo.BigDecimalConverter
BigDecimalConverter.java
package demo;
import java.math.BigDecimal;
import java.util.Map;
import org.apache.struts2.util.StrutsTypeConverter;
public class BigDecimalConverter extends StrutsTypeConverter{
@Override
public Object convertFromString(Map context, String[] values, Class clazz) {
System.out.println("BigDecimal : Converting from String : " + values[0]);
return new BigDecimal(values[0]);
}
@Override
public String convertToString(Map context, Object value) {
String str = ((BigDecimal)value).toPlainString();
System.out.println("BigDecimal : Converted to String : " + str);
return str;
}
}
TheAction.java
package demo;
//imports...
public class TheAction extends ActionSupport {
private BigDecimal bigField; //with getter and setter
public String execute() {
return SUCCESS;
}
}
struts.xml
<package name="demo" extends="json-default">
<action name="processBig" class="demo.TheAction">
<result type="json"/>
</action>
</package>
Observation
When request is submitted with some big decimal say "12345678901234567890.123456789123456789"
, method convertFromString
is executed and value is converted to string and prints
BigDecimal : Converting from String : 12345678901234567890.123456789123456789
But when response is parsed, method convertToString
is not executed as it does not log expected line on standard output. Struts2 internally converts BigDecimal
to String
and following response is returned.
{"bigField":12345678901234567890.123456789123456789}
When response is received in JavaScript, it becomes 12345678901234567000
, a big loss of value.
Questions:
BigDecimalConverter.convertToString
is not being called?String
field and/or String
getter)?Short answer:
BigDecimalConverter.convertToString
is not being called because it isn't used.
Is there any other way to achieve this (without defining corresponding String field and/or String getter)?
No, you can't. JavaScript is a not your language choice or you can use it as string. Means that a limitation of the language you can overcome with something like bigdecimal.js
.