Invalid Field Value when using Map

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

Invalid Field Value when using Map

Prasanth-2
Hi,

I have a form that uses maps to store data as shown below. When the user enters valid numbers it works as expected, but when user enters non digit characters in the text field a String object is saved
in the map rather than showing a "Invalid field value for field <field name>" message which is done for basic data types like int/long/double. Is this something that struts has not implemented for
maps yet, as the annotations provide the expected data type?

     @Element(value=java.lang.Double.class)
     private HashMap<Long, Double> deferralAmountValue = new HashMap<Long,Double>();
     @Element(value=java.lang.Double.class)
     private HashMap<Long, Double> deferralPercentValue = new HashMap<Long,Double>();


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

RE: Invalid Field Value when using Map

Yasser Zamani-2
Hi Prasanth,

I'm surprised how you get non digit characters in your map while both key and value are not String!

Regards.

>-----Original Message-----
>From: Prasanth <[hidden email]>
>Sent: Monday, June 17, 2019 8:52 PM
>To: Struts Users Mailing List <[hidden email]>
>Subject: Invalid Field Value when using Map
>
>Hi,
>
>I have a form that uses maps to store data as shown below. When the user enters
>valid numbers it works as expected, but when user enters non digit characters in
>the text field a String object is saved in the map rather than showing a "Invalid
>field value for field <field name>" message which is done for basic data types like
>int/long/double. Is this something that struts has not implemented for maps yet,
>as the annotations provide the expected data type?
>
>     @Element(value=java.lang.Double.class)
>     private HashMap<Long, Double> deferralAmountValue = new
>HashMap<Long,Double>();
>     @Element(value=java.lang.Double.class)
>     private HashMap<Long, Double> deferralPercentValue = new
>HashMap<Long,Double>();
>
>
>Thanks,
>Prasanth

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

Re: Invalid Field Value when using Map

Prasanth Pasala
Hi Yasser,

I think at run time you can add any type of objects you want to the map. The checks are only at compile time on the data types of objects added to Maps.

Thanks,
Prasanth

On 6/19/19 1:29 AM, Yasser Zamani wrote:

> Hi Prasanth,
>
> I'm surprised how you get non digit characters in your map while both key and value are not String!
>
> Regards.
>
>> -----Original Message-----
>> From: Prasanth <[hidden email]>
>> Sent: Monday, June 17, 2019 8:52 PM
>> To: Struts Users Mailing List <[hidden email]>
>> Subject: Invalid Field Value when using Map
>>
>> Hi,
>>
>> I have a form that uses maps to store data as shown below. When the user enters
>> valid numbers it works as expected, but when user enters non digit characters in
>> the text field a String object is saved in the map rather than showing a "Invalid
>> field value for field <field name>" message which is done for basic data types like
>> int/long/double. Is this something that struts has not implemented for maps yet,
>> as the annotations provide the expected data type?
>>
>>      @Element(value=java.lang.Double.class)
>>      private HashMap<Long, Double> deferralAmountValue = new
>> HashMap<Long,Double>();
>>      @Element(value=java.lang.Double.class)
>>      private HashMap<Long, Double> deferralPercentValue = new
>> HashMap<Long,Double>();
>>
>>
>> Thanks,
>> Prasanth
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [hidden email]
> For additional commands, e-mail: [hidden email]
>

Reply | Threaded
Open this post in threaded view
|

Re: Invalid Field Value when using Map

Yasser Zamani-2
Oh interesting!

Have you seen [1]? e.g. conversion error interceptor should be present
in your interceptor stack. To debug, for example put a number field
which is not a map and see if conversion error appears - I think you
should also have fieldErrors tag in your jsp.

It seems your action also should be ValidationAware. Put a break-point
at [2] and see if your code reaches there and what happens next.

Regards.

[1]
https://struts.apache.org/core-developers/type-conversion.html#collection-and-map-support
[2]
https://github.com/apache/struts/blob/651eac2c57396aa11ee65002006b3123dd69cbb4/core/src/main/java/com/opensymphony/xwork2/interceptor/ConversionErrorInterceptor.java#L116

On 6/19/2019 10:35 PM, Prasanth Pasala wrote:

