You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by florian <cs...@structbench.com> on 2003/05/18 21:42:16 UTC

value objects and forms

hi!


im passing value objects from struts actions down to ejbs.
i find it quite annoying to always get every value from the
formBean and set it to the appropiate value in the value
object..

so i was was wondering about this:

if i have a person value object and a actionform, both
describe exactly the same data.. wouldnt it be possible
to just define a person field in the actionform, and delegate
the getters and setters of the actionform to the getter and
setters of the person value object..

like this i could avoid endless setter calling in my
actions..

is that possible some how and a good practice? or impossible
and/or horrible bad? =)

thanks!

ciao!
florian


---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org


Re: value objects and forms

Posted by Adam Hardy <ah...@cyberspaceroad.com>.
One solution to this which I always use: I don't have any 
object-specific properties in the formbean. I just have a collection 
property for objects, and store a value bean or many value beans in 
this. I use nested tags in the jsp with a nested:iterate.

The validation code for the object properties are done by code in the 
value beans. Each value bean gets a reference to the form's errors 
collection. In the form validate method, I call the getProperty() method 
on each property of each bean to validate - they're the methods that 
have the validation code.

Might violate some architectural principle, but I haven't found out yet.


Mike Whittaker wrote:
> Seems you're having similar quandries to me.  See 'Getting Action Form
> data...'
> 
> If I understand you and Struts correctly (unlikely at this early for me
> stage!)
> You're form bean represents the user form and that alone, if you delegate to
> the business object then the form could be populated with this, does this
> make sense in your system?
> 
> What I've been investigating is duplicating these form beans dyamically (see
> commons.beanutils package) and populating them from the original servlet
> level parameter map with one of those beanutils methods.
> 
> Perhaps any replies to my post will help you too.
> 
> --
> Mike Whittaker


---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org


Re: value objects and forms

Posted by Adam Hardy <ah...@cyberspaceroad.com>.
you can put your value bean in your form bean in a property with 
getBean() and setBean(). You can then use the 'nested' tags to call the 
properties of your value bean in the jsps directly. Thus you don't 
require duplication of the properties in the form bean.


florian wrote:
> 
> On Montag, Mai 19, 2003, at 04:36  Uhr, Mohd Amin wrote:
> 
>> If I understood your question correctly, using BeanUtils.copyProperties
>> would do the trick just fine?
> 
> 
> hey armin!
> 
> good tip!
> 
> but i would still  have to define the fields in the value object and the
> form bean.  still some duplication going on : /
> 
> 
> ciao!
> florian
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: struts-user-help@jakarta.apache.org
> 
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org


RE: value objects and forms

Posted by Mike Whittaker <mi...@ntlworld.com>.
There are few tangents of this discussion floating around, mine has been
lost.

A client enters details into a form, and a form bean is created.

Sometimes the reverse happens and data prepopulates the form from a value
bean via a form bean.

But more frequent is the need to get a users input from the form bean into
the business logic.
This is what concerns me at present.

IMO the form bean should never appear in the business logic, above all else
it needs servlet API support!

So some way of copying this data is required, unless we revert back to the
requests ability to provide a parameter map.

An earlier attempt at global dynamic bean creation and from every form
delivered to all Actions was a bit messy (see earlier post).

How about creating a subclass of action form that has a method that copies
itself into a dynabean (created there and then) and returns that.  And then
subclassing this for all specific forms.

I understand what list backed beans are now Vic, and I see that you could
easily get a form bean to return this list.  But the above approach offers
total flexibility and makes the value bean transparent.

My terminology re value bean may not be quite right.

Thoughts?

--
Mike Wyttaker


---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org


RE: value objects and forms

Posted by Mike Whittaker <mi...@ntlworld.com>.
There are few tangents of this discussion floating around, mine has been
lost.

A client enters details into a form, and a form bean is created.

Sometimes the reverse happens and data prepopulates the form from a value
bean via a form bean.

But more frequent is the need to get a users input from the form bean into
the business logic.
This is what concerns me at present.

