AppletTalk.com Forum Index AppletTalk.com
Java discussions newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

struts question: ok to use actionform as model?

 
Post new topic   Reply to topic    AppletTalk.com Forum Index -> Java Help
View previous topic :: View next topic  
Author Message
tom
Guest





PostPosted: Tue Jul 15, 2003 10:50 pm    Post subject: struts question: ok to use actionform as model? Reply with quote



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





Back to top
G
Guest





PostPosted: Wed Jul 16, 2003 8:22 pm    Post subject: [OT] Re: struts question: ok to use actionform as model? Reply with quote



- 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.


tom wrote:

Quote:
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







Back to top
tk
Guest





PostPosted: Tue Jul 22, 2003 4:44 am    Post subject: Re: [OT] Re: struts question: ok to use actionform as model? Reply with quote



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.


Back to top
Display posts from previous:   
Post new topic   Reply to topic    AppletTalk.com Forum Index -> Java Help All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.