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