You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Caroline Jen <ji...@yahoo.com> on 2006/06/18 04:47:46 UTC
BeanUtils, Form Bean and POJO Bean
The Java "types" of the properties in my form bean
sometimes are not the same as those of the data fields
in my database.
The request.getParameter(); always return a String. I
can use the copyProperties() method of the BeanUtils
to convert all the String(s) read from the web page to
proper Java "types" in my form bean.
What do I do when the property type in my form bean
does not match the data field type in my POJO bean?
__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org
Re: BeanUtils, Form Bean and POJO Bean
Posted by Scott Van Wart <sc...@indosoft.ca>.
Caroline Jen wrote:
> The Java "types" of the properties in my form bean
> sometimes are not the same as those of the data fields
> in my database.
>
> The request.getParameter(); always return a String. I
> can use the copyProperties() method of the BeanUtils
> to convert all the String(s) read from the web page to
> proper Java "types" in my form bean.
>
> What do I do when the property type in my form bean
> does not match the data field type in my POJO bean?
>
You need to convert it manually... what if you have a foreign key
relationship from your database? How is Struts supposed to know about
that? What if it's a many-to-many relationship? Should Struts give you
a list of IDs, or a list of foreign entities with all properties? You
need to code this kind of relationship in the way you set up your form
and process a form submission. You need to figure out a way to do the
database entity->form data (key(s)=value) and form data submission
(key(s)=value)->database entity. Your primary candidates of a form
submission are going to be a String or a String[]. The reason why
Struts doesn't give you java.lang.Long, or any other class is because
you can't confidently trust the values you get from a form submission
(clients can submit whatever they want, due to the disconnected nature
of the net). From the look of your question, you might want to have a
look at request.getParameterValues(), which returns a String[] (for
multi-select lists, multiple checkboxes, etc.).
HTH
- Scott
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org
Re: BeanUtils, Form Bean and POJO Bean
Posted by Paul Benedict <pa...@yahoo.com>.
And to Rick's note, I should mention I never use copyProperties.. I "can" if I was insane enough :o)
Rick Reumann <ri...@gmail.com> wrote: Paul Benedict wrote:
> I can use the copyProperties() method of the BeanUtils
> to convert all the String(s) read from the web page to
> proper Java "types" in my form bean.
Just be weary though, that BeanUtils can really by a major PAIN when you
have domain objects that have number wrapper data types (Integer,
Double, etc). BeanUtils has the VERY annoying behavior of converting
null Strings to 0 for those number types. I end up always have to make
Converters to deal with this and I really think BeanUtils should have a
release that makes this NOT the default behavior. (Possibly provide some
kind of extra param if you really want null to become 0).
Here's a test to demonstrate...
public class Test() {
public static void main(String[] args) {
Test test = new Test();
test.doTest();
}
private void doTest() {
FormBean form = new FormBean();
DomainBean bean = new DomainBean();
try {
BeanUtils.copyProperties(bean, form);
System.out.println("The string value of integer: "+
bean.getNumber() ); //0 !!!
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
public class FormBean {
private String number;
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
}
public class DomainBean {
private Integer number;
public Integer getNumber() {
return number;
}
public void setNumber(Integer number) {
this.number = number;
}
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org
---------------------------------
New Yahoo! Messenger with Voice. Call regular phones from your PC and save big.
Re: BeanUtils, Form Bean and POJO Bean
Posted by Rick Reumann <ri...@gmail.com>.
Paul Benedict wrote:
> I can use the copyProperties() method of the BeanUtils
> to convert all the String(s) read from the web page to
> proper Java "types" in my form bean.
Just be weary though, that BeanUtils can really by a major PAIN when you
have domain objects that have number wrapper data types (Integer,
Double, etc). BeanUtils has the VERY annoying behavior of converting
null Strings to 0 for those number types. I end up always have to make
Converters to deal with this and I really think BeanUtils should have a
release that makes this NOT the default behavior. (Possibly provide some
kind of extra param if you really want null to become 0).
Here's a test to demonstrate...
public class Test() {
public static void main(String[] args) {
Test test = new Test();
test.doTest();
}
private void doTest() {
FormBean form = new FormBean();
DomainBean bean = new DomainBean();
try {
BeanUtils.copyProperties(bean, form);
System.out.println("The string value of integer: "+
bean.getNumber() ); //0 !!!
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
public class FormBean {
private String number;
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
}
public class DomainBean {
private Integer number;
public Integer getNumber() {
return number;
}
public void setNumber(Integer number) {
this.number = number;
}
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org
Re: BeanUtils, Form Bean and POJO Bean
Posted by Paul Benedict <pa...@yahoo.com>.
Jen, it is standard practice for an ActionForm to contain only string properties. The reason is if validation fails, you want to return the text as-is as it was inputted. Once you have successfuly validated the ActionForm, then copy your form properties into your model object.
Caroline Jen <ji...@yahoo.com> wrote: The Java "types" of the properties in my form bean
sometimes are not the same as those of the data fields
in my database.
The request.getParameter(); always return a String. I
can use the copyProperties() method of the BeanUtils
to convert all the String(s) read from the web page to
proper Java "types" in my form bean.
What do I do when the property type in my form bean
does not match the data field type in my POJO bean?
__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org
---------------------------------
Yahoo! Messenger with Voice. Make PC-to-Phone Calls to the US (and 30+ countries) for 2ยข/min or less.
RE: BeanUtils, Form Bean and POJO Bean
Posted by Joe Hertz <jh...@speakeasy.net>.
For starters, you should generally be using String properties in your form
bean because this is what the JSP sends to you. If you declare an integer in
your form bean, and the user enters character data into the text field,
youre going to get an exception thrown before you (or the validator) ever
gets to see it.
When you call BeanUtils.copyProperties(destObject, srcObject), it will do
the type conversions for you. If the field names are the same, it will
typically handle it.
If youre using your own type that BeanUtils doesn't understand out of the
box, you can register a class that implements the Converter interface. This
tells copyProperties() how to convert it from a String. I do this quite a
bit with enumerations.
> -----Original Message-----
> From: Caroline Jen [mailto:jiapei_jen@yahoo.com]
> Sent: Saturday, June 17, 2006 10:48 PM
> To: user@struts.apache.org
> Subject: BeanUtils, Form Bean and POJO Bean
>
> The Java "types" of the properties in my form bean sometimes
> are not the same as those of the data fields in my database.
>
> The request.getParameter(); always return a String. I can
> use the copyProperties() method of the BeanUtils to convert
> all the String(s) read from the web page to proper Java
> "types" in my form bean.
>
> What do I do when the property type in my form bean does not
> match the data field type in my POJO bean?
>
> __________________________________________________
> Do You Yahoo!?
> Tired of spam? Yahoo! Mail has the best spam protection
> around http://mail.yahoo.com
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org