You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Rene Eigenheer <re...@bridgesolutions.net> on 2003/01/07 22:19:00 UTC

ConvertUtils, BeanUtils

Craig,

I use the opportunity to jump into this and to ask you and everybody here
for an advice:

I try to follow the good practice to have only Strings within the
form-classes and having value objects with the correct types.

In the past I coded the copy from vo to form and vice-versa with the typical
setter-getter for every field - easy but sometimes painfull.

Recently I've started using BeanUtils to copy the properties, I even ;-)))
managed it to register my own converter for GregorianCalendar properties.

Now my two questions/problems:

question 1:
where do i need to register the converter in a struts web-application? once
in a plugin? within the the controller? within every action?

problem/question 2:
when i have the typical situation of vaqlue object "invoice"  having an
ArrayList of value objects "invoice position" and having a form "invoice"
with an ArrayList of "subforms" "invoice position", then the copyProperties
works fine for the vo-invoice to form-invoice but it copes the
vo-invoice-positions to the ArrayList of the form-invoice.
is there a solution for this problem?
is there a possibility to ask the converter not to convert collections?
what is a good practice to manage this?


-Rene



> -----Original Message-----
> From: Craig R. McClanahan [mailto:craigmcc@apache.org]
> Sent: Dienstag, 7. Januar 2003 21:28
> To: Struts Users Mailing List
> Subject: Re: ConvertUtils, Null Integers
>
>
>
>
> On Tue, 7 Jan 2003, Jim Krygowski wrote:
>
> > Date: Tue, 07 Jan 2003 09:00:04 -0500
> > From: Jim Krygowski <ja...@shaws.com>
> > Reply-To: Struts Users Mailing List <st...@jakarta.apache.org>
> > To: Struts Users Mailing List <st...@jakarta.apache.org>
> > Subject: ConvertUtils, Null Integers
> >
> > Hi-
> >
> > This might be a question to Craig specifically, but I'd
> appreciate hearing
> > from anyone with experience.  I noticed that the
> ConvertUtils instructs
> > IntegerConverter to create Integer objects with the default
> value of 0 when
> > it can't convert what it receives into a proper Integer.
> The application
> > I'm building with Struts has several forms that must
> support "null" values.
> > That is, fields in which no entries are made.  The
> underlying database
> > fields are nullable and zero is a significant and
> meaningfull number.
> >
> > Using the Struts infrastructure as is, form fields
> containing nothing are
> > converted to zero by ConvertUtils/IntegerConverter thus
> messing up my data
> > somewhat.
> >
> > Is there any way to get the Converter to set the Integer to
> null when the
> > value recieved from the HTML form isn't a valid integer?
> I'm aware of the
> > approach of creating my own custom Integer converter and
> loading it in a
> > static intializer but is there a better way?  Preferably
> some way that's
> > configurable via an xml file?
> >
>
> You can make Struts act like this by registering your own
> Converter, but
> you are ***much*** better off by keeping the property as a
> String in your
> form bean.  No only can you then distinguish between a
> zero-length String
> and a "0", it also doesn't screw things up when the user types garbage
> like "1a3" instead of "123".
>
> > thanks.
>
> Craig
>
>
>
>
> --
> To unsubscribe, e-mail:
> <ma...@jakarta.apache.org>
> For additional commands, e-mail:
> <ma...@jakarta.apache.org>
>
>


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: anyone use LocaleBeanUtils?

Posted by Franco Caponi <fr...@tin.it>.
Hi david, I have encountered the same problem and i'm finding for the best
solution.

My solution is to register (with the ConvertUtils.register() method) two new
default converter used by the entire struts framework (for the populate
method for example):
One for the class java.sql.Date, that occur form String to java.sql.Date and
another for the class String that occur from java.sql.Date to String

Sample code may be
ConvertUtils.register ( new SqlDateLocaleConverter( Locale.ITALY,
'MM/dd/yyyy') , java.sql.Date.class);
ConvertUtils.register ( new StringLocaleConverter( Locale.ITALY,
'MM/dd/yyyy') , String.class);


This istruction is global to entire application, therefore i think that must
placed inside a Plugin.

I don't know if it is a correct solution... i'm currently developing it...




----- Original Message -----
From: "david chan" <da...@yahoo.com>
To: "Struts Users Mailing List" <st...@jakarta.apache.org>
Sent: Thursday, January 09, 2003 6:42 PM
Subject: anyone use LocaleBeanUtils?


> Hi,
>  I posted this questions two times but no body seems
> using LocaleBeanUtils in action class. Hope this time
> someone can give me a feedback.
>
> I am looking for LocaleBeanUtils.copyproperties
> example. I have a valueobject which contain some sql
> date memeber and a formobject with corresponding
> string memeber. I want to copy the valueobject to
> formobject and convert the date object to a string
> "MM/dd/yyyy" when copying the bean. So I try to use
> LocaleBeanUtils.copyproperties() method but the
> conversion result use a default date string
> "yyyy-mm-dd".
> 1) Is it a right direction to use LocaleBeanUtils for
> this problem?
> 2) If so, how can I set the date parttern when copying
> bean? (I am using struts 1.1b3)
>
> Thanks.
> David
>
>
>
> __________________________________________________
> Do you Yahoo!?
> Yahoo! Mail Plus - Powerful. Affordable. Sign up now.
> http://mailplus.yahoo.com
>
> --
> To unsubscribe, e-mail:
<ma...@jakarta.apache.org>
> For additional commands, e-mail:
<ma...@jakarta.apache.org>
>


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


