Map backed form

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

Map backed form

Prasanth-2
Hi,

I have a map (Map<Long, Double>) but based on the data entered by the user the value in the Map is Double or String. Example if the user enters a value of 10 then it is coming in as double but if the
user enters 10.5 then a string is present in the value. Does Struts2 look at the data types of the key and value of the map and set the key and values in the map accordingly?

If not, what is the best practice for the declaration of Map, is there a way to force Struts2 to only have Double objects as values?

Thanks,
Prasanth
Reply | Threaded
Open this post in threaded view
|

Re: Map backed form

Lukasz Lenart
pt., 15 lut 2019 o 00:17 Prasanth <[hidden email]> napisał(a):
> I have a map (Map<Long, Double>) but based on the data entered by the user the value in the Map is Double or String. Example if the user enters a value of 10 then it is coming in as double but if the
> user enters 10.5 then a string is present in the value. Does Struts2 look at the data types of the key and value of the map and set the key and values in the map accordingly?

Struts is not a problem here, generic types are erased at runtime, so
basically this a Map<Object, Object> at the end
https://docs.oracle.com/javase/tutorial/java/generics/erasure.html

> If not, what is the best practice for the declaration of Map, is there a way to force Struts2 to only have Double objects as values?

I would probably use a custom type instead of Double and implement
converter for it, or even replace the Map with a List of custom
objects.


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: Map backed form

Prasanth-2
Does struts follow a specific logic as to what the data type of the object would be based on user input? Meaning when is it converted to a Double vs Integer vs String?

Thanks,
Prasanth

On 2/15/19 7:45 AM, Lukasz Lenart wrote:

> pt., 15 lut 2019 o 00:17 Prasanth <[hidden email]> napisał(a):
>> I have a map (Map<Long, Double>) but based on the data entered by the user the value in the Map is Double or String. Example if the user enters a value of 10 then it is coming in as double but if the
>> user enters 10.5 then a string is present in the value. Does Struts2 look at the data types of the key and value of the map and set the key and values in the map accordingly?
> Struts is not a problem here, generic types are erased at runtime, so
> basically this a Map<Object, Object> at the end
> https://docs.oracle.com/javase/tutorial/java/generics/erasure.html
>
>> If not, what is the best practice for the declaration of Map, is there a way to force Struts2 to only have Double objects as values?
> I would probably use a custom type instead of Double and implement
> converter for it, or even replace the Map with a List of custom
> objects.
>
>
> Regards

Reply | Threaded
Open this post in threaded view
|

RE: Map backed form

Yasser Zamani-2
I think a file named yourActionClass-conversion.properties beside your action with following contents should help [1]:

Key_xxx=java.lang.Long
Element_xxx=java.lang.Double

(where xxx is the field name of the collection property in your action or object)

Regards.

[1] https://struts.apache.org/core-developers/type-conversion.html#collection-and-map-support

>-----Original Message-----
>From: Prasanth <[hidden email]>
>Sent: Friday, February 15, 2019 9:08 PM
>To: [hidden email]
>Subject: Re: Map backed form
>
>Does struts follow a specific logic as to what the data type of the object would be
>based on user input? Meaning when is it converted to a Double vs Integer vs
>String?
>
>Thanks,
>Prasanth
>
>On 2/15/19 7:45 AM, Lukasz Lenart wrote:
>> pt., 15 lut 2019 o 00:17 Prasanth <[hidden email]> napisał(a):
>>> I have a map (Map<Long, Double>) but based on the data entered by the
>>> user the value in the Map is Double or String. Example if the user enters a
>value of 10 then it is coming in as double but if the user enters 10.5 then a string
>is present in the value. Does Struts2 look at the data types of the key and value of
>the map and set the key and values in the map accordingly?
>> Struts is not a problem here, generic types are erased at runtime, so
>> basically this a Map<Object, Object> at the end
>> https://docs.oracle.com/javase/tutorial/java/generics/erasure.html
>>
>>> If not, what is the best practice for the declaration of Map, is there a way to
>force Struts2 to only have Double objects as values?
>> I would probably use a custom type instead of Double and implement
>> converter for it, or even replace the Map with a List of custom
>> objects.
>>
>>
>> Regards


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

