struts-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Martin Gainty <mgai...@hotmail.com>
Subject Re: Invalid Field Value when using Map
Date Sat, 22 Jun 2019 22:25:43 GMT
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: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message