struts-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Rick Reumann" <rick.reum...@gmail.com>
Subject Re: Inheritance within form beans
Date Wed, 03 Oct 2007 21:37:17 GMT
On 10/3/07, Kothari, Kailash <Kailash_Kothari@intuit.com> wrote:
> Thanks a lot Rick. That gives me a new area to explore.
>
> The BeanUtils method - BeanUtils.copyProperties(Object target, Object
> src) - does give me a shallow copy of an object, Im trying to figure out
> how to get a deep copy..

I might be the only one that does this, but I have all my form beans
extend an abstract  BaseActionForm that has these two methods:

public void populateValueObject(Object obj) throws
IllegalAccessException, InvocationTargetException {
		 BeanUtils.copyProperties(obj, this);
	}
	public void populateForm(Object obj) throws IllegalAccessException,
InvocationTargetException {
		 BeanUtils.copyProperties(this, obj);
	}

The default behavior is nothing interesting - for example in a
setUpFormForEdit method in an Action:

public ActionForward setUpFormForEdit(...) {
      EmployeeForm eForm = (EmployeeForm)form;
      Employee employee = serviceLayer.getEmployee(someID);
      eForm.popluateForm( employee );
}

Similar thing on the way out   :

public ActionForward doEdit(...) {
      EmployeeForm eForm = (EmployeeForm)form;
      Employee employee = new Emloyee();
      eForm.populateValueObject( employee );
      serviceLayer.doUpdate(employee);
}

Where it becomes more interesting is when you have cases where
BeanUtils can't do everything. For example, I've had cases where if
certain items in a form were checked, it would alter what I do the
resulting ValueObject and same thing the other way.  In a more common
sense, sometimes people might have a form with a month, date, and year
form fields but ultimately your value object just needs a single
"date."  I found it nicer in these cases then to just override those
base class methods and make sure my form and value object is populated
correctly there instead of doing all that mess in my Action class.

In your case this is where you'd do your deep copy stuff....

YourCustomActionForm extends YourBaseBeanActionForm {

public void populateForm(Object obj)   {
         //maybe first get a base shallow copy of some things
         //BeanUtils.copy or call super.populateForm(obj)

         //now do your custom deep copy stuff as needed
}

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


Mime
View raw message