Iusse in BigDecimal conversion after upgrading from 2.5.10.1 to 2.5.13

classic Classic list List threaded Threaded
18 messages Options
Reply | Threaded
Open this post in threaded view
|

Iusse in BigDecimal conversion after upgrading from 2.5.10.1 to 2.5.13

sreekanth
Hi,
    We have a List object which is getting set at JSP (element added
dynamically in front end), while saving this form the object's value for
BigDecimal fields are getting null.

eg:

Class A {
String strVal;
BigDecimal drAmt;
BigDecimal crAmt;
}

in jsp
<input type="hidden" name='myList[0].strVal'/>
<input type="hidden" name='myList[0].drAmt'/>
<input type="hidden" name='myList[0].crAmt'/>


while we submit the form *strVal* is getting binded properly but both *drAmt
and crAmt* not getting set.

When i revert the upgrade back to 2.5.10.1 then it started working.



--
Sent from: http://struts.1045723.n5.nabble.com/Struts-User-f3426046.html

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: Iusse in BigDecimal conversion after upgrading from 2.5.10.1 to 2.5.13

Lukasz Lenart
2017-10-03 18:56 GMT+02:00 sreekanth <[hidden email]>:

> Hi,
>     We have a List object which is getting set at JSP (element added
> dynamically in front end), while saving this form the object's value for
> BigDecimal fields are getting null.
>
> eg:
>
> Class A {
> String strVal;
> BigDecimal drAmt;
> BigDecimal crAmt;
> }
>
> in jsp
> <input type="hidden" name='myList[0].strVal'/>
> <input type="hidden" name='myList[0].drAmt'/>
> <input type="hidden" name='myList[0].crAmt'/>
>
>
> while we submit the form *strVal* is getting binded properly but both *drAmt
> and crAmt* not getting set.

As from Struts version 2.3.12, the built-in converter is Locale aware
which means it will convert String to BigDecimal according to user
locale.


Regards
--
Łukasz
+ 48 606 323 122 http://www.lenart.org.pl/

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: Iusse in BigDecimal conversion after upgrading from 2.5.10.1 to 2.5.13

sreekanth
Thanks Lukasz, can you please point me some wiki page or what change i have
to do to make my existing code working ?



--
Sent from: http://struts.1045723.n5.nabble.com/Struts-User-f3426046.html

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: Iusse in BigDecimal conversion after upgrading from 2.5.10.1 to 2.5.13

Yasser Zamani
In reply to this post by sreekanth


On 10/3/2017 8:26 PM, sreekanth wrote:

> Hi,
>      We have a List object which is getting set at JSP (element added
> dynamically in front end), while saving this form the object's value for
> BigDecimal fields are getting null.
>
> eg:
>
> Class A {
> String strVal;
> BigDecimal drAmt;
> BigDecimal crAmt;
> }
>
> in jsp
> <input type="hidden" name='myList[0].strVal'/>
> <input type="hidden" name='myList[0].drAmt'/>
> <input type="hidden" name='myList[0].crAmt'/>
>
>
> while we submit the form *strVal* is getting binded properly but both *drAmt
> and crAmt* not getting set.

Do they not being setted at all or they being setted but with a rounded
value? If not setted at all, I recommend setting devmode to true and
configuring log4j2 level to WARN and then examining the output for any
related WARN. If you have problem with rounded values posted back, then
I think you can `setMaximumFractionDigits` to a larger number in current
locale.

>
> When i revert the upgrade back to 2.5.10.1 then it started working.
>
>
>
> --
> Sent from: http://struts.1045723.n5.nabble.com/Struts-User-f3426046.html
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [hidden email]
> For additional commands, e-mail: [hidden email]
>

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]
Reply | Threaded
Open this post in threaded view
|

Re: Iusse in BigDecimal conversion after upgrading from 2.5.10.1 to 2.5.13

sreekanth
The value not getting set at all, its coming as null. By the time i will also
see if i can provide some log as you mentioned.



--
Sent from: http://struts.1045723.n5.nabble.com/Struts-User-f3426046.html

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: Iusse in BigDecimal conversion after upgrading from 2.5.10.1 to 2.5.13

