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.
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..!
posted 8 years ago
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)
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..!
posted 8 years ago
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.