2012/9/21 Miguel Almeida <mig...@almeida.at>:
> Sure!
>
> Here's a maven project with a failing test case.
>
> There's actually a map being built, but it's a <String,String>, and the key
> reads "OGNL no conversion possible".

Got it working, but I think the main problem is with key value - it
isn't a simple string but an int that must be converted to complex
class Entity.

First thing, in TypeConversionAction-conversion.properties just put:
Key_simpleMap=test.example.Entity
Element_simpleMap=java.lang.String
CreateIfNull_codePropertyMap=true

Next create xwork-conversion.properties in the root folder
(src/java/resources) with:
test.example.Entity=test.example.EntityConverter

And the very end create EntityConverter (Struts2 must know how to
convert int into Entity)

public class EntityConverter extends StrutsTypeConverter {

    @Override
    public Object convertFromString(Map context, String[] values,
Class toClass) {
        if (values != null && values.length > 0) {
            Entity entity = new Entity();
            entity.setId(Integer.parseInt(values[0]));
            return entity;
        }
        return null;
    }

    @Override
    public String convertToString(Map context, Object o) {
        if (o != null) {
            return "" + ((Entity) o).getId();
        }
        return null;
    }

    @Override
    public Object convertValue(Map context, Object o, Class toClass) {
        if (toClass == Entity.class) {
            return convertFromString(context, new
String[]{o.toString()}, toClass);
        }
        return super.convertValue(context, o, toClass);
    }
}

The problem is in convertValue() method which supports only String
based conversions (but simpleMap[1] => key = 1 was already converted
to int) - and I'm thinking that the method should be changed from

    public Object convertValue(Map context, Object o, Class toClass) {
        if (toClass.equals(String.class)) {
            return convertToString(context, o);
        } else if (o instanceof String[]) {
            return convertFromString(context, (String[]) o, toClass);
        } else if (o instanceof String) {
            return convertFromString(context, new String[]{(String)
o}, toClass); // <-- problematic line
        } else {
            return performFallbackConversion(context, o, toClass);
        }
    }

to

    public Object convertValue(Map context, Object o, Class toClass) {
        if (toClass.equals(String.class)) {
            return convertToString(context, o);
        } else if (o instanceof String[]) {
            return convertFromString(context, (String[]) o, toClass);
// <-- changed
        } else {
            return convertFromString(context, new
String[]{o.toString()}, toClass);
        }
    }

I've been using Struts 2.3.5-SNAPSHOT


Kind regards
-- 
Ɓukasz
+ 48 606 323 122 http://www.lenart.org.pl/

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscr...@struts.apache.org
For additional commands, e-mail: user-h...@struts.apache.org

Reply via email to