Hello,
I have a relatively simple application I am building using Struts 1.1.
I've just started using this framework so I'm not quite up to speed yet. My
question is in relation to the action forms: where should I put all my
business logic? For example, I've got a an action form with about 20 values
(ie get/set methods) and if they all validate properly I'd like to submit
this to a database.
What I have read here in this newsgroup is that in your Action you
should first get the formBean and then submit this to a javaBean. The
question I have is: should I write a new javaBean with exactly all the same
values as the formBean, and in that javaBean write the methods to write to a
database, and others I would like to implement such as "toXML()" method?
Let's say I take that approach--this brings me to my next question then: is
there an easy way to send all the "values" from the formBean to the
javaBean, or do I have for example like this:
customerForm f = (customerForm) form;
customerBean cb = new customerBean();
cb.setName(f.getName());
cb.setAddress(f.getAddress());
.... and so forth for about 20 or so values?
And lastly, is there an easy way to serialize the javaBean to XML?
Lots of questions here! Thank for any help!
-Tom
G - 16 Jul 2003 21:22 GMT
- You should not put your business logic in your action forms. In my
opinion, Action forms are the view in the MVC pattern (or at least the
'proxy' between the view and the controller, not the controller and the
model).
I use to copy the properties into a "model" bean and pass that bean to
the classes implementing the business logic.
customerActionForm f = (customerActionForm ) form;
CustomerBean cb = new CustomerBean(); // "model" bean
BeanUtils.copyProperties( f, cb ); // or cb, f... see later
CustomerDAO.registerCustomer( cb ); // Do business logic
But a lot of people implement the registerCustomer method into the
CustomerBean class...
- You can copy all the matching properties from a bean to other target
bean this simple way:
BeanUtils.copyProperties( sourceBean, targetBean);
(or maybe copyProperties( targetBean, sourceBean), i don't remember).
I think BeanUtils class is part of the commons-beanutils package...
- If you are using JDK 1.4 you can serialize your java bean using
XMLEncoder class. It is _very_ easy, something like (take a look at the
api)
XMLEncoder.encode( bean );
regards from Spain.
> Hello,
>
[quoted text clipped - 27 lines]
>
> -Tom
tk - 22 Jul 2003 05:44 GMT
What he said!!! :-)
Business logic should (IHMO) be placed into classes who's message
invocations are delegated thru the Action methods of the "model" portion.