Lukasz Lenart
In reply to this post by sreekanth
2017-10-03 20:03 GMT+02:00 sreekanth <[hidden email]>:
> Thanks Lukasz, can you please point me some wiki page or what change i have
> to do to make my existing code working ?

Hm... as far I recall there is no special settings, just use format
according to user locale, eg.: US locale 1.000,12, DE locale 1 000,12


Regards
--
Łukasz
+ 48 606 323 122 http://www.lenart.org.pl/

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: Iusse in BigDecimal conversion after upgrading from 2.5.10.1 to 2.5.13

Yasser Zamani
In reply to this post by sreekanth


On 10/3/2017 9:56 PM, sreekanth wrote:
> The value not getting set at all, its coming as null. By the time i will also
> see if i can provide some log as you mentioned.
>

I tested similar action with a list of A where A has two BigDecimals;
posted values (myList[x].y] getting set here however they're rounded but
are setted. please use following resources to find the cause there:

[1] https://struts.apache.org/docs/devmode.html
[2]
https://struts.apache.org/getting-started/how-to-create-a-struts2-web-application.html 
(Step 4 - Add Logging; set levels to WARN)

>
>
> --
> Sent from: http://struts.1045723.n5.nabble.com/Struts-User-f3426046.html
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [hidden email]
> For additional commands, e-mail: [hidden email]
>

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]
Reply | Threaded
Open this post in threaded view
|

Re: Iusse in BigDecimal conversion after upgrading from 2.5.10.1 to 2.5.13

sreekanth
In reply to this post by Lukasz Lenart
Hi Lukasz,
             what bothering me is, we haven't changed this particular jsp
since an year or more and its working fine till  2.5.10.x. I have the
following doubt, if it can be answered then i can assume whats going wrong
in my code.

What was the behaviour of struts 2.5.10.1 while converting an empty value ?
was it converting to null or was it converting to BigDecimal ZERO ?





--
Sent from: http://struts.1045723.n5.nabble.com/Struts-User-f3426046.html

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: Iusse in BigDecimal conversion after upgrading from 2.5.10.1 to 2.5.13

sreekanth
In reply to this post by Yasser Zamani
Thanks Yasser, if my previous doubts get cleared then probably i can assume
there is behavioral change in binding empty value. If thats not the case
then will try to replicate as you have mentioned.



--
Sent from: http://struts.1045723.n5.nabble.com/Struts-User-f3426046.html

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: Iusse in BigDecimal conversion after upgrading from 2.5.10.1 to 2.5.13

Yasser Zamani


On 10/3/2017 10:33 PM, sreekanth wrote:
> Thanks Yasser, if my previous doubts get cleared then probably i can assume
> there is behavioral change in binding empty value. If thats not the case
> then will try to replicate as you have mentioned.
>

In my yesterday test application, empty string value converts to null.
The locale is en_US.

>
>
> --
> Sent from: http://struts.1045723.n5.nabble.com/Struts-User-f3426046.html
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [hidden email]
> For additional commands, e-mail: [hidden email]
>

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]
Reply | Threaded
Open this post in threaded view
|

Re: Iusse in BigDecimal conversion after upgrading from 2.5.10.1 to 2.5.13

sreekanth
thanks Yasser, may i know with which version of struts you have tested it
2.5.10.1 or 2.5.13 ?



--
Sent from: http://struts.1045723.n5.nabble.com/Struts-User-f3426046.html

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: Iusse in BigDecimal conversion after upgrading from 2.5.10.1 to 2.5.13

Yasser Zamani


On 10/4/2017 8:07 AM, sreekanth wrote:
> thanks Yasser, may i know with which version of struts you have tested it
> 2.5.10.1 or 2.5.13 ?
>

2.5.13. But now I also tested 2.5.10.1 and get following warn when try
to set empty string value to a BigDecimal:

Unexpected Exception caught setting 'myList[0].crAmt' on 'class
me.zamani.yasser.ww_convention.actions.MyBigDecimalList: Error setting
expression 'myList[0].crAmt' with value ['', ]

Also, 2.5.10.1 does not round my values during creating the s:hidden tag.

>
>
> --
> Sent from: http://struts.1045723.n5.nabble.com/Struts-User-f3426046.html
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [hidden email]
> For additional commands, e-mail: [hidden email]
>

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]
Reply | Threaded
Open this post in threaded view
|