> Hi Yasser,
>
> I think at run time you can add any type of objects you want to the map.
> The checks are only at compile time on the data types of objects added
> to Maps.
>
> Thanks,
> Prasanth
>
> On 6/19/19 1:29 AM, Yasser Zamani wrote:
>> Hi Prasanth,
>>
>> I'm surprised how you get non digit characters in your map while both
>> key and value are not String!
>>
>> Regards.
>>
>>> -----Original Message-----
>>> From: Prasanth <[hidden email]>
>>> Sent: Monday, June 17, 2019 8:52 PM
>>> To: Struts Users Mailing List <[hidden email]>
>>> Subject: Invalid Field Value when using Map
>>>
>>> Hi,
>>>
>>> I have a form that uses maps to store data as shown below. When the
>>> user enters
>>> valid numbers it works as expected, but when user enters non digit
>>> characters in
>>> the text field a String object is saved in the map rather than
>>> showing a "Invalid
>>> field value for field <field name>" message which is done for basic
>>> data types like
>>> int/long/double. Is this something that struts has not implemented
>>> for maps yet,
>>> as the annotations provide the expected data type?
>>>
>>>      @Element(value=java.lang.Double.class)
>>>      private HashMap<Long, Double> deferralAmountValue = new
>>> HashMap<Long,Double>();
>>>      @Element(value=java.lang.Double.class)
>>>      private HashMap<Long, Double> deferralPercentValue = new
>>> HashMap<Long,Double>();
>>>
>>>
>>> Thanks,
>>> Prasanth
>> ---------------------------------------------------------------------
>> 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: Invalid Field Value when using Map

Prasanth-2
Hi Yasser,

The conversion error occurs when the field is not a map. I have a form with int/long fields, if the user enters non numeric value the form display error.

The actions are validation aware and the control does come to validation method. It is in the validation method that I usually get exceptions  as the code is expecting to have a Double but the type of
the object in the map is a String. I have tried to check in the validate method if the object is of type Double and if it is not a Double add a field error. When I do that OGNL seems to have an issue
when displaying the input form with the values in the map. Seems like OGNL is now expecting a Double value and is not able to handle a String object being present in the map. The JSP stops at the
field which has String value.

So even if I add additional validations to make sure all values in the map are Doubles it would still cause a problem as Struts would not be able to display the input form.

Thanks,
Prasanth

On 6/21/19 1:23 AM, Yasser Zamani wrote:

> Oh interesting!
>
> Have you seen [1]? e.g. conversion error interceptor should be present
> in your interceptor stack. To debug, for example put a number field
> which is not a map and see if conversion error appears - I think you
> should also have fieldErrors tag in your jsp.
>
> It seems your action also should be ValidationAware. Put a break-point
> at [2] and see if your code reaches there and what happens next.
>
> Regards.
>
> [1]
> https://struts.apache.org/core-developers/type-conversion.html#collection-and-map-support
> [2]
> https://github.com/apache/struts/blob/651eac2c57396aa11ee65002006b3123dd69cbb4/core/src/main/java/com/opensymphony/xwork2/interceptor/ConversionErrorInterceptor.java#L116
>
> On 6/19/2019 10:35 PM, Prasanth Pasala wrote:
>> Hi Yasser,
>>
>> I think at run time you can add any type of objects you want to the map.
>> The checks are only at compile time on the data types of objects added
>> to Maps.
>>
>> Thanks,
>> Prasanth
>>
>> On 6/19/19 1:29 AM, Yasser Zamani wrote:
>>> Hi Prasanth,
>>>
>>> I'm surprised how you get non digit characters in your map while both
>>> key and value are not String!
>>>
>>> Regards.
>>>
>>>> -----Original Message-----
>>>> From: Prasanth <[hidden email]>
>>>> Sent: Monday, June 17, 2019 8:52 PM
>>>> To: Struts Users Mailing List <[hidden email]>
>>>> Subject: Invalid Field Value when using Map
>>>>
>>>> Hi,
>>>>
>>>> I have a form that uses maps to store data as shown below. When the
>>>> user enters
>>>> valid numbers it works as expected, but when user enters non digit
>>>> characters in
>>>> the text field a String object is saved in the map rather than
>>>> showing a "Invalid
>>>> field value for field <field name>" message which is done for basic
>>>> data types like
>>>> int/long/double. Is this something that struts has not implemented
>>>> for maps yet,
>>>> as the annotations provide the expected data type?
>>>>
>>>>       @Element(value=java.lang.Double.class)
>>>>       private HashMap<Long, Double> deferralAmountValue = new
>>>> HashMap<Long,Double>();
>>>>       @Element(value=java.lang.Double.class)
>>>>       private HashMap<Long, Double> deferralPercentValue = new
>>>> HashMap<Long,Double>();
>>>>
>>>>
>>>> Thanks,
>>>> Prasanth
>>> ---------------------------------------------------------------------
>>> 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]
>

