struts question: ok to use actionform as model?

Discussion in 'Java' started by tom, Jul 15, 2003.

  1. tom

    tom Guest

    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
    tom, Jul 15, 2003
    #1
    1. Advertising

  2. tom

    tom Guest

    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:-ip.de...
    >
    > "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/beanutils/api/index.html
    > You can download the BeanUtils component here:
    > http://jakarta.apache.org/commons/beanutils.html
    >
    > Hope that helps,
    >
    > regards,
    > Joerg
    >
    >
    >
    >
    tom, Jul 17, 2003
    #2
    1. Advertising

  3. tom

    code learner

    Joined:
    Dec 24, 2010
    Messages:
    19
    BeanUtil.copyProperty() may help you.

    bye
    learner
    code learner, Dec 24, 2010
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Rode
    Replies:
    0
    Views:
    834
  2. PERCIVAL BRAGG
    Replies:
    0
    Views:
    535
    PERCIVAL BRAGG
    Oct 15, 2003
  3. Pratap Das
    Replies:
    2
    Views:
    2,016
    Pratap Das
    Apr 5, 2004
  4. sp

    Struts and actionForm

    sp, Jul 12, 2004, in forum: Java
    Replies:
    1
    Views:
    546
    Sudsy
    Jul 12, 2004
  5. Hamvil
    Replies:
    0
    Views:
    374
    Hamvil
    Nov 4, 2005
Loading...

Share This Page