| Author |
DynaValidatorForm
|
jay roy
Ranch Hand
Joined: Nov 16, 2006
Posts: 145
|
|
hi guys I have a form-bean like this in my struts - config <form-bean name="AddressInfoBean" type="org.apache.struts.validator.DynaValidatorForm" dynamic="true"> <form-property name="city" type="java.lang.String" /> </form-bean> I have a field in my jsp page like this <TR> <TD class="line" colspan="4"> Address CITY:<html:text property="city" style="20" /> // corresponding to above form-bean </TD> <tr> <html:submit property="method"> SUBMIT </html:submit> </tr> </TR> I want to exactly know the flow when i enter some value to "city" field and click on submit button on the jsp. what exactly is the communication process between the "city" field in jsp and the entry <form-property name="city" type="java.lang.String" /> in form-bean. How are they mapped and what internal methods are invoked when a value is entered in the "city" field and SUBMIT is clicked. Basically , how is the value in the "city" field ( when entered at runtime) persisted to the for-bean When is this line "org.apache.struts.validator.DynaValidatorForm" read by struts a ) when server starts or b) when submit button is clicked. c) both times d) when some other operation occurs. The reason i am asking this is because i want to do some runtime operations on the city field. I would appreciate if anyone can share the answers with me. thanks Jay
|
 |
Merrill Higginson
Ranch Hand
Joined: Feb 15, 2005
Posts: 4864
|
|
Here's a simplified version of the sequence of events: A user requests "mypage.jsp", which causes the web container to pass control to the compiled version of your JSPWhen the <html:form> was compiled, it added code to look at the action attribute and find the action path in the struts-config.xml file. It then sees that the action is associated with a DynaValidator Form. If there is no instance of this bean in the specified scope, Struts will instantiate the bean, initialize its properties, and put it in the designated scopeThe code created by your <html:text> tag looks in the form bean for a property by the name of "city". It then calls the "getter" on that property. In the case of a DynaValidatorForm, the call is get("city"). If the property exists and has a value, Struts puts it in the value attribute of the rendered HTML. For example, if the get("city") method returns "New York", Struts will render the tag as <input type="text" name="city" value="New York" > and send the HTML back to the browser.The user sees the form on the browser, changes the City field to "Chicago" and presses the submit buttonThe Web Container receives the request and passes control first to the Struts ActionServlet, then to the Struts RequestProcessor. The RequestProcessor looks at the action path and matches it up with a form bean. If an instance already exists in the specified scope, it is used. If not, Struts instantiates a new one.The RequestProcessor then calls on the BeanUtils.populate method to match up the parameters received in the input stream with the properties of the ActionForm bean. In this case, the BeanUtils will find a "city" parameter in the request. Since the value in the city paramater is "Chicago", Struts will then call set("city", "Chicago") on the DynaValidatorForm bean which will set the value in the form bean.After all the properties on the form bean have been populated, the RequestProcessor passes control to the Action class you wrote and processes the logic in your execute method. [ February 20, 2007: Message edited by: Merrill Higginson ]
|
Merrill
Consultant, Sima Solutions
|
 |
jay roy
Ranch Hand
Joined: Nov 16, 2006
Posts: 145
|
|
thanks for the excellent explanation. It was very helpful. I was actually trying to override initialize method of DynavalidatorForm something like... <form-bean name="AddressInfoBean" type="mypackage.MyDynaForm" dynamic="true"> and this is MyDynaForm public class MyDynaForm extends DynaValidatorForm{ //trying to overrie initialize method. public void initialize(ActionMapping mapping) { System.out.println("Inside MyDynaForm"); //initialize form bean name String name = mapping.getName(); } but System.out.println("Inside MyDynaForm "); is never printed neither when server starts nor when i hit the SUBMIT button(the submit button maps to the above form-bean MyDynaForm in struts-config using html:form tag). I am trying to get hold of DynaValidatorForm and try manupulating its methods, but it doesnt seem to work! Actually there are some field values which i am passing from the jsp but are not getting persisted in the form-bean (MyDynaForm). so i wanted to print them inside MyDynaForm something like System.out.println("field value" +fieldValue which is entered in jsp) thanks J [ February 21, 2007: Message edited by: jay roy ] [ February 21, 2007: Message edited by: jay roy ]
|
 |
Merrill Higginson
Ranch Hand
Joined: Feb 15, 2005
Posts: 4864
|
|
Here's my personal opinion: I've never had any use for DynaActionForm or DynaValidatorForm. For me it's just as easy to write a subclass of ActionForm with properties and accessor methods as it is to definie properties for a DynaActionForm in the struts-config.xml using XML. There are several advantages to subclassing ActionForm: You can instantiate an ActionForm any time you want. With DynaActionForm, you must always let Struts instantiate the bean for youThe getters and setters are more intuitive an less error prone. The compiler will tell me if i misspell "getFirrsttNammme()". It will not tell me if I misspell get("firrsttNammme") As I see it, all this manipulation of the DynaValidatorForm is an exercise in futility. I'd suggest you just write an ActionForm or ValidatorForm bean. There you have complete control of what goes on instead of relying on the "black box" of the DynaValidatorForm bean. [ February 21, 2007: Message edited by: Merrill Higginson ]
|
 |
jay roy
Ranch Hand
Joined: Nov 16, 2006
Posts: 145
|
|
thanks again. I agree with you that ActionForms are more simple, more intuitive and have advantages over dynaforms like getting detected on compile time,ect. But in our project we are extensively using struts validation framework (server side validation) and as far as i know struts validation framework cannot be used with Regular action forms. It has to be used with DynaValidatorForms. (i guess thats why the name DynaVALIDATORforms) . Or may be there is a way to handle struts validation framework with action forms.but i am not aware of it. what do you think. how are you guys handling the validation stuff?? thank you J
|
 |
jay roy
Ranch Hand
Joined: Nov 16, 2006
Posts: 145
|
|
Just for clarification , when i say >> struts validation framework (server side validation) , i mean validation using validation.xml, validation-rules.xml or writing my own CustomValidator using Struts validator api. http://struts.apache.org/1.2.4/userGuide/dev_validator.html thanks J
|
 |
Brent Sterling
Ranch Hand
Joined: Feb 08, 2006
Posts: 948
|
|
as far as i know struts validation framework cannot be used with Regular action forms
Yes it can. Your forms just need to extend ValidatorForm instead of ActionForm. - Brent
|
 |
 |
|
|
subject: DynaValidatorForm
|
|
|