> -----Original Message----- > From: Leandro Melo [mailto:[EMAIL PROTECTED] > Sent: Wednesday, September 08, 2004 4:28 PM > To: Struts Users Mailing List > Subject: Re: ActionForm data type X BeanUtils -> using Dates > > > I got one doubt in your code when used in a situation > that i mentioned (transforming data from action form > to dto). > When date comes from action form, they usually come in > Strings, but in your method you verify > > value instanceof Date...
Bi-directional conversion. Look at what he does if it is a date. This isn't just for ActionForm to DTO, it's for any other time you have to convert a string to a date using BeanUtils. > > This will never happen, as the value comes in String. > > Am i saying something stupid??? Not really... just not being generic enough for the solution. > > I'm not saying your code is wrong, but not appropriate > for the situation in question. > > Am i right? Sorta :) > > > --- Bryce Fischer <[EMAIL PROTECTED]> > escreveu: > > The custom converter is done independently of the > > action, form or DTO. > > > > First step is to implement the interface > > org.apache.commons.beanutils.Converter. > > > > Keep in mind that the converter you are writing uses > > the appropriate > > converter for the original class. So, if you were > > converting from a > > String to a Date, you would create a String > > converter. And vice versa, > > if you were converting from a Date to a String you > > would create a Date > > converter. Example below > > > > Then, to override the standard converters, you call: > > ConvertUtils.register(<Your converter class>, <class > > to use converter > > on>.class); > > > > First parameter is your new converter, and the > > second is the class its > > used for. I call this in a static initializer of a > > utility class, you > > just need to make sure its called before any > > validations occur, and it > > only needs to be done once. > > > > Since we are interested in customizing conversions > > from String to Date, > > I override the String converter: > > > > public class StringConverter implements Converter{ > > public Object convert(Class type, Object value) { > > Object returnObj = null; > > if (value != null) { > > if (value instanceof Date){ > > returnObj = > > dateFormatter.format((Date)value); > > } else if (value instanceof BigDecimal){ > > returnObj = > > decimalFormatter.format((BigDecimal)value); > > } else { > > returnObj = value.toString(); > > } > > } > > > > return returnObj; > > } > > } > > > > I've left out some properties, their getters and > > setters, but I think > > you get the gist of what I'm doing. Now, I register > > this converter as such: > > > > ConvertUtils.register(new StringConverter(), > > String.class); > > > > I actually have a utility class, where the register > > is done in the > > static initializer. I suppose another way would be > > to use a servlet that > > loads prior to Struts.. Or if your Actions have a > > base class, etc... > > > > Maybe there's a more elegant way of doing that. This > > is what I've come > > up with. If someone has a better way, post please, > > I'm always looking > > for more elegant ways of accomplishing stuff like > > this. > > > > Jason King wrote: > > > > > Could you point us at some code that does this? > > Do you customize in > > > the action, the form or the DTO? > > > > > > > --------------------------------------------------------------------- > > To unsubscribe, e-mail: > > [EMAIL PROTECTED] > > For additional commands, e-mail: > > [EMAIL PROTECTED] > > > > > > > > > > _______________________________________________________ > Yahoo! Acesso Grátis - navegue de graça com conexão de qualidade! > http://br.acesso.yahoo.com/ > > --------------------------------------------------------------------- > To unsubscribe, e-mail: [EMAIL PROTECTED] > For additional commands, e-mail: [EMAIL PROTECTED] > > --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]