struts-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Jukka Välimaa" <valimaa.ju...@gmail.com>
Subject Re: S2 type conversion problem with generics
Date Wed, 20 Aug 2008 19:06:08 GMT
A look into documentation<http://struts.apache.org/2.0.11.2/docs/type-conversion.html#TypeConversion-GenericsandErasure>solved
this. Apparently Struts 2 only supports generics for Collections or
arrays.
On Wed, Aug 20, 2008 at 9:32 PM, Chris Pratt <thechrispratt@gmail.com>wrote:

> Are you sure they do?  Or is it just recognizing that it's a Collection and
> treating it appropriately for a Collection of Objects?
>  (*Chris*)
>
> On Wed, Aug 20, 2008 at 10:16 AM, Jukka Välimaa <valimaa.jukka@gmail.com
> >wrote:
>
> > Sounds reasonable, thanks for the answer. But why do the generics used in
> > java collections work with type conversion, and generics in my custom
> class
> > won't?
> >
> > On Wed, Aug 20, 2008 at 7:06 PM, Chris Pratt <thechrispratt@gmail.com
> > >wrote:
> >
> > > My bet is that, because Struts uses run-time introspection to determine
> > how
> > > to pass the parameters, and the new Java Generics use type erasure to
> > > ensure
> > > backward portability, what Struts see's isn't your generic type, it's
> an
> > > Object.  So it passes in the unconverted value that it got from
> > > Request.getParameters(), which is a String Array.
> > >  (*Chris*)
> > >
> > > On Wed, Aug 20, 2008 at 8:17 AM, Jukka Välimaa <
> valimaa.jukka@gmail.com
> > > >wrote:
> > >
> > > > Hi all,
> > > >
> > > > I have the following problem which I think is caused by Struts 2 type
> > > > conversion.
> > > >
> > > > I have a class I use to wrap a map for easier interface with view:
> > > >
> > > > public class ObjectWeekHandler<T> {
> > > >    private Map<WeekDayEntry, T> weekDayObjects = new
> > > HashMap<WeekDayEntry,
> > > > T>();
> > > >
> > > >    public Map<WeekDayEntry, T> getWeekDayObjects() {
> > > >        return weekDayObjects;
> > > >    }
> > > >
> > > >    public void setWeekDayObjects(Map<WeekDayEntry, T> weekDayObjects)
> {
> > > >        this.weekDayObjects = weekDayObjects;
> > > >    }
> > > >
> > > >    public T getEvenMonObject() {
> > > >        return weekDayObjects.get(WeekDayEntry.EVEN_MONDAY);
> > > >    }
> > > >
> > > >    public void setEvenMonObject(T evenMonObject) {
> > > >        weekDayObjects.put(WeekDayEntry.EVEN_MONDAY, evenMonObject);
> > > >    }
> > > >
> > > >    [...]
> > > >
> > > > I use this class in my action class to get strings for various
> > weekdays:
> > > > private ObjectWeekHandler<String> weekDayTruckIds = new
> > > > ObjectWeekHandler<String>();
> > > >
> > > > public ObjectWeekHandler<String> getWeekDayIds() {
> > > >        return weekDayIds;
> > > >  }
> > > >
> > > > When I send the request, the parameters are of this type:
> > > >    weekDayIds.evenMonObject=-1
> > > >
> > > > If I understand the workings of Struts 2 type conversion correctly,
> it
> > > > should call getWeekDayIds.setEvenMonObject(type converted parameter
> > > value).
> > > > This is indeed what happens, but instead of setting the parameter as
> > > > String,
> > > > it is set as String array. The same thing happens if I parameterize
> > > > weekDayIds as weekDayIds<Long>.
> > > >
> > > > I have an ugly workaround in place- I cast the supposed String value
> I
> > > get
> > > > from getEvenMonObject into Object and then into String[], and get the
> > > first
> > > > value. It works, but I certainly don't like it. Does anyone have an
> > idea
> > > > what might be wrong and how to fix it?
> > > >
> > >
> >
>

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