RE: Pre-Populating ActionForm (again)
Aapo, Would you care to elaborate on your solution? ThomasB -Original Message- From: Aapo Laakkonen [mailto:[EMAIL PROTECTED]] Sent: Wednesday, July 25, 2001 6:25 AM To: [EMAIL PROTECTED] Subject: Re: Pre-Populating ActionForm (again) I know this too... but it's not the problem what I'm having. Here is the work flow: 1. select user from list - executes SelectUserAction that places UserBean in request scope. - forwards to edituser.jsp. 2. Populate form that is in edituser.jsp from UserBean - that form has action mapping to UpdateUserAction. 3. Submit form (Update) - UserForm gets populated and then it's validated by struts by calling validate method. - If validation fails then go back to edituser.jsp and the problem arises: there is not anymore UserBean in request scope, because SelectUserAction is responsible of that and it does not get executed, so I get error: No xxx bean in scope null. So how do I write that form so I can populate it automatically? I mean that what code I should put in SelectUserAction so I can use tag like this: I got it to work. Thanks for everyone who replied to this issue!
Re: Pre-Populating ActionForm (again)
Here is the work flow: 1. select user from list - executes SelectUserAction that places UserBean in request scope. - forwards to edituser.jsp. 2. Populate form that is in edituser.jsp from UserBean - that form has action mapping to UpdateUserAction. 3. Submit form (Update) - UserForm gets populated and then it's validated by struts by calling validate method. - If validation fails then go back to edituser.jsp and the problem arises: there is not anymore UserBean in request scope, because SelectUserAction is responsible of that and it does not get executed, so I get error: No xxx bean in scope null. So how do I write that form so I can populate it automatically? I mean that what code I should put in SelectUserAction so I can use tag like this: I got it to work. Thanks for everyone who replied to this issue! Would you care to elaborate on your solution? Ok here it goes. Instead of populating custom UserBean I populate UserForm Bean in EditUserAction. The form is the same that is also used in UpdateUserAction and InsertUserAction. I know that struts guidelines say that reuse you action classes, but I like separating these different actions in different action classes because I'm quite new to this Java World and I'm not using EJBs. This works for me. If you are planning bigger application then it is recommended to use EJBs, but the timeframe in this project is so tight so I have no time to learn all those fancy Java APIs and Patterns. My Mappings looks like this: form-beans form-bean name=UserForm type=com.projectcast.sfundm.UserForm/ /form-beans action-mappings action path=/userlist type=com.projectcast.sfundm.UserListAction name=UserForm scope=request validate=false forward name=success path=/WEB-INF/web/userlist.jsp/ !-- this is redundant as it forwards to same place as success, but it is here so I can change it if I want and Action class remains unchanged -- forward name=failure path=/WEB-INF/web/userlist.jsp/ /action action path=/edituser type=com.projectcast.sfundm.UserSelectAction name=UserForm scope=request validate=false forward name=success path=/WEB-INF/web/edituser.jsp/ /action action path=/updateuser type=com.projectcast.sfundm.UserUpdateAction name=UserForm scope=request input=/edituser.do validate=true forward name=success path=/userlist.do redirect=true/ forward name=cancel path=/userlist.do redirect=true/ /action action path=/insertuser type=com.projectcast.sfundm.UserInsertAction name=UserForm scope=request input=/userlist.do validate=true forward name=success path=/userlist.do redirect=true/ /action action path=/deleteuser type=com.projectcast.sfundm.UserDeleteAction scope=request input=/userlist.do validate=false forward name=success path=/userlist.do/ /action /action-mappings Pretty weird, huh!
Re: Pre-Populating ActionForm (again)
if u have overridden validate method in the actionForm then ActionServlet automatically take u to the mapping.getInput() ( i.e ur input jsp file ) where u can show the errors using html:errors tag . Basicallly ActionErrors are kept in the request scope which are retrieved in the html:errors tag Else if u want to do validation even in ur action class , then create a instance of ActionErrors in the action class and add errors to it . save the errors using saveErrors(request , actionErrros) method available in the Struts- Action class . So errors will be kept in the request scope and again use same html:errors tag to display them For both editUser and save saveUser use same actionForm Suhas - Original Message - From: Aapo Laakkonen [EMAIL PROTECTED] To: [EMAIL PROTECTED] Sent: Wednesday, July 25, 2001 2:32 PM Subject: Pre-Populating ActionForm (again) I know that this have been asked several times, but I did not find answer for this little problem. I have list of lets say users. Each user has a link that maps to edituser action. That action then gets executed and it places userbean to request scope. Then struts forwards action to edituser.jsp. Edituser.jsp has form that has action mapping set to updateuser. When user submits that form which binds to userform action form the validate method of that actionform gets executed. If validation errors occurred then we get back to input form (which is edituser.jsp or edituser.do -- which to use?). And this is the point when I do not really understand what I should do, because when user gets back to edituser.jsp or edituser.do then there is not anymore that userbean is request scope. And because edituser.do does not have any forms associated with it then I should instead use edituser.jsp as input for updateuser action. But how I then write that form (jsp-page)? I have several options: 1. put userbean in session scope (might be problem in multiserver environment also the modifications that user entered are not visible). 2. Have another jsp-page that does not populate the form from userbean. Not good option because almost the same form needs to be written second time. 3. Use logic tags to check the existence of userbean and if it does not exist then do not populate and let the form populate itself from posted form. Same problems as in 2. 4. Somehow prepopulate the form with no manual population, but automatically. So do I pre-populate the form. What code I need to put in edituser action that previously placed userbean in request scope. I want to place the form object in that same scope, but don't know how. This is the best option and has no problems or at least I have not found a one. Thanks again! Aapo Laakkonen -- Aapo Laakkonen, +358 (50) 33 99 682, ProjectCast Ltd, Helsinki, Finland.
Re: Pre-Populating ActionForm (again)
if u have overridden validate method in the actionForm then ActionServlet automatically take u to the mapping.getInput() ( i.e ur input jsp file ) where u can show the errors using html:errors tag . Basicallly ActionErrors are kept in the request scope which are retrieved in the html:errors tag I know that. Else if u want to do validation even in ur action class , then create a instance of ActionErrors in the action class and add errors to it . save the errors using saveErrors(request , actionErrros) method available in the Struts- Action class . So errors will be kept in the request scope and again use same html:errors tag to display them I know this too... but it's not the problem what I'm having. Here is the work flow: 1. select user from list - executes SelectUserAction that places UserBean in request scope. - forwards to edituser.jsp. 2. Populate form that is in edituser.jsp from UserBean - that form has action mapping to UpdateUserAction. 3. Submit form (Update) - UserForm gets populated and then it's validated by struts by calling validate method. - If validation fails then go back to edituser.jsp and the problem arises: there is not anymore UserBean in request scope, because SelectUserAction is responsible of that and it does not get executed, so I get error: No xxx bean in scope null. So how do I write that form so I can populate it automatically? I mean that what code I should put in SelectUserAction so I can use tag like this: html:text property=userName size=14 maxlength=14/ to automatically pre-populate that form, instead of writing code like this: html:text name=userBean property=userName size=14 maxlength=14/. Is there some way to put pre-populated form in some scope so that struts automatically finds it and populates the form (I bet there is, but don't know how to do it). I also included my first mail in this so you can get more specific detail on this problem. I know that this have been asked several times, but I did not find answer for this little problem. I have list of lets say users. Each user has a link that maps to edituser action. That action then gets executed and it places userbean to request scope. Then struts forwards action to edituser.jsp. Edituser.jsp has form that has action mapping set to updateuser. When user submits that form which binds to userform action form the validate method of that actionform gets executed. If validation errors occurred then we get back to input form (which is edituser.jsp or edituser.do -- which to use?). And this is the point when I do not really understand what I should do, because when user gets back to edituser.jsp or edituser.do then there is not anymore that userbean is request scope. And because edituser.do does not have any forms associated with it then I should instead use edituser.jsp as input for updateuser action. But how I then write that form (jsp-page)? I have several options: 1. put userbean in session scope (might be problem in multiserver environment also the modifications that user entered are not visible). 2. Have another jsp-page that does not populate the form from userbean. Not good option because almost the same form needs to be written second time. 3. Use logic tags to check the existence of userbean and if it does not exist then do not populate and let the form populate itself from posted form. Same problems as in 2. 4. Somehow prepopulate the form with no manual population, but automatically. So do I pre-populate the form. What code I need to put in edituser action that previously placed userbean in request scope. I want to place the form object in that same scope, but don't know how. This is the best option and has no problems or at least I have not found a one. Thanks again! Aapo Laakkonen