J C
Reply | Threaded
Open this post in threaded view
|

Re: Invalid Field Value when using Map

J C
In reply to this post by Prasanth-2
Hi Prasanth.

Do you have a small reproducer application available to demonstrate the behaviour ?  There might be something relevant in the configuration and setup too.

The behaviour you're seeing could have something to do with the @Element annotations identifying the types involved.  Maybe you can work-around the issue by providing explicit getters/setters using String values and perform direct type-conversions in those methods ?

Maybe another user has a suggestion (and if there's a reproducer application can comment further).

Regards,
James.


On 2019/06/17 16:21:55, Prasanth wrote:

> Hi,
>
> I have a form that uses maps to store data as shown below. When the user enters valid numbers it works as expected, but when > user enters non digit characters in the text field a String object is saved
> in the map rather than showing a "Invalid field value for field <field name>" message which is done for basic data types like
> int/long/double. Is this something that struts has not implemented for
> maps yet, as the annotations provide the expected data type?
>
>     @Element(value=java.lang.Double.class)
>     private HashMap<Long, Double> deferralAmountValue = new HashMap<Long,Double>();
>     @Element(value=java.lang.Double.class)
>     private HashMap<Long, Double> deferralPercentValue = new HashMap<Long,Double>();
>
>
> Thanks,
> Prasanth

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

Reply | Threaded
Open this post in threaded view
|

Re: Invalid Field Value when using Map

Martin Gainty


________________________________
From: [hidden email] <[hidden email]>
Sent: Saturday, June 22, 2019 12:47 AM
To: [hidden email]
Subject: Re: Invalid Field Value when using Map

Hi Prasanth.

Do you have a small reproducer application available to demonstrate the behaviour ?  There might be something relevant in the configuration and setup too.

The behaviour you're seeing could have something to do with the @Element annotations identifying the types involved.  Maybe you can work-around the issue by providing explicit getters/setters using String values and perform direct type-conversions in those methods ?

Maybe another user has a suggestion (and if there's a reproducer application can comment further).

Regards,
James.


On 2019/06/17 16:21:55, Prasanth wrote:

> Hi,
>
> I have a form that uses maps to store data as shown below. When the user enters valid numbers it works as expected, but when > user enters non digit characters in the text field a String object is saved
> in the map rather than showing a "Invalid field value for field <field name>" message which is done for basic data types like
> int/long/double. Is this something that struts has not implemented for
> maps yet, as the annotations provide the expected data type?
>
>     @Element(value=java.lang.Double.class)
>     private HashMap<Long, Double> deferralAmountValue = new HashMap<Long,Double>();
>     @Element(value=java.lang.Double.class)
>     private HashMap<Long, Double> deferralPercentValue = new HashMap<Long,Double>();
>
>
> Thanks,
> Prasanth

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

Reply | Threaded
Open this post in threaded view
|

Re: Invalid Field Value when using Map

Martin Gainty
In reply to this post by J C
supposedly the mapping class looks at Key_xxx and Element_xxx inside Class-conversion*.properties

 This {@link ObjectTypeDeterminer} looks at the <b>Class-conversion.properties</b> for entries that indicated what
 * objects are contained within Maps and Collections. For Collections, such as Lists, the element is specified using the
 * pattern <b>Element_xxx</b>, where xxx is the field name of the collection property in your action or object. For
 * Maps, both the key and the value may be specified by using the pattern <b>Key_xxx</b> and <b>Element_xxx</b>,
 * respectively.

in your fuBar Class
fuBar-conversion.properties would look like

# syntax: <type> = <converterClassName>
java.lang.Double = com.acme.MyDouble


    /**
     * Determines the element class by looking for the value of @Element annotation for the given
     * class.
     * If no annotation is found, the element class is determined by using the generic parametrics.
     *
     * As fallback, it determines the key class by looking for the value of Element_${property} in the properties
     * file for the given class. Also looks for the deprecated Collection_${property}
     *
     * @param parentClass the Class which contains as a property the Map or Collection we are finding the key for.
     * @param property    the property of the Map or Collection for the given parent class
     * @see com.opensymphony.xwork2.conversion.ObjectTypeDeterminer#getElementClass(Class, String, Object)
     */
    public Class getElementClass(Class parentClass, String property, Object key) {
        Element annotation = getAnnotation(parentClass, property, Element.class);

        if (annotation != null) {
            return annotation.value();
        }
//departure of getClass passing parentClass property and objectIsElement bool
        Class clazz = getClass(parentClass, property, true);

        if (clazz != null) {
            return clazz;
        }

        clazz = (Class) xworkConverter.getConverter(parentClass, ELEMENT_PREFIX + property);

        if (clazz == null) {
            clazz = (Class) xworkConverter
                    .getConverter(parentClass, DEPRECATED_ELEMENT_PREFIX + property);

            if (clazz != null) {
                LOG.info("The Collection_xxx pattern for collection type conversion is deprecated. Please use Element_xxx!");
            }
        }
        return clazz;
}

as you can see there is no conversion to String in getClass below
    /**
     * Returns the class for the given field via generic type check.
     *
     * @param parentClass the Class which contains as a property the Map or Collection we are finding the key for.
     * @param property    the property of the Map or Collection for the given parent class
     * @param element     <tt>true for indexed types and Maps.
     * @return Class of the specified field.
     */
    private Class getClass(Class parentClass, String property, boolean element) {
        try {
            Field field = reflectionProvider.getField(parentClass, property);

            Type genericType = null;

            // Check fields first
            if (field != null) {
                genericType = field.getGenericType();
            }

            // Try to get ParameterType from setter method
            if (genericType == null || !(genericType instanceof ParameterizedType)) {
                try {
                    Method setter = reflectionProvider.getSetMethod(parentClass, property);
                    genericType = setter.getGenericParameterTypes()[0];
                }
                catch (ReflectionException ognle) {
                    ; // ignore
                }
                catch (IntrospectionException ie) {
                    ; // ignore
                }
            }

            // Try to get ReturnType from getter method
            if (genericType == null || !(genericType instanceof ParameterizedType)) {
                try {
                    Method getter = reflectionProvider.getGetMethod(parentClass, property);
                    genericType = getter.getGenericReturnType();
                }
                catch (ReflectionException ognle) {
                    ; // ignore
                }
                catch (IntrospectionException ie) {
                    ; // ignore
                }
            }

            if (genericType instanceof ParameterizedType) {


                ParameterizedType type = (ParameterizedType) genericType;

                int index = (element && type.getRawType().toString().contains(Map.class.getName())) ? 1 : 0;

                Type resultType = type.getActualTypeArguments()[index];

                if ( resultType instanceof ParameterizedType) {
                    return (Class) ((ParameterizedType) resultType).getRawType();
                }
                return (Class) resultType;

            }
        } catch (Exception e) {
            if ( LOG.isDebugEnabled()) {
                LOG.debug("Error while retrieving generic property class for property=" + property, e);
            }
        }
        return null;
    }
}

of course any/all values passed thru HTTP GET are "passed as String" ...is this what you're seeing with WireShark?

M-

On 2019/06/17 16:21:55, Prasanth wrote:

> Hi,
>
> I have a form that uses maps to store data as shown below. When the user enters valid numbers it works as expected, but when > user enters non digit characters in the text field a String object is saved
> in the map rather than showing a "Invalid field value for field <field name>" message which is done for basic data types like
> int/long/double. Is this something that struts has not implemented for
> maps yet, as the annotations provide the expected data type?
>
>     @Element(value=java.lang.Double.class)
>     private HashMap<Long, Double> deferralAmountValue = new HashMap<Long,Double>();
>     @Element(value=java.lang.Double.class)
>     private HashMap<Long, Double> deferralPercentValue = new HashMap<Long,Double>();
>
>
> Thanks,
> Prasanth

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

Reply | Threaded
Open this post in threaded view
|

RE: Invalid Field Value when using Map

Yasser Zamani-2
In reply to this post by Prasanth-2
Hi,

I think putting breakpoints at [1] and [2] and seeing if you reach there and what happens next can help proceeding this issue.

Thanks in advance!

Regards.

[1] https://github.com/apache/struts/blob/651eac2c57396aa11ee65002006b3123dd69cbb4/core/src/main/java/com/opensymphony/xwork2/conversion/impl/DefaultObjectTypeDeterminer.java#L114
[2] https://github.com/apache/struts/blob/651eac2c57396aa11ee65002006b3123dd69cbb4/core/src/main/java/com/opensymphony/xwork2/interceptor/ConversionErrorInterceptor.java#L118


>-----Original Message-----
>From: Prasanth <[hidden email]>
>Sent: Friday, June 21, 2019 6:27 PM
>To: [hidden email]
>Subject: Re: Invalid Field Value when using Map
>
>Hi Yasser,
>
>The conversion error occurs when the field is not a map. I have a form with
>int/long fields, if the user enters non numeric value the form display error.
>
>The actions are validation aware and the control does come to validation
>method. It is in the validation method that I usually get exceptions  as the code is
>expecting to have a Double but the type of the object in the map is a String. I
>have tried to check in the validate method if the object is of type Double and if it
>is not a Double add a field error. When I do that OGNL seems to have an issue
>when displaying the input form with the values in the map. Seems like OGNL is
>now expecting a Double value and is not able to handle a String object being
>present in the map. The JSP stops at the field which has String value.
>
>So even if I add additional validations to make sure all values in the map are
>Doubles it would still cause a problem as Struts would not be able to display the
>input form.
>
>Thanks,
>Prasanth
>
>On 6/21/19 1:23 AM, Yasser Zamani wrote:
>> Oh interesting!
>>
>> Have you seen [1]? e.g. conversion error interceptor should be present
>> in your interceptor stack. To debug, for example put a number field
>> which is not a map and see if conversion error appears - I think you
>> should also have fieldErrors tag in your jsp.
>>
>> It seems your action also should be ValidationAware. Put a break-point
>> at [2] and see if your code reaches there and what happens next.
>>
>> Regards.
>>
>> [1]
>> https://struts.apache.org/core-developers/type-conversion.html#collect
>> ion-and-map-support
>> [2]
>> https://github.com/apache/struts/blob/651eac2c57396aa11ee65002006b3123
>>
>dd69cbb4/core/src/main/java/com/opensymphony/xwork2/interceptor/Conver
>> sionErrorInterceptor.java#L116
>>
>> On 6/19/2019 10:35 PM, Prasanth Pasala wrote:
>>> Hi Yasser,
>>>
>>> I think at run time you can add any type of objects you want to the map.
>>> The checks are only at compile time on the data types of objects
>>> added to Maps.
>>>
>>> Thanks,
>>> Prasanth
>>>
>>> On 6/19/19 1:29 AM, Yasser Zamani wrote:
>>>> Hi Prasanth,
>>>>
>>>> I'm surprised how you get non digit characters in your map while
>>>> both key and value are not String!
>>>>
>>>> Regards.
>>>>
>>>>> -----Original Message-----
>>>>> From: Prasanth <[hidden email]>
>>>>> Sent: Monday, June 17, 2019 8:52 PM
>>>>> To: Struts Users Mailing List <[hidden email]>
>>>>> Subject: Invalid Field Value when using Map
>>>>>
>>>>> Hi,
>>>>>
>>>>> I have a form that uses maps to store data as shown below. When the
>>>>> user enters valid numbers it works as expected, but when user
>>>>> enters non digit characters in the text field a String object is
>>>>> saved in the map rather than showing a "Invalid field value for
>>>>> field <field name>" message which is done for basic data types like
>>>>> int/long/double. Is this something that struts has not implemented
>>>>> for maps yet, as the annotations provide the expected data type?
>>>>>
>>>>>       @Element(value=java.lang.Double.class)
>>>>>       private HashMap<Long, Double> deferralAmountValue = new
>>>>> HashMap<Long,Double>();
>>>>>       @Element(value=java.lang.Double.class)
>>>>>       private HashMap<Long, Double> deferralPercentValue = new
>>>>> HashMap<Long,Double>();
>>>>>
>>>>>
>>>>> Thanks,
>>>>> Prasanth
>>>> --------------------------------------------------------------------
>>>> - 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]
>>


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