Re: Iusse in BigDecimal conversion after upgrading from 2.5.10.1 to 2.5.13

sreekanth
thanks Yasser, that info was great, so do you think so the way struts2 binds
BigDecimal changed between these two versions ? and also if possible kindly
share your sample project in github so that i can fork it and check myself
faster.



--
Sent from: http://struts.1045723.n5.nabble.com/Struts-User-f3426046.html

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: Iusse in BigDecimal conversion after upgrading from 2.5.10.1 to 2.5.13

Yasser Zamani


On 10/4/2017 9:19 AM, sreekanth wrote:
> thanks Yasser, that info was great, so do you think so the way struts2 binds
> BigDecimal changed between these two versions ?

I found the difference at [1]. That is in order to fix WW-4581 [2].

[1]
https://gitbox.apache.org/repos/asf?p=struts.git;a=commitdiff;h=f874f9cde56f74c5161b17e645f779805c51a04b
[2] https://issues.apache.org/jira/browse/WW-4581

>  and also if possible kindly
> share your sample project in github so that i can fork it and check myself
> faster.
>

My sample is inside a big project like a big Struts lab ;) but the
config is simple as below:

```java
package me.zamani.yasser.ww_convention.actions;

import java.math.BigDecimal;

/**
  * Created by user on 10/3/2017.
  */
public class A {
         public String getStrVal() {
             return strVal;
         }

         public void setStrVal(String strVal) {
             this.strVal = strVal;
         }

         public BigDecimal getDrAmt() {
             return drAmt;
         }

         public void setDrAmt(BigDecimal drAmt) {
             this.drAmt = drAmt;
         }

         public BigDecimal getCrAmt() {
             return crAmt;
         }

         public void setCrAmt(BigDecimal crAmt) {
             this.crAmt = crAmt;
         }

         String strVal;
         BigDecimal drAmt;
         BigDecimal crAmt;
     }
```

```java
package me.zamani.yasser.ww_convention.actions;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;

import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.List;

@Results({
         @Result(name = Action.SUCCESS, location =
"/WEB-INF/content/myBigDecimalList.jsp"/*, type = "freemarker"*/)
})
public class MyBigDecimalList extends ActionSupport {

     public List<A> getMyList() {
         return myList;
     }

     public void setMyList(List<A> myList) {
         this.myList = myList;
     }

     private List<A> myList;

 
@org.apache.struts2.convention.annotation.Action(value="myBigDecimalList")
     public String execute() throws Exception {
         A a = new A();
         a.setStrVal("my str val");
         a.setCrAmt(new
BigDecimal("0.01000000000000000020816681711721685132943093776702880859375"));
         a.setDrAmt(new
BigDecimal("0.02000000000000000020816681711721685132943093776702880859375"));
         myList=new ArrayList<>();
         myList.add(a);
         NumberFormat format = NumberFormat.getNumberInstance(getLocale());
         //NumberFormat.set
         format.setMaximumFractionDigits(40);
         return Action.SUCCESS;
     }
}

```

```jsp
<%@taglib prefix="s" uri="/struts-tags"%>
<%--
   Created by IntelliJ IDEA.
   User: user
   Date: 8/18/2017
   Time: 12:36 AM
   To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
     <title>AdvancedWildcardAction1</title>
</head>
<body>
<s:form action="myBigDecimalList">
<s:hidden name='myList[0].strVal'/>
<s:hidden name='myList[0].drAmt'/>
<s:hidden name='myList[0].crAmt'/>
     <s:submit/>
</s:form>
</body>
</html>
```

Hope these help!
Yasser.

>
>
> --
> Sent from: http://struts.1045723.n5.nabble.com/Struts-User-f3426046.html
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [hidden email]
> For additional commands, e-mail: [hidden email]
>

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]
Reply | Threaded
Open this post in threaded view
|

Re: Iusse in BigDecimal conversion after upgrading from 2.5.10.1 to 2.5.13

sreekanth
thanks Yasser, let me setup a sample too. I already went through fixes
introduced at WW-4581 as well and ran a test against it and found that's not
the reason for this behavior rather something related to form field binding.

Here i'm adding your finding which i too believe the root cause for this new
behavior.

