Re: example of PlugIn for java.sql.Date Converter registration

2003-03-29 Thread Jeff Kyser
Hi all,

as a follow-up to my question, wanted to thank Dan Tran for his help
and also Rick Reumann for his.
I was trying to add a PlugIn in the struts-config.xml file to augment
the functionality of the BeanUtils.copyProperties function to handle
java.util.Date going from a form to a model bean.
If this was obvious to others, please excuse the post, but it sure
wasn't obvious to me. I ended up reviewing the sources for the
validator and tiles plugins to get this to work.
Here's a snippet from my struts-config.xml file; in addition to loading
the custom converters, I wanted to be able to supply a value to be
used by the SimpleDateFormat, hence my dateFormat property below.
!-- A PlugIn to extend BeanUtils.copyProperties to handle 
java.sql.Date --
plug-in className=torch.beans.ConvertUtils.ConverterPlugIn
set-property property=dateFormat value=-MM-dd/
/plug-in

And here's (below) the init() method I ended up with as a part of 
implementing
the PlugIn interface. The  basics came from Dan, and finding the
proper PlugIn to retrieve my dateFormat property was gleaned
from the sources.

Well, it works, but as to whether there is a cleaner/shorter way to
do this, I don't know, but would be glad to hear it.
regards,

-jeff

public final class ConverterPlugIn implements 
org.apache.struts.action.PlugIn {
/**
 * init - implements the PlugIn interface
 */
public void init(ActionServlet servlet, ModuleConfig config)
throws ServletException {

PlugInConfig[] plugs = config.findPlugInConfigs();
boolean found = false;
int i = 0;
while (i  plugs.length  !found) {
if 
(this.getClass().getName().equals(plugs[i].getClassName()))
found = true;
else
i++;
}
if (found) {
Object fmt = plugs[i].getProperties().get(dateFormat);
if (fmt != null)
setDateFormat(fmt.toString());
}

ConvertUtils.register(new DateConverter(),Date.class);
ConvertUtils.register(new StringConverter(),  String.class);
}
   public static void setDateFormat(String fmt) {
fmt_ = fmt;
   }
I had a default format, in case there wasn't one in the config file,
and in the DateConverter, I retrieved the date format...
On Thursday, March 27, 2003, at 08:40  PM, Jeff Kyser wrote:

Hi,

I want to use BeanUtils.copyProperties to copy FROM my model TO my 
form,
and I have java.sql.Dates in my model.

From numerous related posts, I glean that the best place for a
a custom Converter to be registered is in a PlugIn in your 
struts-config.xml file.

1. Does anyone have an example of this they could offer up? i think 
I've
broken google...

2. Do you really have to do this to get a java.sql.Date from a model 
bean onto
a form as a String using BeanUtils? or is there a better way?

thanks,

-jeff



-
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]


Re: example of PlugIn for java.sql.Date Converter registration

2003-03-29 Thread Dan Tran
Jeff, your code suggests a better way to make date format configurable.
Thanks

-Dan

- Original Message -
From: Jeff Kyser [EMAIL PROTECTED]
To: Struts Users Mailing List [EMAIL PROTECTED]
Sent: Saturday, March 29, 2003 4:45 PM
Subject: Re: example of PlugIn for java.sql.Date Converter registration


 Hi all,

 as a follow-up to my question, wanted to thank Dan Tran for his help
 and also Rick Reumann for his.

 I was trying to add a PlugIn in the struts-config.xml file to augment
 the functionality of the BeanUtils.copyProperties function to handle
 java.util.Date going from a form to a model bean.

 If this was obvious to others, please excuse the post, but it sure
 wasn't obvious to me. I ended up reviewing the sources for the
 validator and tiles plugins to get this to work.

 Here's a snippet from my struts-config.xml file; in addition to loading
 the custom converters, I wanted to be able to supply a value to be
 used by the SimpleDateFormat, hence my dateFormat property below.

  !-- A PlugIn to extend BeanUtils.copyProperties to handle
 java.sql.Date --
  plug-in className=torch.beans.ConvertUtils.ConverterPlugIn
  set-property property=dateFormat value=-MM-dd/
  /plug-in

 And here's (below) the init() method I ended up with as a part of
 implementing
 the PlugIn interface. The  basics came from Dan, and finding the
 proper PlugIn to retrieve my dateFormat property was gleaned
 from the sources.

 Well, it works, but as to whether there is a cleaner/shorter way to
 do this, I don't know, but would be glad to hear it.

 regards,

 -jeff

 public final class ConverterPlugIn implements
 org.apache.struts.action.PlugIn {
  /**
   * init - implements the PlugIn interface
   */
  public void init(ActionServlet servlet, ModuleConfig config)
  throws ServletException {

  PlugInConfig[] plugs = config.findPlugInConfigs();
  boolean found = false;
  int i = 0;
  while (i  plugs.length  !found) {
  if
 (this.getClass().getName().equals(plugs[i].getClassName()))
  found = true;
  else
  i++;
  }
  if (found) {
  Object fmt = plugs[i].getProperties().get(dateFormat);
  if (fmt != null)
  setDateFormat(fmt.toString());
  }

  ConvertUtils.register(new DateConverter(),Date.class);
  ConvertUtils.register(new StringConverter(),  String.class);
  }

 public static void setDateFormat(String fmt) {
 fmt_ = fmt;
 }

 I had a default format, in case there wasn't one in the config file,
 and in the DateConverter, I retrieved the date format...

 On Thursday, March 27, 2003, at 08:40  PM, Jeff Kyser wrote:

  Hi,
 
  I want to use BeanUtils.copyProperties to copy FROM my model TO my
  form,
  and I have java.sql.Dates in my model.
 
  From numerous related posts, I glean that the best place for a
  a custom Converter to be registered is in a PlugIn in your
  struts-config.xml file.
 
  1. Does anyone have an example of this they could offer up? i think
  I've
  broken google...
 
  2. Do you really have to do this to get a java.sql.Date from a model
  bean onto
  a form as a String using BeanUtils? or is there a better way?
 
  thanks,
 
  -jeff
 
 
 
 
  -
  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]



-
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



example of PlugIn for java.sql.Date Converter registration?

2003-03-27 Thread Jeff Kyser
Hi,

I want to use BeanUtils.copyProperties to copy FROM my model TO my form,
and I have java.sql.Dates in my model.
From numerous related posts, I glean that the best place for a
a custom Converter to be registered is in a PlugIn in your 
struts-config.xml file.

1. Does anyone have an example of this they could offer up? i think I've
broken google...
2. Do you really have to do this to get a java.sql.Date from a model 
bean onto
a form as a String using BeanUtils? or is there a better way?

thanks,

-jeff



-
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]