Re: Map backed form

Lukasz Lenart
sob., 16 lut 2019 o 10:28 Yasser Zamani <[hidden email]> napisał(a):
>
> I think a file named yourActionClass-conversion.properties beside your action with following contents should help [1]:
>
> Key_xxx=java.lang.Long
> Element_xxx=java.lang.Double
>
> (where xxx is the field name of the collection property in your action or object)

Ahh right, totally forgot about that :)


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: Map backed form

Prasanth-2
In reply to this post by Yasser Zamani-2
Thank you Yasser. I will try that.


On 2/16/19 3:20 AM, Yasser Zamani wrote:

> I think a file named yourActionClass-conversion.properties beside your action with following contents should help [1]:
>
> Key_xxx=java.lang.Long
> Element_xxx=java.lang.Double
>
> (where xxx is the field name of the collection property in your action or object)
>
> Regards.
>
> [1] https://struts.apache.org/core-developers/type-conversion.html#collection-and-map-support
>
>> -----Original Message-----
>> From: Prasanth <[hidden email]>
>> Sent: Friday, February 15, 2019 9:08 PM
>> To: [hidden email]
>> Subject: Re: Map backed form
>>
>> Does struts follow a specific logic as to what the data type of the object would be
>> based on user input? Meaning when is it converted to a Double vs Integer vs
>> String?
>>
>> Thanks,
>> Prasanth
>>
>> On 2/15/19 7:45 AM, Lukasz Lenart wrote:
>>> pt., 15 lut 2019 o 00:17 Prasanth <[hidden email]> napisał(a):
>>>> I have a map (Map<Long, Double>) but based on the data entered by the
>>>> user the value in the Map is Double or String. Example if the user enters a
>> value of 10 then it is coming in as double but if the user enters 10.5 then a string
>> is present in the value. Does Struts2 look at the data types of the key and value of
>> the map and set the key and values in the map accordingly?
>>> Struts is not a problem here, generic types are erased at runtime, so
>>> basically this a Map<Object, Object> at the end
>>> https://docs.oracle.com/javase/tutorial/java/generics/erasure.html
>>>
>>>> If not, what is the best practice for the declaration of Map, is there a way to
>> force Struts2 to only have Double objects as values?
>>> I would probably use a custom type instead of Double and implement
>>> converter for it, or even replace the Map with a List of custom
>>> objects.
>>>
>>>
>>> Regards
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [hidden email]
> For additional commands, e-mail: [hidden email]
>

Reply | Threaded
Open this post in threaded view
|

Re: Map backed form

Prasanth-2
In reply to this post by Yasser Zamani-2
I have created the below in CompDeferralAction-conversion.properties (action class is CompDeferralAction). The variable name is deferralCode. But still struts is populating the value as Long rather
than Integer. Am I missing anything here?
This action is ModelDriven so the deferralCode variable is in a different class.

Looking at the documentation seems like it should be KeyProperty, so I named it as such.
KeyProperty_deferralCode=java.lang.Long
Element_deferralCode=java.lang.Interger

I have also tried the annotations

    @TypeConversion(rule=ConversionRule.MAP, key="java.lang.Long", value="java.lang.Integer")
    public void setDeferralCode(HashMap<Long, Integer> deferralCode) {
        this.deferralCode = deferralCode;
    }

Thanks,
Prasanth

On 2/16/19 3:20 AM, Yasser Zamani wrote:

> I think a file named yourActionClass-conversion.properties beside your action with following contents should help [1]:
>
> Key_xxx=java.lang.Long
> Element_xxx=java.lang.Double
>
> (where xxx is the field name of the collection property in your action or object)
>
> Regards.
>
> [1] https://struts.apache.org/core-developers/type-conversion.html#collection-and-map-support
>
>> -----Original Message-----
>> From: Prasanth <[hidden email]>
>> Sent: Friday, February 15, 2019 9:08 PM
>> To: [hidden email]
>> Subject: Re: Map backed form
>>
>> Does struts follow a specific logic as to what the data type of the object would be
>> based on user input? Meaning when is it converted to a Double vs Integer vs
>> String?
>>
>> Thanks,
>> Prasanth
>>
>> On 2/15/19 7:45 AM, Lukasz Lenart wrote:
>>> pt., 15 lut 2019 o 00:17 Prasanth <[hidden email]> napisał(a):
>>>> I have a map (Map<Long, Double>) but based on the data entered by the
>>>> user the value in the Map is Double or String. Example if the user enters a
>> value of 10 then it is coming in as double but if the user enters 10.5 then a string
>> is present in the value. Does Struts2 look at the data types of the key and value of
>> the map and set the key and values in the map accordingly?
>>> Struts is not a problem here, generic types are erased at runtime, so
>>> basically this a Map<Object, Object> at the end
>>> https://docs.oracle.com/javase/tutorial/java/generics/erasure.html
>>>
>>>> If not, what is the best practice for the declaration of Map, is there a way to
>> force Struts2 to only have Double objects as values?
>>> I would probably use a custom type instead of Double and implement
>>> converter for it, or even replace the Map with a List of custom
>>> objects.
>>>
>>>
>>> Regards
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [hidden email]
> For additional commands, e-mail: [hidden email]
>

Reply | Threaded
Open this post in threaded view
|

Re: Map backed form

Yasser Zamani-2
Hi Prasanth,

I searched and saw it is some different for ModelDriven [1].

Regards.

[1]
https://struts.apache.org/core-developers/type-conversion.html#applying-a-type-converter-to-a-bean-or-model
(Applying a Type Converter to a bean or model)

On 3/6/2019 7:36 PM, Prasanth wrote:

> I have created the below in CompDeferralAction-conversion.properties (action class is CompDeferralAction). The variable name is deferralCode. But still struts is populating the value as Long rather
> than Integer. Am I missing anything here?
> This action is ModelDriven so the deferralCode variable is in a different class.
>
> Looking at the documentation seems like it should be KeyProperty, so I named it as such.
> KeyProperty_deferralCode=java.lang.Long
> Element_deferralCode=java.lang.Interger
>
> I have also tried the annotations
>
>     @TypeConversion(rule=ConversionRule.MAP, key="java.lang.Long", value="java.lang.Integer")
>     public void setDeferralCode(HashMap<Long, Integer> deferralCode) {
>         this.deferralCode = deferralCode;
>     }
>
> Thanks,
> Prasanth
>
> On 2/16/19 3:20 AM, Yasser Zamani wrote:
>> I think a file named yourActionClass-conversion.properties beside your action with following contents should help [1]:
>>
>> Key_xxx=java.lang.Long
>> Element_xxx=java.lang.Double
>>
>> (where xxx is the field name of the collection property in your action or object)
>>
>> Regards.
>>
>> [1] https://struts.apache.org/core-developers/type-conversion.html#collection-and-map-support
>>
>>> -----Original Message-----
>>> From: Prasanth <[hidden email]>
>>> Sent: Friday, February 15, 2019 9:08 PM
>>> To: [hidden email]
>>> Subject: Re: Map backed form
>>>
>>> Does struts follow a specific logic as to what the data type of the object would be
>>> based on user input? Meaning when is it converted to a Double vs Integer vs
>>> String?
>>>
>>> Thanks,
>>> Prasanth
>>>
>>> On 2/15/19 7:45 AM, Lukasz Lenart wrote:
>>>> pt., 15 lut 2019 o 00:17 Prasanth <[hidden email]> napisał(a):
>>>>> I have a map (Map<Long, Double>) but based on the data entered by the
>>>>> user the value in the Map is Double or String. Example if the user enters a
>>> value of 10 then it is coming in as double but if the user enters 10.5 then a string
>>> is present in the value. Does Struts2 look at the data types of the key and value of
>>> the map and set the key and values in the map accordingly?
>>>> Struts is not a problem here, generic types are erased at runtime, so
>>>> basically this a Map<Object, Object> at the end
>>>> https://docs.oracle.com/javase/tutorial/java/generics/erasure.html
>>>>
>>>>> If not, what is the best practice for the declaration of Map, is there a way to
>>> force Struts2 to only have Double objects as values?
>>>> I would probably use a custom type instead of Double and implement
>>>> converter for it, or even replace the Map with a List of custom
>>>> objects.
>>>>
>>>>
>>>> Regards
>>
>> ---------------------------------------------------------------------
>> 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: Map backed form

