*
The moose likes Struts and the fly likes DynaValidator reset options being reset. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "DynaValidator reset options being reset." Watch "DynaValidator reset options being reset." New topic
Author

DynaValidator reset options being reset.

Whitney Albrecht
Greenhorn

Joined: May 09, 2005
Posts: 19
I am having trouble with a form. When the validator throws the error all the drop-downs that have been selected get set back to default settings.

The form is in session scope. I have attempted in the reset method getting the values from the previous session with no avail.

I found this article

http://www.coderanch.com/t/47344/Struts/Code-When-Work-DynaValidatorForm

which might help me but can't find an archive or reference link to this article mentioned at the bottom

can anyone point me in the right direction?

thanks
Marc Peabody
pie sneak
Sheriff

Joined: Feb 05, 2003
Posts: 4727

Originally posted by Whitney Albrecht:
The form is in session scope. I have attempted in the reset method getting the values from the previous session with no avail.


This statement makes me question your belief of what a session is. What do you mean that you are getting the values from a previous session? Are these supposed to be values that were previously saved to the database? Please post the code from your reset() method.

In the link you gave, Rick mentions that he already answered the question in the Struts mailing list. The person asking the question often cross-posted between here and the Struts mailing list (a netiquette no-no) and went by the name Caroline Jen in the other group. You might be able to Google for it.


A good workman is known by his tools.
alan do
Ranch Hand

Joined: Apr 14, 2005
Posts: 354
if you are using htmlptions, the collection that you used to display the options must be placed in the session. if not, you must use html:select together with the bunch of htmlption's for struts to automatically select the previously selected value.


-/a<br />certified slacker...yes, my last name is 'do' - <a href="http://www.luckycouple.com" target="_blank" rel="nofollow">luckycouple.com</a>
alan do
Ranch Hand

Joined: Apr 14, 2005
Posts: 354
oh, and having the form itself in session is irrelevant to this problem.
Whitney Albrecht
Greenhorn

Joined: May 09, 2005
Posts: 19
For further clarification here is my retyped code:

In my stuts-config.xml

<form-bean name="newJobForm"
type="actionForm.job.NewJobActionForm">
<form-property name="list1" type="java.util.ArrayList"/>
<form-property name="selectionList1" type="java.lang.String"/>
...
<form-property name="list2" type="java.util.ArrayList"/>
<form-property name="selectionList2" type="java.lang.Long"/>
</form-bean>

<action path="/NewJobForm"
type="action.job.NewJobAction"
name="newJobForm"
scope="request"
input="program.newJob"
validate="true">
<forward name="Success" path="program.newJobRouting"/>
<forward name="Failure" path="program.newJob"/>
</action>

****

In my ActionForm class I overwrote the reset class to fill in each ArrayList (from the Database) which in turn are used as drop downs in the Jsp.

In the JSP I have something similar to this:

<html:select property="selectionList1">
<logic:iterate name="newJobForm" property="list1" id="row">
<option value="<bean:write name='row' property='division'/> ">
<bean:write name='row' property='division'/> ---
<bean:write name='row' property='division_abbrev'/>
</option>
</logic:iterate>
</html:select>

...

<html:select property="selectionList2">
<html ptionsCollection property="list2" label="sectionName" value="sectionID"/>
</html:select>

I have more fields on this form too. I have a form validator on this form. I understand if the required fields are not filled in of course the validator will throw it back and call the reset() again in my actionForm class.

My problem is that when the page reloads the dropdowns that were selected are getting reset.

Help!
alan do
Ranch Hand

Joined: Apr 14, 2005
Posts: 354
hm...i don't like playing with the 'reset' method. i haven't messed with the dropdown this way, but i don't trust using it.

have you tried using html ptionCollections while specify the 'selectionList2' type as 'java.lang.String' instead of 'Long'? also, you are using 'option' instead of html ption for selectionList1 which may not allow for the value to 'stick'.

i've always implemented my dropdowns this way...instead of putting the ArrayList in the form-bean, generate it in your initial action call and place it in the session scope (have a helper method to check to see if the collection exists already and generate only when necessary) and leave the reset method alone. use the html ptionsCollection as you are already. the down side is that if you have a HUGE ArrayList, storing that in the session scope has the same resource consumption as storing any huge object in the session. for smaller objects, i don't think it will be a big deal.

if it's unacceptable to put the ArrayList into the session, instead of putting the ArrayList in the session scope, you can place it into the request scope. since there will be no chance for you to rebuild the ArrayList when validation fails, you need to create your custom validation methods (extend FieldChecks.validateRequired() for example) and add a call to the DB to place the ArrayList back into the request before returning 'false'. of course, this requires some work with the validation framework (see struts user guide on validator - validateTwoFields example).

ok, another way would be generating all the drop-down options and store them as ArrayLists into the application scope (have a separate Servlet or an existing StartUpServlet of some kind retrieve and store the ArrayLists into the ServletContext). this way the ArrayLists are generated once per start of the app. the down side is that dynamic values updated into the DB won't be picked up until next application restart. it's best used when dropdowns are fixed (States, countries etc.)

so there isn't a nice way of addressing this problem. i'd go with the session approach if possible.
Whitney Albrecht
Greenhorn

Joined: May 09, 2005
Posts: 19
i've always implemented my dropdowns this way...instead of putting the ArrayList in the form-bean, generate it in your initial action call and place it in the session scope (have a helper method to check to see if the collection exists already and generate only when necessary) and leave the reset method alone.



Do I need to create a similar setup where I have two action classes. Where one fills the ArrayList in (and sets it as an attribute) and one that handles the action? (jakarta.apache.org/struts/faq/newbie.html#prepopulate).

Am I on the right track?

Thanks for you help.
[ October 03, 2005: Message edited by: Whitney Albrecht ]
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: DynaValidator reset options being reset.
 
Similar Threads
Mulitple Image submit buttons question (code needed)
enctype="multipart/form-data" request.getParameter() issue
Struts 1.2 - Client and Server-side validations
clear dynavalidator form
Multi page form - MDB or SFSB ?