IMO the form bean should never appear in the business logic, above all else
it needs servlet API support!

So some way of copying this data is required, unless we revert back to the
requests ability to provide a parameter map.

An earlier attempt at global dynamic bean creation and from every form
delivered to all Actions was a bit messy (see earlier post).

How about creating a subclass of action form that has a method that copies
itself into a dynabean (created there and then) and returns that.  And then
subclassing this for all specific forms.

I understand what list backed beans are now Vic, and I see that you could
easily get a form bean to return this list.  But the above approach offers
total flexibility and makes the value bean transparent.

My terminology re value bean may not be quite right.

Thoughts?

--
Mike Whittaker


---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org


Re: value objects and forms

Posted by florian <cs...@structbench.com>.
On Montag, Mai 19, 2003, at 04:36  Uhr, Mohd Amin wrote:

> If I understood your question correctly, using BeanUtils.copyProperties
> would do the trick just fine?

hey armin!

good tip!

but i would still  have to define the fields in the value object and the
form bean.  still some duplication going on : /


ciao!
florian


---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org


RE: value objects and forms

Posted by Mohd Amin <mo...@xymphany.com>.
If I understood your question correctly, using BeanUtils.copyProperties
would do the trick just fine?

Amin

>
>> im passing value objects from struts actions down to ejbs.
>> i find it quite annoying to always get every value from the
>> formBean and set it to the appropiate value in the value
>> object..
>>
>> so i was was wondering about this:
>>
>> if i have a person value object and a actionform, both
>> describe exactly the same data.. wouldnt it be possible
>> to just define a person field in the actionform, and delegate
>> the getters and setters of the actionform to the getter and
>> setters of the person value object..
>>
>> like this i could avoid endless setter calling in my
>> actions..
>>
>> is that possible some how and a good practice? or impossible
>> and/or horrible bad? =)
>>
>>
>
> Seems you're having similar quandries to me.  See 'Getting Action Form
> data...'
>
> If I understand you and Struts correctly (unlikely at this early for
me
> stage!)
> You're form bean represents the user form and that alone, if you 
> delegate to
> the business object then the form could be populated with this, does 
> this
> make sense in your system?

actually the the form bean and the value object are quite similar they
repesent exactly the same kind of data (a person).. the only difference
is that the value object is passed to the business layer and thereby 
should
be not coupled to a web layer, which is the case for the form bean..

so its not really about how do i populate formbeans with data from the
business layer..

its more about how to i get rid of the duplication of fields in the 
formbean
and the value object..

plus its really akward to call 20 getter and setter methods in the 
action,
to get the data from the formbean to the value object..


ciao!
florian


---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org




---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org


RE: value objects and forms

Posted by Mike Whittaker <mi...@ntlworld.com>.
>
>actually the the form bean and the value object are quite similar they
>repesent exactly the same kind of data (a person).. the only difference
>is that the value object is passed to the business layer and thereby
>should
>be not coupled to a web layer, which is the case for the form bean..
>
>so its not really about how do i populate formbeans with data from the
>business layer..

No I understood you, I was just trying to say that if you did use the form
bean in the business layer, then it would affect the 'view' form if you
changed its properties. (for that session/request depending on the forms
scope).  And of course MVC is violated.

>
>its more about how to i get rid of the duplication of fields in the
>formbean
>and the value object..

Yes, precisely my problem.

