wood burning stoves 2.0*
The moose likes Struts and the fly likes Doubt regarding DTO Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Doubt regarding DTO" Watch "Doubt regarding DTO" New topic
Author

Doubt regarding DTO

Rajaprabhu Aravindasamy
Greenhorn

Joined: Dec 26, 2011
Posts: 25
Hello guys..!

I'm having a doubt in setting a value to the DTO object in my project.

we can set a value directly to DTO object like the below code..,

User user=new User();
user.setUserId(1);
user.setName("Jinoy P George");
user.setDesignation("Programmer");
user.setAge(35);

But my doubt is how can we set a value to DTO object from a JSP form with out using getters and setters...!

User user=new User();
user.setUserId(?);
user.setName(?);
user.setDesignation(?);
user.setAge(?);

What may come in the place of question mark..? please post me a reply soon..!
Komal Renu
Ranch Hand

Joined: Jul 21, 2011
Posts: 51
Hi Raja,

Ideally you should have a form bean for the properties on the JSP page to be able to set those values in the DTO.
However if you want to avoid that, then i guess you can achieve this by sending those values form JSP in url parameters or setting them in session variables.

Please note that these are not good coding practices though.

Thanks and Regards,
-------------------------------------------------------------------------------------
Komal Renu | krenu@infocepts.com | www.infocepts.com
-------------------------------------------------------------------------------------
Rajaprabhu Aravindasamy
Greenhorn

Joined: Dec 26, 2011
Posts: 25
@Komal Renu

Thank you very much for your reply..!

Generally we are using getters and setters in our action classes to get values from JSP pages
and pass values to JSP pages through OGNL.
But in order to minimize those complexity of writing getters and setters every time, we go for DTO..
So that i asked a doubt(getting value from JSP in action class with out getters and setters) like that..

I'm just a learner of Struts2 now. Please correct and guide me if my above thoughts are wrong and
give me the example code to set the values to DTO in action class from JSP..!

Thank you..!

Komal Renu
Ranch Hand

Joined: Jul 21, 2011
Posts: 51
Hi Raja,

I understand you problem more clearly now.
Before i proceed with giving you an example, there are few questions i have which will help me provide better solutions.

1. Can you give me a smal code snippet from your JSP?
2. When you define the various tags in JSP are you using those property names from any bean or are they random names?
3. Is your DTO supposed to act like a form bean ? Does you application have a form bean ? (a form is nothing but a class that has the property names and their setters and getters)

Please confirm.

Thanks and Regards,
-------------------------------------------------------------------------------------
Komal Renu | krenu@infocepts.com | www.infocepts.com
-------------------------------------------------------------------------------------
Rajaprabhu Aravindasamy
Greenhorn

Joined: Dec 26, 2011
Posts: 25
@ Komal Renu

Here is my JSP code snippet..,

<tr>
<th>
<div align="right">
メールアドレス(Mail Address)
</div>
</th>
<td>
<s:property value="MailAddress" />
<input type="hidden" name="MailAddress" value="<s:property value="MailAddress" />"/>
</td>
</tr>

actually the snippet is from a confirmation page that is proceeding to Insert action.
So i need the mail address in my action to set it in my DTO object.
the code is,

UserDTO obj=new UserDTO(); // In UserDTO.java, i had written the getters and setters for all the fields
obj.setMailADDRESS(?); // i need to pass the mail address in the place of question mark

UserDAO obj1=new UserDAO();
obj1.Insert(obj); // here i'm passing the object of DTO.., so that i can manipulate the mail address in DAO using the DTO object(obj.getMailAddress()).

This is my exact scenario.. Correct me if anything i did wrong..!

Thank you..!


Komal Renu
Ranch Hand

Joined: Jul 21, 2011
Posts: 51
Hi Raja,

Ideally you should create a form, for all the fields you require. This form bean should be used in the jsp, That ways, when you navigate to the action class incase of page submit, your form bean would have the user updated values.

This is how you write the useBean tag in JSP :



Once you write the above line in you jsp, then you can directly access the form properties like :



In your action class you can get this form and then set the necessary parameters in your DTO as



This is the better way to do it.

However in your case, either you can use the DTO directly on the jsp page or through an object in your form.

Hope this helps!

Thanks and Regards,
-------------------------------------------------------------------------------------
Komal Renu | krenu@infocepts.com | www.infocepts.com
-------------------------------------------------------------------------------------
Rajaprabhu Aravindasamy
Greenhorn

Joined: Dec 26, 2011
Posts: 25
Thank you..! Komal Renu..!

I have fixed my problem by using the similar way that you are saying..

My approach was,

I created a bean for the form datum(as you said..!).
and then i just used ModelDriven interface in my action class to get the values from bean..!

Any way thank you very much for your help..!
Komal Renu
Ranch Hand

Joined: Jul 21, 2011
Posts: 51
Thats great
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Doubt regarding DTO
 
Similar Threads
Doubt in Effective Java
Is it possible to retrive multiple objects at one time ?
jsp:useBean Doubt?
srting doubt on ==
Dynamic Typecasting