This week's book giveaways are in the iOS and Features new in Java 8 forums. We're giving away four copies each of Barcodes with iOS: Bringing together the digital and physical worlds and Core Java for the Impatient and have the authors on-line! See this thread and this one for details.
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.
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.
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.
Joined: Feb 15, 2005
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.
Joined: Oct 29, 2004
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.
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.