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 a request is sent with some large decimal "12345678901234567890.123456789123456789" , the convertFromString method is convertFromString and the value is converted to a string and prints
BigDecimal : Converting from String : 12345678901234567890.123456789123456789
But when analyzing the response, the convertToString method convertToString not executed, since it does not register the expected string on standard output. Struts2 internally converts BigDecimal to String and returns a response.
{"bigField":12345678901234567890.123456789123456789}
When the response is received in JavaScript, it becomes 12345678901234567000 , a big loss of value.
Questions:
- Why isn't
BigDecimalConverter.convertToString called? - Is there any other way to achieve this (without defining the appropriate
String and / or String getter field)?
source share