You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Jesse Vitrone <je...@vitalworks.com> on 2003/04/14 17:18:38 UTC
Objects in the form
I just started looking at putting an object in the form, then in the jsp
use:
<html:text property="object.field"/>
The only problem I'm running into is that if the object is null, then Struts
can't set it when I submit.
That's not a big deal for the object, because I can just init it in the
form, but for objects on objects, that means I have to init them all on my
beans, which I don't want to do since we check to see if things are null to
see if they've been set before.
Has anyone else run into this problem? Why doesn't Struts / Bean Commons
just check for null and create a new instance if it's null? Are there
issues with doing that?
Thanks in advance,
Jesse
Re: Objects in the form
Posted by Florian Convers <fc...@webnet.fr>.
Maybe because the bean property
which itself contain a bean is private member
And action servlet just have access to the getter
But you could maybe implement instance creation in your getter
----- Original Message -----
From: "Jesse Vitrone" <je...@vitalworks.com>
To: <st...@jakarta.apache.org>
Sent: Monday, April 14, 2003 5:18 PM
Subject: Objects in the form
I just started looking at putting an object in the form, then in the jsp
use:
<html:text property="object.field"/>
The only problem I'm running into is that if the object is null, then Struts
can't set it when I submit.
That's not a big deal for the object, because I can just init it in the
form, but for objects on objects, that means I have to init them all on my
beans, which I don't want to do since we check to see if things are null to
see if they've been set before.
Has anyone else run into this problem? Why doesn't Struts / Bean Commons
just check for null and create a new instance if it's null? Are there
issues with doing that?
Thanks in advance,
Jesse
---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org
RE: Objects in the form
Posted by Jesse Vitrone <je...@vitalworks.com>.
This is what I meant - if I add this code:
if (value == null) {
try {
Method writeMethod = getWriteMethod(descriptor);
if (writeMethod == null) {
throw new NoSuchMethodException("Property '" + name +
"' has no setter method");
}
value = readMethod.getReturnType().newInstance();
writeMethod.invoke(bean, new Object[] {value});
} catch (Exception e) {
e.printStackTrace();
}
}
}
To the bottom of commons org.apache.commons.beanutils.PropertyUtils.java,
everything works like I want it to. If the bean on my form is not
initialized, it creates me a new one, and sets in on the form.
Would other people find something like this useful? That way, if I have a
Person who's info is being submitted, I can just have a Person on my form,
and property="person.firstName" in my htm:form, and all my Person info is
set for me, and then back in my Action class, I have my Person object to
save.
Granted, if I init my Person in my form, it'd work with no code change, but
that means all my objects everywhere would need to be initialized. That's a
lot of object creation when we don't need it. Keep in mind, I don't just
mean the objects on the forms, IE - all the objects on my Person would need
to be initialized too, or else Struts / Commons will get a null when it
tries to set those. So I'm trying to be able to do this w/o having to init
all those objects.
Would anyone else find this useful? Do you think there should be a flag or
something we can set somewhere that tells Struts / Commons to do this?
Jesse
-----Original Message-----
From: Jesse Vitrone [mailto:jessevitrone@vitalworks.com]
Sent: Monday, April 14, 2003 11:19 AM
To: struts-user@jakarta.apache.org
Subject: Objects in the form
I just started looking at putting an object in the form, then in the jsp
use:
<html:text property="object.field"/>
The only problem I'm running into is that if the object is null, then Struts
can't set it when I submit.
That's not a big deal for the object, because I can just init it in the
form, but for objects on objects, that means I have to init them all on my
beans, which I don't want to do since we check to see if things are null to
see if they've been set before.
Has anyone else run into this problem? Why doesn't Struts / Bean Commons
just check for null and create a new instance if it's null? Are there
issues with doing that?
Thanks in advance,
Jesse
---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org