Hi All, Well, after much trial and error, I've worked out how to do POJO-level TypeConverters. the Struts Type Converter page (see http://struts.apache.org/2.x/docs/type-conversion.html) unfortunately leaves out some key details that make it quite difficult.
1. create the Type Converter class and as described on the Struts site (see above) and as I outlined below. The Struts website says in one place that you should extend org.apache.struts2.util.StrutsTypeConverter and in another that you should "implement the TypeConverter interface". I extended StrutsTypeConverter and that worked fine. 2. Create your XXX-conversion.properties file. Its name needs to be the same as your POJO class name (not fully-qualified). So, for my example below, I called it Shift-conversion.properties. The key thing to note is that this file must end up in the same directory as your POJO's .class file. So, in Appfuse, put it under your src/main/resources directory, in a path that matches your class package. My Shift-conversion.properties file ended up in src/main/resources/au/com/myapp/model. 3. Inside your XXX-conversion.properties file, define each attribute (or field, whatever you want to call it) that you want handled by your converter. The definitions are specified as follows, one per line: myAttributeName = fully.qualified.typeconverter.name so, to continue my example below, I had two fields in my Shift class that I wanted to be handled by my TimeTypeConverter class, "startTime" and "endTime". I defined them like this: startTime=au.com.myapp.webapp.action.converters.TimeTypeConverter endTime=au.com.myapp.webapp.action.converters.TimeTypeConverter And that's all there is to it! On 24 Oct 2007 at 23:13, Rob Hills wrote: > Hi All, > > I'm trying to implement a model.attribute-level TypeConverter but I can't get > it to work. I want to convert between "time" on a form > (hh:mm) and a Date class in my model. > > I'm using AppFuse 2.0 + Hibernate + Struts2 > > I have an "au.com.myapp.model.Shift" class that contains two Date attributes > "startTime" and "endTime". Before I started creating > a converter, I had a data entry form that displayed the startTime and endTime > values as dates in text fields, as you'd expect. If I > put time values in those fields (hh:mm) I get type conversion errors from the > default date type converter, again as expected. > > I've followed the Struts documentation here: > > http://struts.apache.org/2.x/docs/type-conversion.html > > and created: > > - au.com.myapp.webapp.action.converters.BaseTypeConverter.java > (Abstract type, extends org.apache.struts2.util.StrutsTypeConverter and > provides base functionality like logging etc.) > > - au.com.myapp.webapp.action.converters.TimeTypeConverter.java > (extends au.com.myapp.webapp.action.converters.BaseTypeConverter.java and > implements > convertFromString and convertToString methods (which convert between the > Date object and the time string > representation. > - Shift-conversion.properties (in my src/main/resources directory) which > contains the following two lines: > startTime=au.com.myapp.webapp.action.converters.TimeTypeConverter > endTime=au.com.myapp.webapp.action.converters.TimeTypeConverter > > >From my reading, that should be all I need to do. However, when I build and > >run my app with this setup, the form's behaviour > doesn't change. Further, I've put logging into the converter class (both the > constructor and the methods) and it appears it's never > called. > > Is there some other undocumented thing I need to do, or have I misunderstood > the documentation? This took me many hours of trial-and-error, so I hope this post will save someone else the same heartache one day. Cheers, Rob Hills Waikiki, Western Australia Mobile +61 (412) 904-357 Fax: +61 (8) 9529-2137 --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
