You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@struts.apache.org by Piotr Matkowski <ze...@gmail.com> on 2008/09/18 11:35:29 UTC

Enum type conversion

Hi,
I was wondering, is there some easy way (annotation, config) to perform type
conversion on Enum types?
I noticed that struts does not handle Enum types by default, which is not
what I expected.

Lets say we have an Enum like that:

    public enum Fruit { ORANGE, APPLE; }

and Action with field and setter:

    private Fruit fruit;
    public void setFruit(Fruit fruit) { this.fruit = fruit; }

I want to submit String ORANGE from html form and get Fruit.ORANGE object in
my Action after population.
I wrote my own type converter, here's the code:

public class EnumConverter extends StrutsTypeConverter {
    public Object convertFromString(Map context, String[] values, Class
toClass) {
        try {
            return toClass.getMethod("valueOf",
String.class).invoke(toClass, values[0]);
        } catch (Exception e) {
            throw new TypeConversionException(e);
        }
    }
    public String convertToString(Map context, Object o) {
        return ((Enum) o).name();
    }
}

It works for me, but the thing is, the code is quite ugly (i mean the java
reflection part).
Does someone know a better way to do that?
Maybe Enums should be handled by struts by default? Why they aren't handled?
Any comments appreciated,
regards
Piotr

Re: Enum type conversion

Posted by Piotr Matkowski <ze...@gmail.com>.
ok, I just found it: there is already a converter for enums, I missed
somehow: com.opensymphony.xwork2.util.EnumTypeConverter
Piotr

2008/9/18 Piotr Matkowski <ze...@gmail.com>

> Hi,
> I was wondering, is there some easy way (annotation, config) to perform
> type conversion on Enum types?
> I noticed that struts does not handle Enum types by default, which is not
> what I expected.
>
> Lets say we have an Enum like that:
>
>     public enum Fruit { ORANGE, APPLE; }
>
> and Action with field and setter:
>
>     private Fruit fruit;
>     public void setFruit(Fruit fruit) { this.fruit = fruit; }
>
> I want to submit String ORANGE from html form and get Fruit.ORANGE object
> in my Action after population.
> I wrote my own type converter, here's the code:
>
> public class EnumConverter extends StrutsTypeConverter {
>     public Object convertFromString(Map context, String[] values, Class
> toClass) {
>         try {
>             return toClass.getMethod("valueOf",
> String.class).invoke(toClass, values[0]);
>         } catch (Exception e) {
>             throw new TypeConversionException(e);
>         }
>     }
>     public String convertToString(Map context, Object o) {
>         return ((Enum) o).name();
>     }
> }
>
> It works for me, but the thing is, the code is quite ugly (i mean the java
> reflection part).
> Does someone know a better way to do that?
> Maybe Enums should be handled by struts by default? Why they aren't
> handled?
> Any comments appreciated,
> regards
> Piotr
>

Re: Enum type conversion

Posted by Musachy Barroso <mu...@gmail.com>.
Yeah, this was a big annoyance and Don fixed it at some point.

On Thu, Sep 18, 2008 at 8:35 AM, Jeromy Evans <
jeromy.evans@blueskyminds.com.au> wrote:

> Bob Tiernay wrote:
>
>> I'm not 100% sure, but I though Musachy enabled this by default?
>>
>
> Ah, yes, Don did for 2.1.0,
> https://issues.apache.org/struts/browse/WW-2162
>
> But it's not back-ported to 2.0.x
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscribe@struts.apache.org
> For additional commands, e-mail: dev-help@struts.apache.org
>
>


-- 
"Hey you! Would you help me to carry the stone?" Pink Floyd

Re: Enum type conversion

Posted by Jeromy Evans <je...@blueskyminds.com.au>.
Bob Tiernay wrote:
> I'm not 100% sure, but I though Musachy enabled this by default?

Ah, yes, Don did for 2.1.0, https://issues.apache.org/struts/browse/WW-2162

But it's not back-ported to 2.0.x


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@struts.apache.org
For additional commands, e-mail: dev-help@struts.apache.org


Re: Enum type conversion

Posted by Bob Tiernay <bt...@hotmail.com>.
I'm not 100% sure, but I though Musachy enabled this by default?

--------------------------------------------------
From: "Jeromy Evans" <je...@blueskyminds.com.au>
Sent: Thursday, September 18, 2008 5:51 AM
To: "Struts Developers List" <de...@struts.apache.org>
Subject: Re: Enum type conversion

> Piotr Matkowski wrote:
>> Maybe Enums should be handled by struts by default? Why they aren't 
>> handled?
>>
>
> Enum conversion broken/unsupported in OGNL's default type converter.  I 
> can't recall where.  I spent hours hunting it down and put it in the too 
> hard basket...
> However, if you enable XWork's default Enum convert it works just fine 
> without further configuration
>
> ie. com.opensymphony.xwork2.util.EnumTypeConverter
>
> You can enable that converter in the default converters file, or enable it 
> for individual properties.
>
> I'm not sure why it's not enabled by default.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscribe@struts.apache.org
> For additional commands, e-mail: dev-help@struts.apache.org
>
> 

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@struts.apache.org
For additional commands, e-mail: dev-help@struts.apache.org


Re: Enum type conversion

Posted by Jeromy Evans <je...@blueskyminds.com.au>.
Piotr Matkowski wrote:
> Maybe Enums should be handled by struts by default? Why they aren't handled?
>   

Enum conversion broken/unsupported in OGNL's default type converter.  I 
can't recall where.  I spent hours hunting it down and put it in the too 
hard basket...
However, if you enable XWork's default Enum convert it works just fine 
without further configuration

ie. com.opensymphony.xwork2.util.EnumTypeConverter

You can enable that converter in the default converters file, or enable 
it for individual properties.

I'm not sure why it's not enabled by default.

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@struts.apache.org
For additional commands, e-mail: dev-help@struts.apache.org