I am new to Struts and need some help understanding how to best pre-fill an ActionForm. At a high level, I created a jsp page, ActionForm and Action for some user information. The form, field validations, etc... all work well as expected. I would like to implement an edit function that would pre-load the user information onto the form and allow the user to update. I have the update portion of the code working (saving updates to database), but have not figured out how to get the form to pre-load the user info. I have developed a User object stored in the session with getter, setter, save, initialize, etc... methods.
1) Should the pre-loading occur in the ActionForm or the Action?
- If in the ActionForm, how can I access the session to retrieve the User object?
- If in the Action, where and how can it be done? In the execute method of the Action, I can use the request object to get to the session, but the execute method only seems to execute after the form passes the validation process, not when the jsp/ActionForm is first initialized.
2) I tried importing the User Object onto the jsp page, but when I use the html:text tag in conjunction with the bean:write tag, it gives me an error referring to no bean found in scope. If I need to declare the the User object as the page bean object, how do I go about doing so?
I can upload the Action/ActionForm/jsp code and the struts config if needed, but I was hoping to get a high level understanding of how to best approach this scenario. Please let me know if you need the code and configuration files to help answer my question?
Edgardo Mejias wrote:..., but have not figured out how to get the form to pre-load the user info.
What does it exactly mean - on page load or on submission of the page?
Edgardo Mejias wrote:I have developed User object stored in the session with getter, setter, save, initialize, etc... methods
This is not required until and unless you want to save the data for the complete session
Edgardo Mejias wrote:Should the pre-loading occur in the ActionForm or the Action?
I prefer ActionForm, you can still have a behavior in form [say formPrePopulate] which accepts "request"[through action class] as parameter from where we can access session. I prefer this to make sure my form deals only with fields/data and action class deals only with the action it needs to perform for the request.
Edgardo Mejias wrote:but when I use the html:text tag in conjunction with the bean:write tag
Please do not mess up the JSP's, why would we need a framework if we can not make use of it to the fullest? It would make our job more tedious later.
Have Fun with Java
little,little.. little by little makes a lot..