*
2.5.13. But now I also tested 2.5.10.1 and get following warn when try
to set empty string value to a BigDecimal:

Unexpected Exception caught setting 'myList[0].crAmt' on 'class
me.zamani.yasser.ww_convention.actions.MyBigDecimalList: Error setting
expression 'myList[0].crAmt' with value ['', ]
*



--
Sent from: http://struts.1045723.n5.nabble.com/Struts-User-f3426046.html

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: Iusse in BigDecimal conversion after upgrading from 2.5.10.1 to 2.5.13

Yasser Zamani


On 10/4/2017 10:45 AM, sreekanth wrote:

> thanks Yasser, let me setup a sample too. I already went through fixes
> introduced at WW-4581 as well and ran a test against it and found that's not
> the reason for this behavior rather something related to form field binding.
>
> Here i'm adding your finding which i too believe the root cause for this new
> behavior.
>
> *
> 2.5.13. But now I also tested 2.5.10.1 and get following warn when try
> to set empty string value to a BigDecimal:
>
> Unexpected Exception caught setting 'myList[0].crAmt' on 'class
> me.zamani.yasser.ww_convention.actions.MyBigDecimalList: Error setting
> expression 'myList[0].crAmt' with value ['', ]
> *
>

This is because of line#93 [1] in Struts 2.5.13:

```java
return format.parse(strValue);
```

Currently it cannot parse empty values to 0 as java's DecimalFormat
class cannot. Maybe we can change the behavior by replacing any empty
with "0" in Strut's future versions ?!

Until that time, I think you can extend NumberConverter and override
it's convertToBigDecimal method and use it instead (see also [2]):

```java
@override
     protected Object convertToBigDecimal(context,value) {
String strValue = String.valueOf(value);
if(null==value || strValue.isNullOrEmpty()) strValue="0";
return super.convertToBigDecimal(context,strValue);
}
```

Hope these help!
Yasser.

[1]
https://gitbox.apache.org/repos/asf?p=struts.git;a=blob;f=core/src/main/java/com/opensymphony/xwork2/conversion/impl/NumberConverter.java;h=16bbd499e00f03d4a673cd43d90dc261db3849df;hb=f874f9cde56f74c5161b17e645f779805c51a04b#l93
[2]
https://saifmasadeh.blogspot.com/2012/10/custom-converter-for-bigdecimal-struts2.htm

>
>
> --
> Sent from: http://struts.1045723.n5.nabble.com/Struts-User-f3426046.html
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [hidden email]
> For additional commands, e-mail: [hidden email]
>

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]
Reply | Threaded
Open this post in threaded view
|

Re: Iusse in BigDecimal conversion after upgrading from 2.5.10.1 to 2.5.13

Lukasz Lenart
2017-10-04 12:41 GMT+02:00 Yasser Zamani <[hidden email]>:

>> 2.5.13. But now I also tested 2.5.10.1 and get following warn when try
>> to set empty string value to a BigDecimal:
>>
>> Unexpected Exception caught setting 'myList[0].crAmt' on 'class
>> me.zamani.yasser.ww_convention.actions.MyBigDecimalList: Error setting
>> expression 'myList[0].crAmt' with value ['', ]
>
> This is because of line#93 [1] in Struts 2.5.13:
>
> ```java
> return format.parse(strValue);
> ```

This an expected behaviour, assigning an empty String to BigDecimal
means you want to have "null" - there is no default value as for
primitives (0 for int, false for boolean, etc). I would rather add a
required validator upfront instead of changing the conversion logic.


Regards
--
Łukasz
+ 48 606 323 122 http://www.lenart.org.pl/

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: Iusse in BigDecimal conversion after upgrading from 2.5.10.1 to 2.5.13

sreekanth
In reply to this post by Yasser Zamani
thanks Yasser for all you help and code.

I would like to conclude this as a struts2 upgrade change, as BigDecimal
field value was setting as BigDecimal.ZERO when field value passed as empty
in struts2.5.10.x or < whereas 2.5.13 will set such empty BigDecimal field
as null value (that is the correct behavior according to me as well), so
planning to do a through testing across my project and correct such issues.



--
Sent from: http://struts.1045723.n5.nabble.com/Struts-User-f3426046.html

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]