Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > struts question: ok to use actionform as model?

Reply
Thread Tools

struts question: ok to use actionform as model?

 
 
tom
Guest
Posts: n/a
 
      07-15-2003
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



 
Reply With Quote
 
 
 
 
tom
Guest
Posts: n/a
 
      07-17-2003
Hello Joerg,

Many thanks for the help. I've decided to put the data access in the
bean itself... Yes I know I should probably use a separate object for the
data access but I think I'll leave that for my next version!

Regards,

-Tom


"Joerg Gippert" <> wrote in message
news:...
>
> "tom" <> schrieb im Newsbeitrag
> news:...
> > Hello,

>
> Hi Tom,
>
> > 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.

>
> In the ActionForm you should only do the validating of the form values.In
> your Struts config, you also set an action mapping to an action that

should
> do something with the date that has been passed through your ActionForm.

To
> store information, you usually use so called Data Transfer Objects(DTO).
> This is an object, that only has the variables you want to store and their
> according getter and setter methods. Look at Sunīs Java website for J2EE
> core patterns and especially at the DAO pattern (this is where DTO is
> explained). A simple example: Your form asks for an addres you should

enter.
> the fields in your form would be like name, lastname, street, zipcode etc.
> You create another object (the DTO) that would look like this:
> public class Address_DTO
> {
> private String name;
> private String lastname;
> private String street;
> ... and so forth
>
> //then getter and setter methods:
> public String getName()
> ...
> public void setName()
> }
>
> Then you should have another class, that actually does the database

access.
> It could be a class called MyDBHandler that has a method
> saveAddress(Address_DTO, address). In your Action you can simply type
> MyDBHandler.saveAddress(addres);
>
> > 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?

>
> You can use the BeanUtils class to go around this. It belongs to the

Apache
> commons jar. Check for org.apache.commons.beanutils.BeanUtils package.
>
> If you have it, you can easily do something like that (adjusted to your
> example above):
>
> Customer_DTO customerDTO = new Customer_DTO();
> BeanUtils.copyProperties( customerDTO, customerForm );
>
> For documentation please refer to:
> http://jakarta.apache.org/commons/be...api/index.html
> You can download the BeanUtils component here:
> http://jakarta.apache.org/commons/beanutils.html
>
> Hope that helps,
>
> regards,
> Joerg
>
>
>
>



 
Reply With Quote
 
 
 
 
code learner code learner is offline
Junior Member
Join Date: Dec 2010
Posts: 19
 
      12-24-2010
BeanUtil.copyProperty() may help you.

bye
learner
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[Struts] DispatchAction and ActionForm ShadowMan Java 2 07-12-2004 06:03 PM
Struts and actionForm sp Java 1 07-12-2004 01:10 PM
[Struts] Newbie - For ActionForm population, must I use Struts taglib? Pratap Das Java 2 04-05-2004 07:42 PM
Struts ActionForm set methods not being executed PERCIVAL BRAGG Java 0 10-15-2003 12:58 AM
Struts, Problems with 'indexed (nested) properties' in ActionForm Rode Java 0 10-09-2003 05:06 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57