wood burning stoves*
The moose likes Struts and the fly likes Drop-down list empty when Validation Fails... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Drop-down list empty when Validation Fails..." Watch "Drop-down list empty when Validation Fails..." New topic
Author

Drop-down list empty when Validation Fails...

Nina Anderson
Ranch Hand

Joined: Jul 18, 2006
Posts: 148
Hi,

I have a JSP page that has database values pre-populated in a drop-down menu. When struts form validation fails and the user is forwarded back to the JSP page, the drop-down list is empty. Since the list is in a request scope, this list is not available to re-populate the form.

Storing the form or list of data in a session is not an option, because we don't want to store several forms on the website that need validation in memory.

Does anyone know a solution to this? I'm new to Struts, so I'm not too familiar on how it invokes reset() during validation.

Here's my code:

//////JSP/////////////////

...
<TD>
<bean efine id="pList" name="PackageForm" property="placeOfBusinessList" />
<html:select property="selectedPlaceOfBusiness">
<logic:iterate id="locationCbo" name="pList">
<option value='<bean:write name="locationCbo" property="placeOfBusinessCode" />'><bean:write name="locationCbo" property="placeOfBusinessName" />
</logic:iterate>
</html:select>
</TD>
....

///struts-config///

<action path="/submitBusinessProfile" scope="request"
type="org.springframework.web.struts.DelegatingActionProxy"
name="PackageForm" validate="true" input="/web/services/businessProfile.jsp">
<forward name="checkoutPage" path="/web/services/checkoutService.jsp" />
<forward name="failure" path="/web/services/businessProfile.jsp" redirect="true" />
</action>


I've been banging my heading for 3-days trying to figure this out...
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
Here's what I'd suggest:
  • Make the list of options a property of the ActionForm bean
  • override the validate method of your Action form bean. If you're using the validation framework, have your method call super.validate(...) to perform the framework validations
  • If there is an error (i.e. the ActionErrors object is not empty) then insert logic to repopulate the list of options from the database


  • Merrill
    Consultant, Sima Solutions
    Nina Anderson
    Ranch Hand

    Joined: Jul 18, 2006
    Posts: 148
    Merrill,

    The following are the questions I have:

    1. "placeOfBusinessList" is already an ArrayList property of the PackageForm. Is that what you mean?

    2. I'm using "redirect=true" for the validation. I'm new to Struts, so I'm wonder where I need to implement the "super.validate(...)" method.

    3. /submitBusinessProfile calls my ProcessCheckoutAction that extends strut's "Action" class.
    - you mention that I'll need to repopulate the list from db. So, I'm wondering...Will I need to re-query the database again to get this list? That doesn't seem like an efficient route.
    Merrill Higginson
    Ranch Hand

    Joined: Feb 15, 2005
    Posts: 4864
    Originally posted by Nina Anderson:

    1. "placeOfBusinessList" is already an ArrayList property of the PackageForm. Is that what you mean?

    Ok, I see what you mean. I didn't see the bean:define tag earlier. Yes, the placeOfBusinessList is what I mean.
    Originally posted by Nina Anderson:
    2. I'm using "redirect=true" for the validation. I'm new to Struts, so I'm wonder where I need to implement the "super.validate(...)" method.

    Whether you use redirect="true" is unimportant. What is important is whether you're using the Struts Validation Framework or just writing your own validation logic in the ActionForm's validate method. If you don't know what the validation framework is, then you're obviously not using it, so you can ignore what I said about calling super.validate(...). You only need to make that call if you're using the Validation Framework.
    Originally posted by Nina Anderson:

    you mention that I'll need to repopulate the list from db. So, I'm wondering...Will I need to re-query the database again to get this list? That doesn't seem like an efficient route.

    Realistically, you have only three choices here:

    1 - You can store the list of options or the ActionForm in the session.
    2 - You can pass the values via hidden fields in the JSP.
    3 - You can re-query the database to get the list.

    Option 1 I dimissed because you already said you're against storing anything in the HTTPSession (Which I disagree with, but that's another discussion).

    Option 2 is in my view way more trouble to implement than it's worth, especially if you have a list of JavaBeans. You'd have to write a lot of code for this, and you wouldn't really get much in return.

    That leaves us with option 3. I can understand why it might appear to be inefficient. After all, you already got these values once... why do all those disk reads just to get them again?

    In actuality, it's likely this option will not cause any additional disk reads at all. Every enterprise relational database worth having has a caching mechanism. That means that if you read the same data again a second time, the chances are pretty good that the RDB is retrieving it from its memory cache rather than from disk.

    If you implement this solution, I doubt you'll see any perceivable difference in performance, even with a large number of concurrent users.
    [ October 09, 2007: Message edited by: Merrill Higginson ]
    Brent Sterling
    Ranch Hand

    Joined: Feb 08, 2006
    Posts: 948
    Some people would argue that the most efficient web applications are the simplest ones. Don't cache things; don't store items on the session; don't pool objects; instead just process a request, pull the needed values from the database, and display the page. As long as your queries are not complex you will find that in a fraction of a second you can execute a few queries and generate a page. The overhead of executing a query is likely to pale in comparison to the network overhead required.

    I have a different approach that Merrill's. I don't do any special list processing in the validation cycle. I already have code in my "display" action to populate the needed lists. I have a "refresh" action mapping that uses the same action class as the display action but passes in a parameter value so the action knows that the page is refreshing and it should use the form values from the request. My input attribute then points to my refresh action.

    - Brent
     
    GeeCON Prague 2014
     
    subject: Drop-down list empty when Validation Fails...