anyone use LocaleBeanUtils?

Posted by david chan <da...@yahoo.com>.
Hi,
 I posted this questions two times but no body seems
using LocaleBeanUtils in action class. Hope this time
someone can give me a feedback.

I am looking for LocaleBeanUtils.copyproperties
example. I have a valueobject which contain some sql
date memeber and a formobject with corresponding
string memeber. I want to copy the valueobject to
formobject and convert the date object to a string
"MM/dd/yyyy" when copying the bean. So I try to use
LocaleBeanUtils.copyproperties() method but the
conversion result use a default date string
"yyyy-mm-dd".
1) Is it a right direction to use LocaleBeanUtils for
this problem?
2) If so, how can I set the date parttern when copying
bean? (I am using struts 1.1b3)

Thanks.
David 



__________________________________________________
Do you Yahoo!?
Yahoo! Mail Plus - Powerful. Affordable. Sign up now.
http://mailplus.yahoo.com

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


LocaleBeanUtils question repost

Posted by david chan <da...@yahoo.com>.
Hi,
I am looking for LocaleBeanUtils.copyproperties
example. I have a valueobject which contain some sql
date memeber and a formobject with corresponding
string memeber. I want to copy the valueobject to
formobject and convert the date object to a string
"MM/dd/yyyy" when copying the bean. So I try to use
LocaleBeanUtils.copyproperties() method but the
conversion result use a default date string
"yyyy-mm-dd".
1) Is it a right direction to use LocaleBeanUtils for
this problem?
2) If so, how can I set the date parttern when copying
bean? (I am using common-beanutils 1.5 and struts
1.1b3)

Thanks.
David 



__________________________________________________
Do you Yahoo!?
Yahoo! Mail Plus - Powerful. Affordable. Sign up now.
http://mailplus.yahoo.com

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


RE: ConvertUtils, BeanUtils

Posted by Rene Eigenheer <re...@bridgesolutions.net>.
> > problem/question 2:
> > when i have the typical situation of vaqlue object
> "invoice"  having an
> > ArrayList of value objects "invoice position" and having a
> form "invoice"
> > with an ArrayList of "subforms" "invoice position", then
> the copyProperties
> > works fine for the vo-invoice to form-invoice but it copes the
> > vo-invoice-positions to the ArrayList of the form-invoice.
>
> I'm afraid that I don't understand what you are saying here.
> Could you
> illustrate the problem wish some simple examples from your VO and form
> bean declarations?

value object:
public class ConsignmentVO {
	private double totalGrossWeight;
	private double totalVolume;
	private ArrayList lineItems;  // collection of LineItemVO

public class LineItemVO {
	private String marksAndNumbers;
	private String goodsDescription;
	private double grossWeight;
	private double volume;

form object:
public class ConsignmentSeaDtlForm  extends ValidatorForm {
	private String totalWeight;
	private String totalVolume;
	private ArrayList lineItems;  // collection of LineItemForm

public class LineItemForm {
	private String marksAndNumbers;
	private String goodsDescription;
	private String grossWeight;
	private String volume;

I've tried
BeanUtils.copyProperties(ConsignmentSeaDtlForm, ConsignmentVO);

which worked fine for the numeric-to-string conversion, but copied
LineItemVO to lineItems of ConsignmentSeaDtlForm.

I read another post from you within the commons list, refering to deep copy,
but I didn't found a solution for my problem. Do I missunderstand something?

Thanks a lot for your assistance
Rene


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: ConvertUtils, BeanUtils

Posted by "Craig R. McClanahan" <cr...@apache.org>.

On Tue, 7 Jan 2003, Rene Eigenheer wrote:

> Date: Tue, 7 Jan 2003 22:19:00 +0100
> From: Rene Eigenheer <re...@bridgesolutions.net>
> Reply-To: Struts Users Mailing List <st...@jakarta.apache.org>
> To: 'Struts Users Mailing List' <st...@jakarta.apache.org>
> Subject: ConvertUtils, BeanUtils
>
> Craig,
>
> I use the opportunity to jump into this and to ask you and everybody here
> for an advice:
>
> I try to follow the good practice to have only Strings within the
> form-classes and having value objects with the correct types.
>
> In the past I coded the copy from vo to form and vice-versa with the typical
> setter-getter for every field - easy but sometimes painfull.
>
> Recently I've started using BeanUtils to copy the properties, I even ;-)))
> managed it to register my own converter for GregorianCalendar properties.
>

Smart move :-).

> Now my two questions/problems:
>
> question 1:
> where do i need to register the converter in a struts web-application? once
> in a plugin? within the the controller? within every action?
>

The registration is global to your entire commons-beanutils.jar file, so
I'd probably do this in a PlugIn.  It only needs to be done once.

> problem/question 2:
> when i have the typical situation of vaqlue object "invoice"  having an
> ArrayList of value objects "invoice position" and having a form "invoice"
> with an ArrayList of "subforms" "invoice position", then the copyProperties
> works fine for the vo-invoice to form-invoice but it copes the
> vo-invoice-positions to the ArrayList of the form-invoice.

I'm afraid that I don't understand what you are saying here.  Could you
illustrate the problem wish some simple examples from your VO and form
bean declarations?

> is there a solution for this problem?
> is there a possibility to ask the converter not to convert collections?
> what is a good practice to manage this?
>
>
> -Rene

Craig


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>