Re: Invalid Field Value when using Map

Prasanth Pasala
I have built a test project to just test the Map variables. In the validate method it does give a String object (for value of map). But seems like a field error is also added. If I try to use the map
in validate method I would get a class cast exception, but if I don't do anything with the map values then the input page is displayed with invalid field value error.

So I guess the issue is that my validate method is trying to validate the values in the map and that causes the exception, as the map has String values in it.

I have to update the validation methods to check if the value is of type Double or not. Is this how it is supposed to work? I would have thought that a field error would be added and the String value
would not be added to the Map as the code is expecting Doubles.

Thanks,
Prasanth

On 6/24/19 1:42 AM, Yasser Zamani wrote:

> Hi,
>
> I think putting breakpoints at [1] and [2] and seeing if you reach there and what happens next can help proceeding this issue.
>
> Thanks in advance!
>
> Regards.
>
> [1] https://github.com/apache/struts/blob/651eac2c57396aa11ee65002006b3123dd69cbb4/core/src/main/java/com/opensymphony/xwork2/conversion/impl/DefaultObjectTypeDeterminer.java#L114
> [2] https://github.com/apache/struts/blob/651eac2c57396aa11ee65002006b3123dd69cbb4/core/src/main/java/com/opensymphony/xwork2/interceptor/ConversionErrorInterceptor.java#L118
>
>
>> -----Original Message-----
>> From: Prasanth <[hidden email]>
>> Sent: Friday, June 21, 2019 6:27 PM
>> To: [hidden email]
>> Subject: Re: Invalid Field Value when using Map
>>
>> Hi Yasser,
>>
>> The conversion error occurs when the field is not a map. I have a form with
>> int/long fields, if the user enters non numeric value the form display error.
>>
>> The actions are validation aware and the control does come to validation
>> method. It is in the validation method that I usually get exceptions  as the code is
>> expecting to have a Double but the type of the object in the map is a String. I
>> have tried to check in the validate method if the object is of type Double and if it
>> is not a Double add a field error. When I do that OGNL seems to have an issue
>> when displaying the input form with the values in the map. Seems like OGNL is
>> now expecting a Double value and is not able to handle a String object being
>> present in the map. The JSP stops at the field which has String value.
>>
>> So even if I add additional validations to make sure all values in the map are
>> Doubles it would still cause a problem as Struts would not be able to display the
>> input form.
>>
>> Thanks,
>> Prasanth
>>
>> On 6/21/19 1:23 AM, Yasser Zamani wrote:
>>> Oh interesting!
>>>
>>> Have you seen [1]? e.g. conversion error interceptor should be present
>>> in your interceptor stack. To debug, for example put a number field
>>> which is not a map and see if conversion error appears - I think you
>>> should also have fieldErrors tag in your jsp.
>>>
>>> It seems your action also should be ValidationAware. Put a break-point
>>> at [2] and see if your code reaches there and what happens next.
>>>
>>> Regards.
>>>
>>> [1]
>>> https://struts.apache.org/core-developers/type-conversion.html#collect
>>> ion-and-map-support
>>> [2]
>>> https://github.com/apache/struts/blob/651eac2c57396aa11ee65002006b3123
>>>
>> dd69cbb4/core/src/main/java/com/opensymphony/xwork2/interceptor/Conver
>>> sionErrorInterceptor.java#L116
>>>
>>> On 6/19/2019 10:35 PM, Prasanth Pasala wrote:
>>>> Hi Yasser,
>>>>
>>>> I think at run time you can add any type of objects you want to the map.
>>>> The checks are only at compile time on the data types of objects
>>>> added to Maps.
>>>>
>>>> Thanks,
>>>> Prasanth
>>>>
>>>> On 6/19/19 1:29 AM, Yasser Zamani wrote:
>>>>> Hi Prasanth,
>>>>>
>>>>> I'm surprised how you get non digit characters in your map while
>>>>> both key and value are not String!
>>>>>
>>>>> Regards.
>>>>>
>>>>>> -----Original Message-----
>>>>>> From: Prasanth <[hidden email]>
>>>>>> Sent: Monday, June 17, 2019 8:52 PM
>>>>>> To: Struts Users Mailing List <[hidden email]>
>>>>>> Subject: Invalid Field Value when using Map
>>>>>>
>>>>>> Hi,
>>>>>>
>>>>>> I have a form that uses maps to store data as shown below. When the
>>>>>> user enters valid numbers it works as expected, but when user
>>>>>> enters non digit characters in the text field a String object is
>>>>>> saved in the map rather than showing a "Invalid field value for
>>>>>> field <field name>" message which is done for basic data types like
>>>>>> int/long/double. Is this something that struts has not implemented
>>>>>> for maps yet, as the annotations provide the expected data type?
>>>>>>
>>>>>>        @Element(value=java.lang.Double.class)
>>>>>>        private HashMap<Long, Double> deferralAmountValue = new
>>>>>> HashMap<Long,Double>();
>>>>>>        @Element(value=java.lang.Double.class)
>>>>>>        private HashMap<Long, Double> deferralPercentValue = new
>>>>>> HashMap<Long,Double>();
>>>>>>
>>>>>>
>>>>>> Thanks,
>>>>>> Prasanth
>>>>> --------------------------------------------------------------------
>>>>> - 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]
>>>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [hidden email]
> For additional commands, e-mail: [hidden email]
>

