File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Struts and the fly likes Using DAO in struts Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Using DAO in struts" Watch "Using DAO in struts" New topic
Author

Using DAO in struts

Mala Sharma
Ranch Hand

Joined: Oct 29, 2004
Posts: 76
Hello,
I'm learning struts. I don't want to put the database stuff in the Action class so i call the DAO class and let it do the work. In my DAO, i instantiate the form class and then get its member variable through getXXX() like this:

But the problem is getName() and getPassword() is returing null so i can't insert. But if i print out getName() in my Action class execute() method, it prints out fine, but, ofcourse there i'm instatiating the form class as:

So, my question is: Do i HAVE to get the form elements in the Action class and send those to my DAO method as arguments or i'm doing somethign wrong? I don't want to send them as arguements. Thanks for your time.

Regards,
Mala
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
Mala,

You have to either get the individual properties and send them as parameters, or send the entire form as a parameter. The Struts action class provides you with a reference to the form, but if you want to use it in a DAO, you will have to pass it to the DAO in the form of a parameter.

Husted and other authors recommend against using the form as a value object to pass things back and forth between the model and the view. They recommend creating a separate model bean and mapping between the model bean and the form. This is a matter of style and separation of the MVC layers, but There's no technical reason you can't do it.


Merrill
Consultant, Sima Solutions
Mala Sharma
Ranch Hand

Joined: Oct 29, 2004
Posts: 76
Hello Merrill,

Thanks for your answer. I did it with passing the form as an argument but as you stated its a bad solution, i want to try something else. Could you please explain in detail how to achieve this:
They recommend creating a separate model bean and mapping between the model bean and the form.
I would really appreciate it. THanks for your time.

Regards,
Mala
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
Mala,

Here's one way to do it:

Code a javaBean called UserVO (User Value Object) which represents the user in your object model. Use this object to pass values to and from the Data Access Object.

Here's a sample:



Then, in your form, code 2 methods: mapFromVO() and mapToVO() Here's what they'd look like:




In your action class, call these methods to convert data from your value object to your form and back again.

This way you have a Value Object that is part of the model, and the form object remains part of the view and you avoid mixing the two in one object.

Merrill
Mala Sharma
Ranch Hand

Joined: Oct 29, 2004
Posts: 76
Hello Merrill,

THanks alot for spending time to explain me the concept. It's crystal clear what you were saying. But, We would have to define member varibles twice in this case. First in UserVO class

and then in form class where we make use of the methods you mentioned above to set and get this variable. Don't you think that's bad practice in java because we are defining same varibles in 2 classes. If we pass the form as an arguement to the DAO class, we are not doing that. YOur thought on this?. Again, i really appreicate you taking time to help me.

Regards,
Mala
Carol Enderlin
drifter
Ranch Hand

Joined: Oct 10, 2000
Posts: 1364
Check out JavaWorld Struts best practices article. The link should be to page 4 - discusses the Data Transfer Object. Here I quote that part:



Data transfer object implementation
Problem
Usually a data transfer object (DTO) is used for shuttling data between the Web and EJB tiers. It is not a good idea to pass the view-helper class (form bean, in the case of Struts), to the EJB tier, primarily because all of the form bean's fields are of type string. A separate class should be used as the data transfer object. The problem is how to transfer the data from the form bean to the DTO.

Struts best practice
Two options are available for populating the data transfer object:


1. Create the transfer object and copy the data from the form bean to the transfer object. Data-type conversions must be handled before data copying. Similarly, on the way back, you must repeat the same exercise of copying the data from the DTO to the form bean.

2. Use Commons' BeanUtils class, which uses the Reflection API to achieve its objectives.

Developers frequently use the first option, which is not problematic except that the mundane exercise is repeated every time, resulting in bulky and ugly code. While copying data, suitable data-type conversion must be hand-coded in the DTO's setter methods, so that when data is copied from the form bean to the DTO, it converts to the appropriate business type. All the attributes of type string in the form beans are the primary reasons for data-type conversions.

You can avoid all this coding by using BeanUtils's copyProperties() method. The method copies the data from one bean to another, provided the variable names of the business attributes in the value/transfer object are the same as the ones in the form bean. The method also relieves you from data-type conversion by transparently converting source-bean attribute types to destination-bean attribute types.


 
Consider Paul's rocket mass heater.
 
subject: Using DAO in struts
 
Similar Threads
Accesing two synchronized methods
Spring 2 + Aspectj, no working when fetching objects from database
couldn't access data using Dao
Hibernate and Struts 2 question
How to use Validators