My proposed solution - and why I haven't stumbled on this without hours of
API sifting I don't know.
Is (tuned to my requirements but you'll get the principle):

Warning! I am a struts newbie, this is probably diabolical.

#1 Subclass Action as my apps Action superclass, with this code

    public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest req, HttpServletResponse res) throws Exception {

		ParameterMap map = new ParameterMap(req.getParameterMap());
		PropertyDescriptor[] desc = PropertyUtils.getPropertyDescriptors(form);
		DynaProperty[] props = new DynaProperty[desc.length-4];

		// make the class from which to create value Beans in the image of the
form bean
		int j=0;
		for(int i=0;i<desc.length;i++) {
			String name = desc[i].getName();
			if(	name.equals("servlet") ||
				name.equals("servletWrapper") ||
				name.equals("multipartRequestHandler") ||
				name.equals("class")) continue; // filter out superclass (ActionForm)
methods	(YUK!)

			props[j++] = new DynaProperty(name, String.class); // create a class
attribute for the ones that matter.
		}
		BasicDynaClass dynaClass = new BasicDynaClass("dynaValueBean",
BasicDynaBean.class, props); // create new dynamic class with those
attributes

		DynaBean myDynaBean = dynaClass.newInstance(); // create new valueBean
		PropertyUtils.copyProperties(myDynaBean,form); // copy across all the form
bean properties
		req.setAttribute("valueBean",myDynaBean); // add to request

		return null;
	}
#2 In any mapped Action sub class of this, call this method, and then
extract the valueBean from the request.

Fairly messy, but it could be made neater, and maybe with more API
searching/replies to this problem, there will be a neater stock solution.

>
>plus its really akward to call 20 getter and setter methods in the
>action,
>to get the data from the formbean to the value object..

Quite, this solves that.
Must be a better way to implement it though, anybody...

--
Mike Whittaker


---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org


Re: value objects and forms

Posted by florian <cs...@structbench.com>.
On Sonntag, Mai 18, 2003, at 09:59  Uhr, Mike Whittaker wrote:

>
>> im passing value objects from struts actions down to ejbs.
>> i find it quite annoying to always get every value from the
>> formBean and set it to the appropiate value in the value
>> object..
>>
>> so i was was wondering about this:
>>
>> if i have a person value object and a actionform, both
>> describe exactly the same data.. wouldnt it be possible
>> to just define a person field in the actionform, and delegate
>> the getters and setters of the actionform to the getter and
>> setters of the person value object..
>>
>> like this i could avoid endless setter calling in my
>> actions..
>>
>> is that possible some how and a good practice? or impossible
>> and/or horrible bad? =)
>>
>>
>
> Seems you're having similar quandries to me.  See 'Getting Action Form
> data...'
>
> If I understand you and Struts correctly (unlikely at this early for me
> stage!)
> You're form bean represents the user form and that alone, if you 
> delegate to
> the business object then the form could be populated with this, does 
> this
> make sense in your system?

actually the the form bean and the value object are quite similar they
repesent exactly the same kind of data (a person).. the only difference
is that the value object is passed to the business layer and thereby 
should
be not coupled to a web layer, which is the case for the form bean..

so its not really about how do i populate formbeans with data from the
business layer..

its more about how to i get rid of the duplication of fields in the 
formbean
and the value object..

plus its really akward to call 20 getter and setter methods in the 
action,
to get the data from the formbean to the value object..


ciao!
florian


---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org


RE: value objects and forms

Posted by Mike Whittaker <mi...@ntlworld.com>.
>im passing value objects from struts actions down to ejbs.
>i find it quite annoying to always get every value from the
>formBean and set it to the appropiate value in the value
>object..
>
>so i was was wondering about this:
>
>if i have a person value object and a actionform, both
>describe exactly the same data.. wouldnt it be possible
>to just define a person field in the actionform, and delegate
>the getters and setters of the actionform to the getter and
>setters of the person value object..
>
>like this i could avoid endless setter calling in my
>actions..
>
>is that possible some how and a good practice? or impossible
>and/or horrible bad? =)
>
>

Seems you're having similar quandries to me.  See 'Getting Action Form
data...'

If I understand you and Struts correctly (unlikely at this early for me
stage!)
You're form bean represents the user form and that alone, if you delegate to
the business object then the form could be populated with this, does this
make sense in your system?

What I've been investigating is duplicating these form beans dyamically (see
commons.beanutils package) and populating them from the original servlet
level parameter map with one of those beanutils methods.

Perhaps any replies to my post will help you too.

--
Mike Whittaker


---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org