aspose file tools*
The moose likes Struts and the fly likes Where and When to load collections for html:selects Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Where and When to load collections for html:selects" Watch "Where and When to load collections for html:selects" New topic
Author

Where and When to load collections for html:selects

Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15286
    
    6

I am needing to load collections for my html:selects in a form. The problem I am having is when and where to load this collections from the database in order to keep that code out of my JSP.
Just an FYI - I am using the DynaValidatorForm instead of hand coding an ActionForm class if this makes any difference, because I have read in a couple of places that this is where you should do this, but that doesn't make sense to me.
My other thought was to load them as a Bean after you log in, but that would require me putting these in the Session and I don't really want/need them in the session object.
Thanks.


GenRocket - A Test Data Generation Platform
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4419
    
    5

The approach we have used on our projects is to load frequently/commonly used options lists (e.g. list of US States) into the application context. This is done at startup. Less frequently used options lists are put in the request context. This is done before the action forwards to the JSP.
I don't know much about the DynaActionForm as I've been working with 1.0.x but from what I've read about it, I wouldn't think you'd want to put the collection there even if you could. Besides, the Struts select tag documentation is probably one of the most confusing of the lot and I've found it much easier to use when the options list has been placed in one of the context collections; YMMV.


Junilu - [How to Ask Questions] [How to Answer Questions]
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15286
    
    6

So you create a Servlet that loads at startup and you use something like:
servlet.getServletContext().setAttribute("states", states);
Is this close?
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4419
    
    5

Yes, you could do that. Or you could subclass ActionServlet and override the initOther() method and do the loading there.
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15286
    
    6

Thanks. I will look into that and see what I can come up with.
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15286
    
    6

Maybe a stupid question...
If I subclass ActionServlet how do I load it at startup.
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4419
    
    5

Yes, but bartenders are allowed 1 a day <ducking and running>
http://jakarta.apache.org/struts/userGuide/configuration.html#dd_config_servlet
set the servlet-class to your subclass
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15286
    
    6

Ohhh, you meant THE ActionServlet. Ok, I get it now.
Thanks.
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15286
    
    6

Ok, I put an ArrayList into the context. Now I am a bit confused on how to access that from the html : options tag. Any tips?
[ October 01, 2003: Message edited by: Gregg Bolinger ]
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Nevermind, I figured it out.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Where and When to load collections for html:selects
 
Similar Threads
i not wnat to lazy initialization
using dropdown box html:options example
Where and why to override hashcode() and equals() methods in hibernate domain class?
java.lang.NoSuchFieldError: EMPTY_ORDERED_ITERATOR
Hibernate