Prasanth-2
Thank you Yasser. Changed the name of the properties file to use the model class name, it is in same package as the action so no change in location. But no success.

After reading the link you sent, the text in the properties file should be Key_xxx & Element_xxx as you said before and should not be KeyProperty (this is for lists to identify the id inside the
element itself).

I have now tried the @Element on the variable rather than @TypeConversion which has to be used on setXXX method. This is working now as expected. Not sure why properties file or @TypeConversion did
not work.
https://struts.apache.org/core-developers/element-annotation.html

Thanks for all your help.

Thanks,
Prasanth

On 3/7/19 3:57 AM, Yasser Zamani wrote:

> Hi Prasanth,
>
> I searched and saw it is some different for ModelDriven [1].
>
> Regards.
>
> [1]
> https://struts.apache.org/core-developers/type-conversion.html#applying-a-type-converter-to-a-bean-or-model
> (Applying a Type Converter to a bean or model)
>
> On 3/6/2019 7:36 PM, Prasanth wrote:
>> I have created the below in CompDeferralAction-conversion.properties (action class is CompDeferralAction). The variable name is deferralCode. But still struts is populating the value as Long rather
>> than Integer. Am I missing anything here?
>> This action is ModelDriven so the deferralCode variable is in a different class.
>>
>> Looking at the documentation seems like it should be KeyProperty, so I named it as such.
>> KeyProperty_deferralCode=java.lang.Long
>> Element_deferralCode=java.lang.Interger
>>
>> I have also tried the annotations
>>
>>     @TypeConversion(rule=ConversionRule.MAP, key="java.lang.Long", value="java.lang.Integer")
>>     public void setDeferralCode(HashMap<Long, Integer> deferralCode) {
>>         this.deferralCode = deferralCode;
>>     }
>>
>> Thanks,
>> Prasanth
>>
>> On 2/16/19 3:20 AM, Yasser Zamani wrote:
>>> I think a file named yourActionClass-conversion.properties beside your action with following contents should help [1]:
>>>
>>> Key_xxx=java.lang.Long
>>> Element_xxx=java.lang.Double
>>>
>>> (where xxx is the field name of the collection property in your action or object)
>>>
>>> Regards.
>>>
>>> [1] https://struts.apache.org/core-developers/type-conversion.html#collection-and-map-support
>>>
>>>> -----Original Message-----
>>>> From: Prasanth <[hidden email]>
>>>> Sent: Friday, February 15, 2019 9:08 PM
>>>> To: [hidden email]
>>>> Subject: Re: Map backed form
>>>>
>>>> Does struts follow a specific logic as to what the data type of the object would be
>>>> based on user input? Meaning when is it converted to a Double vs Integer vs
>>>> String?
>>>>
>>>> Thanks,
>>>> Prasanth
>>>>
>>>> On 2/15/19 7:45 AM, Lukasz Lenart wrote:
>>>>> pt., 15 lut 2019 o 00:17 Prasanth <[hidden email]> napisał(a):
>>>>>> I have a map (Map<Long, Double>) but based on the data entered by the
>>>>>> user the value in the Map is Double or String. Example if the user enters a
>>>> value of 10 then it is coming in as double but if the user enters 10.5 then a string
>>>> is present in the value. Does Struts2 look at the data types of the key and value of
>>>> the map and set the key and values in the map accordingly?
>>>>> Struts is not a problem here, generic types are erased at runtime, so
>>>>> basically this a Map<Object, Object> at the end
>>>>> https://docs.oracle.com/javase/tutorial/java/generics/erasure.html
>>>>>
>>>>>> If not, what is the best practice for the declaration of Map, is there a way to
>>>> force Struts2 to only have Double objects as values?
>>>>> I would probably use a custom type instead of Double and implement
>>>>> converter for it, or even replace the Map with a List of custom
>>>>> objects.
>>>>>
>>>>>
>>>>> Regards
>>> ---------------------------------------------------------------------
>>> 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]
>