Reply | Threaded
Open this post in threaded view
|

Re: Invalid Field Value when using Map

Yasser Zamani-2


On 6/24/2019 8:51 PM, Prasanth Pasala wrote:

> I have built a test project to just test the Map variables. In the
> validate method it does give a String object (for value of map). But
> seems like a field error is also added. If I try to use the map in
> validate method I would get a class cast exception, but if I don't do
> anything with the map values then the input page is displayed with
> invalid field value error.
>
> So I guess the issue is that my validate method is trying to validate
> the values in the map and that causes the exception, as the map has
> String values in it.
>
> I have to update the validation methods to check if the value is of type
> Double or not. Is this how it is supposed to work?

I also like to not see that value in Map but maybe it's required for
some reason - I didn't check. But you may use following workaround:

if (!hasFieldErrors() || !getFieldErrors().contains(myFieldName) {
    // my extra customized validation logic for myFieldName
}

>  I would have thought> that a field error would be added and the String value would not be
> added to the Map as the code is expecting Doubles.

Me too! Could you please put a break-point at your app's [1] and see
what happens next for that field?

Regards.

[1]
https://github.com/apache/struts/blob/651eac2c57396aa11ee65002006b3123dd69cbb4/core/src/main/java/com/opensymphony/xwork2/conversion/impl/XWorkConverter.java#L445



>
> Thanks,
> Prasanth
>
> On 6/24/19 1:42 AM, Yasser Zamani wrote:
>> Hi,
>>
>> I think putting breakpoints at [1] and [2] and seeing if you reach
>> there and what happens next can help proceeding this issue.
>>
>> Thanks in advance!
>>
>> Regards.
>>
>> [1]
>> https://github.com/apache/struts/blob/651eac2c57396aa11ee65002006b3123dd69cbb4/core/src/main/java/com/opensymphony/xwork2/conversion/impl/DefaultObjectTypeDeterminer.java#L114
>>
>> [2]
>> https://github.com/apache/struts/blob/651eac2c57396aa11ee65002006b3123dd69cbb4/core/src/main/java/com/opensymphony/xwork2/interceptor/ConversionErrorInterceptor.java#L118
>>
>>
>>
>>> -----Original Message-----
>>> From: Prasanth <[hidden email]>
>>> Sent: Friday, June 21, 2019 6:27 PM
>>> To: [hidden email]
>>> Subject: Re: Invalid Field Value when using Map
>>>
>>> Hi Yasser,
>>>
>>> The conversion error occurs when the field is not a map. I have a
>>> form with
>>> int/long fields, if the user enters non numeric value the form
>>> display error.
>>>
>>> The actions are validation aware and the control does come to validation
>>> method. It is in the validation method that I usually get exceptions 
>>> as the code is
>>> expecting to have a Double but the type of the object in the map is a
>>> String. I
>>> have tried to check in the validate method if the object is of type
>>> Double and if it
>>> is not a Double add a field error. When I do that OGNL seems to have
>>> an issue
>>> when displaying the input form with the values in the map. Seems like
>>> OGNL is
>>> now expecting a Double value and is not able to handle a String
>>> object being
>>> present in the map. The JSP stops at the field which has String value.
>>>
>>> So even if I add additional validations to make sure all values in
>>> the map are
>>> Doubles it would still cause a problem as Struts would not be able to
>>> display the
>>> input form.
>>>
>>> Thanks,
>>> Prasanth
>>>
>>> On 6/21/19 1:23 AM, Yasser Zamani wrote:
>>>> Oh interesting!
>>>>
>>>> Have you seen [1]? e.g. conversion error interceptor should be present
>>>> in your interceptor stack. To debug, for example put a number field
>>>> which is not a map and see if conversion error appears - I think you
>>>> should also have fieldErrors tag in your jsp.
>>>>
>>>> It seems your action also should be ValidationAware. Put a break-point
>>>> at [2] and see if your code reaches there and what happens next.
>>>>
>>>> Regards.
>>>>
>>>> [1]
>>>> https://struts.apache.org/core-developers/type-conversion.html#collect
>>>> ion-and-map-support
>>>> [2]
>>>> https://github.com/apache/struts/blob/651eac2c57396aa11ee65002006b3123
>>>>
>>> dd69cbb4/core/src/main/java/com/opensymphony/xwork2/interceptor/Conver
>>>> sionErrorInterceptor.java#L116
>>>>
>>>> On 6/19/2019 10:35 PM, Prasanth Pasala wrote:
>>>>> Hi Yasser,
>>>>>
>>>>> I think at run time you can add any type of objects you want to the
>>>>> map.
>>>>> The checks are only at compile time on the data types of objects
>>>>> added to Maps.
>>>>>
>>>>> Thanks,
>>>>> Prasanth
>>>>>
>>>>> On 6/19/19 1:29 AM, Yasser Zamani wrote:
>>>>>> Hi Prasanth,
>>>>>>
>>>>>> I'm surprised how you get non digit characters in your map while
>>>>>> both key and value are not String!
>>>>>>
>>>>>> Regards.
>>>>>>
>>>>>>> -----Original Message-----
>>>>>>> From: Prasanth <[hidden email]>
>>>>>>> Sent: Monday, June 17, 2019 8:52 PM
>>>>>>> To: Struts Users Mailing List <[hidden email]>
>>>>>>> Subject: Invalid Field Value when using Map
>>>>>>>
>>>>>>> Hi,
>>>>>>>
>>>>>>> I have a form that uses maps to store data as shown below. When the
>>>>>>> user enters valid numbers it works as expected, but when user
>>>>>>> enters non digit characters in the text field a String object is
>>>>>>> saved in the map rather than showing a "Invalid field value for
>>>>>>> field <field name>" message which is done for basic data types like
>>>>>>> int/long/double. Is this something that struts has not implemented
>>>>>>> for maps yet, as the annotations provide the expected data type?
>>>>>>>
>>>>>>>        @Element(value=java.lang.Double.class)
>>>>>>>        private HashMap<Long, Double> deferralAmountValue = new
>>>>>>> HashMap<Long,Double>();
>>>>>>>        @Element(value=java.lang.Double.class)
>>>>>>>        private HashMap<Long, Double> deferralPercentValue = new
>>>>>>> HashMap<Long,Double>();
>>>>>>>
>>>>>>>
>>>>>>> Thanks,
>>>>>>> Prasanth
>>>>>> --------------------------------------------------------------------
>>>>>> - 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]
>>>>
>>
>> ---------------------------------------------------------------------
>> 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]